Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The case for point free style in Scala

Tags:

This may seem really obvious to the FP cognoscenti here, but what is point free style in Scala good for? What would really sell me on the topic is an illustration that shows how point free style is significantly better in some dimension (e.g. performance, elegance, extensibility, maintainability) than code solving the same problem in non-point free style.

like image 955
enhanced_null Avatar asked Jul 25 '11 07:07

enhanced_null


People also ask

What is point free notation?

Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or "points") on which they operate. Instead the definitions merely compose other functions, among which are combinators that manipulate the arguments.

What is point free style Haskell?

Point free style means that the code doesn't explicitly mention it's arguments, even though they exist and are being used. This works in Haskell because of the way functions work.

What is true functional programming?

Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.


1 Answers

Quite simply, it's about being able to avoid specifying a name where none is needed, consider a trivial example:

List("a","b","c") foreach println 

In this case, foreach is looking to accept String => Unit, a function that accepts a String and returns Unit (essentially, that there's no usable return and it works purely through side effect)

There's no need to bind a name here to each String instance that's passed to println. Arguably, it just makes the code more verbose to do so:

List("a","b","c") foreach {println(_)} 

Or even

List("a","b","c") foreach {s => println(s)} 

Personally, when I see code that isn't written in point-free style, I take it as an indicator that the bound name may be used twice, or that it has some significance in documenting the code. Likewise, I see point-free style as a sign that I can reason about the code more simply.

like image 186
Kevin Wright Avatar answered Oct 03 '22 20:10

Kevin Wright