I often hear that scala has a ability to pass function as a parameter to another function. I would like to know the difference between passing a value as a parameter vs passing function itself as the parameter. How the latter approach brings advantage to the programming. If functions are passed as the parameter how the object references are being created since functions are not tied to any objects.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Have you ever used the visitor or strategy patterns, factories or dependency injections? Have you ever passed or asked for an object of type
In the visitor pattern, instead of having
In the strategy pattern, the strategy interface is just a function. In the wiki example, replace the interface with
In the factory method pattern, the whole factory class is a function. In the wiki encapsulation example,
As to your last question, I do not understand very well what you mean. A function, in Scala, is an object implementing the
The advantage is the ability to perform Lazy Evaluation of the expression that is being passed in. Instead of having to calculate the real value of x at the time the expression is passed in. It can create a thunk which defers execution until its absolutely needed.
Why is this useful? One instance is that you can define an infinite list of numbers given an expression.
An example (Haskell)
In this case the value fibs is given an expression which is composed of multiple functions, in this case map fst iterate (and an anonymous expression or lambda).
If fibs was to evaluate immediately it would cause a stack overflow as it would instantly try to calculate an infinite fibonacci sequence. If i were to call
Then that's what would actually happen as sum will immediately try to attempt to evaluate fibs and cause a stack overflow.
However if I were to use fibs with another expression
This sum will still cause fibs to evaluate, however takewhile will cause the evaluation to end when the list reaches 4 million.
Functions as parameters are useful in many many many ways. Consider Java's Listener/Adapter pattern and general situations where callbacks are needed (event-based APIs). In Java, to have the GUI do something on mouse click or key press you will need to implement whole appropriate Listener interface (or extend Adapter class). Contrast this with just passing a function object to be executed on mouse click (you can see this in C# with its event system). It is much simpler.
There are also many situations where you would want to have a certain algorithm, where a part of it is customizable by using a passed-in function. E.g.
These are just a few cases where a general algorithm of "go over collection and do X" is greatly improved by functions as first-class objects, because it allows the X to be user-specified through the function parameter. This is not all however... there are more complex uses of functions as parameters (first-class functions), many involving functions which generate functions. You should look at Clojure programming language for examples of these uses.
Edit: I don't know the exact implementation details but generally the implementation uses a class or and interface instance to represent the function in JVM. This class has a priviledged method that gets called when function object is invoked. This plumbing is made by Scala compiler.