CSC/ECE 517 Fall 2010/ch1 1b YL: Difference between revisions
Line 33: | Line 33: | ||
* First-class function - Whether the language supports passing function as an argument | * First-class function - Whether the language supports passing function as an argument | ||
* Out of context reference - Whether language supports referring variable out of scope of its context | * Out of context reference - Whether the language supports referring variable out of scope of its context | ||
* [http://en.wikipedia.org/wiki/Currying Function currying] - It is the technique by which function with multiple arguments can be called as a function with chain of functions, each with single argument. Some languages support this and some do not. | * [http://en.wikipedia.org/wiki/Currying Function currying] - It is the technique by which function with multiple arguments can be called as a function with chain of functions, each with single argument. Some languages support this and some do not. |
Revision as of 06:09, 26 October 2010
Closures in Ruby vs. closures in other languages
Closure is the important feature of functional programming. Ruby is a very well known functional programming language. Other non functional programming languages like Java, C++ also support closure like features to some extent. But there are some differences when closures in Ruby and closures in other languages are taken into consideration.
Closures
A closure can be defined as
The programming language function which can be passed around like any other object and still remembers the original context where that function is declared first.
Closure is also called first-class function. First-class function is the function which is treated as first-class object which can be passed as an argument. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions.[1]
This way, in closures, variables can be referred out of the scope of the local functions. This ability of accessing original context makes closure different from normal objects.
Closure concept can be illustrated with an example in JavaScript.
<source lang="javascript"> // Return a list of all employees with at least 'threshold' salary. function highlyPaidEmployees(threshold) { return empList.filter( function (emp) { return emp.salary >= threshold; } ); }
In above javascript function highlyPaidEmployees
, another anonymous function is defined. This function is passed to the Array.filter
method with free variable threshold. This free variable is assigned the value that is passed during the function call of highlyPaidEmployees. Thus the anonymous function defined inside is called repeatedly by filter method with free variable threshold having assigned the value passed to the highlyPaidEmployees. This follows both the properties defined in the closure definition i.e. function being passed as an argument and function being able to access free variable out of its context.
Closures are supported by many languages, but most famously used in Ruby. The next sections will compare the implementations of closures in different languages.
Basis of differences in implementations of closure
Even though many languages support closure concept, there are significant differences in closure implementation in different languages. Some languages support closure concept partially. Closure implementation in different languages differ with respect to following aspects:
- First-class function - Whether the language supports passing function as an argument
- Out of context reference - Whether the language supports referring variable out of scope of its context
- Function currying - It is the technique by which function with multiple arguments can be called as a function with chain of functions, each with single argument. Some languages support this and some do not.
- Control structure using closure - Whether the language supports closure for control structure or not.
In the next sections, the concept of closures in Ruby, Lisp, ML, C, C++ and Java is explained.
Closures in Ruby
In Ruby, closures can be implemented using Blocks, Proc objects and lambda expressions.
Blocks
Blocks are like closures, because they can refer to variables from their defining context. Within the body of the invoked method, the code block may be called using the yield keyword. Following example explains the concept of blocks in Ruby.[2]
def thrice yield yield yield end x = 5 puts "value of x before: #{x}" # value of x before: 5 thrice { x += 1 } puts "value of x after: #{x}" # value of x after: 8
In the above example, thrice
is called. There are three yield
statements in thrice
method. Each time yield is executed in thrice
, it calls block { x += 1}
and increments x by 1. Every yield
call remembers value of x
during different calls. Even though x
is not defined in thrice
method, block accesses x
from the location where this block is defined. Thus a block remembers the context in which it is defined, and it uses that context whenever it is called.
Proc
If we want to use same code block multiple times then concept of block is not helpful, as it requires passing this block of code everytime. Instead we can use Proc which is same as procedure in other languages.[3]
class Array def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array_1 = [1, 2, 3, 4] square = Proc.new do |n| n ** 2 end array_1.iterate!(square) puts array_1.inspect # [1, 4, 9, 16]
In the above example, square
Proc is created. As the name suggests, square
proc gives square of the passed argument. This square
proc object is passed to the iterate!
method which iterates over each element of array to square it. To iterate over an array it uses each_with_index
method. The statement code.call
inside iterate!
method executes square
Proc for each array element and hence we see all the elements of an array array_1
squared in the final output.
Lambda
Some languages support anonymous function or lambda. Proc in Ruby acts similar to lambda or anonymous function. Lambdas are also available in Ruby.
class Array def iterate!(code) self.each_with_index do |n, i| self[i] = code.call(n) end end end array = [1, 2, 3, 4] array.iterate!(lambda { |n| n ** 2 }) puts array.inspect # [1, 4, 9, 16]
The above example is almost similar to the Proc example. The only difference is, instead of creating square
Proc, lambda expression for the same is created. Here lambda { |n| n ** 2 }
is passed as Proc to iterate!
method. And thus code.call
calls this lambda expression. Lambdas seem to be exactly the same as Procs. However, there are differences. These differences are provided at Ruby blocks, procs and lambdas.
Control Structures Using Closure
In Ruby, control structures can be implemented using closure. In languages like C or C++, loops provide all the details like iterator, boundary values of iterator etc. Instead, these things are abstracted in Ruby using closure.
3.times {puts "Ruby"} # prints: Ruby # Ruby # Ruby
In the above example, Ruby is printed three times and this is achieved using Blocks in Ruby. Implementing control structure using closure also reduces lines of code.
Currying in Ruby Using Closure
Ruby supports function currying. Currying means creating a new function out of an existing function by fixing the value of some of its input parameters. A simple example illustrates this concept. Consider a function which raises its first parameter to the power specified by the second parameter.[4]
def power(x,y) val = 1 1.upto(y) {|a| val = val * x} val end square = lambda {|x| power(x,2)} cube = lambda {|x| power(x,3)} puts square.call(4) # prints 16 puts cube.call(3) # prints 27
Thus in the above example, square
and cube
functions are created by fixing power parameter to 2 and 3.
Instead of defining a new method, the existing one is curried. In Ruby, we can curry a method using lambda.
Closures in other languages
Closures in Lisp
Lisp is famous for its usage of Closure. Following is the example to demonstrate closure like functionality in Lisp.
(let ((counter 0)) (defun new-id () (incf counter)) (defun reset-id () (setq counter 0))) --> RESET-ID (new-id) --> 1 (new-id) --> 2 (reset-id) --> 0
In the above example, both functions new-id()
and reset-id()
work on the same copy of counter. Even though counter is defined in one context, it is referred out of its scope, which is one of the properties of the closure.
Closures can be returned from functions and then called with FUNCALL
:
(defun make-adder (n) #'(lambda (x) (+ x n))) --> MAKE-ADDER (funcall (make-adder 2) 5) --> 7 (funcall (make-adder 12) 500) --> 512
In the above example, make-adder
method returns anonymous function which can be used to pass as an argument.
Thus, Lisp allows referencing variables out of context. Also it allows to treat function as first-class function. Thus closures in Lisp satisfiy the properties of closures.
Closures in ML
ML is a functional language which supports closures and function currying. Consider the following closure example.[5]
val x = 5 fun f y = x * y val x = 8 val z = f 10 # z becomes 50
Here the value of z
becomes 50 and not 80. This is because of lexical scoping. When a function expression is evaluated, a copy of the environment is attached to the function. Subsequently, all free variables of the function (i.e., those not occurring as parameters) are resolved with respect to the environment attached to the function; the function is therefore said to be “closed” with respect to the attached environment. While applying the function, the active environment is replaced by function's attached environment and it is restored after the completion of function call.
In the above example, environment associated with the function contains the declaration val x = 5
to record the fact that at the time the function was evaluated, the variable x
was bound to the value 5. The variable x
is subsequently re-bound to 8, but when f
is applied, the binding of x
is temporarily reinstated to 5.
To understand the concept of function currying in ML, consider the following simple example of map:
fun map' (f, nil) = nil | map' (f, h::t) = (f h) :: map' (f, t)
When a function is passed to map'
function, it is applied to every element of the list.
e.g. map' (fn x => x+1, [1,2,3]) evaluates to the list [2,3,4]
Now consider a map
function that is specialized to the given function f
that can be applied to different lists.
fun map f nil = nil | map f (h::t) = (f h) :: (map f t)
The function map
takes a function of type ('a->'b)
as an argument and yields another function of type 'a list -> 'b list
as a result. Thus, a two-argument function (more properly, a function taking a pair as an argument) is changed to a function that takes two arguments in succession, yielding the first function that takes the second as its sole argument. This is function currying.
Closures in C
The C language does not support closure directly, but it has some extensions which add closure like functionality. Nested functions satisfy some properties of closure.
A nested function is a function defined inside another function. It's scope is limited to the enclosing function. The nested function can access all the variables of the containing function that are visible at the point of its definition. [6] This is called lexical scoping.
int arrayProcess (int *array, int offset, int size) { int arrayAccess (int *array, int index) { return array[index + offset]; } // arrayAccess can access outer function's(arrayProcess) 'offset' variable int i; /* ... */ for (i = 0; i < size; i++) /* ... */ access (array, i) /* ... */ }
Here, the inner function arrayAccess
can refer to outer function's variable offset
.
The nested function can be called from outside the enclosing function by passing its address to another function:
int arrayProcess (int *array, int size) { void store (int index, int value) { array[index] = value; } intermediate (store, size); // function store passed as an arguemnt }
Here, the address of the function store
is passed to the function intermediate
as an argument. So, intermediate
can call store
with index
and value
arguments. But, intermediate
can call store
as long as the enclosing function arrayProcess
does not exit.
It is not safe to call the nested function through its address after its enclosing function is exited. But, if the nested function does not refer to anything that has gone out of scope then it will work.
Another closure like feature C language provides is function pointers.[7]
int addition(int a, int b) { return a + b; } // <ptr2Func> is a pointer to a function which takes two int values and returns an int void getPoniter(int (*ptr2Func)(int, int)) { int result = (*ptr2Func)(10,20); // call using function pointer printf("Result = " + result); // Result = 30 } void passPoniter() { int (*ptr2Function)(int, int) = NULL; ptr2Function = &addition; //& is optional here but prefered getPoniter(ptr2Function); }
Function pointers are nothing else but variables. In the above example, ptr2Function
is a pointer to the function addition
which takes two integers and returns an integer. It is then passed to another function getPointer()
, which can then call the function addition
using that pointer.
Function pointer can be passed as an argument which is one of the desired properties of the closures.
Closures in C++
C++ does not support nested function like C does. But, it supports function pointers similar to C. Also, C++ has Boost library which provides lambda expressions.
Here is a simple example of Boost lambda expressions in Standard Template Library(STL).
list<int> v(10); for_each(v.begin(), v.end(), _1 = 1); // initializes to v[0]=1, v[1]=1,.., v[9]=1
In the C++ version of lambda expressions, formal parameters have predefined names. In the current version of the library, there are three such predefined formal parameters, called placeholders: _1
, _2
and _3
. They refer to the first, second and third argument of the function defined by the lambda expression.
The expression _1 = 1
creates a lambda functor which assigns the value 1 to every element in v
.
More on Boost libraries can be found at Boost library website [8]
Thus, C++ supports first-class functions as it has function pointers and they can be passed as arguments. It also supports control structures using closure in terms of Boost library lambda expressions.
Closures in Java
A closure in Java is implemented using anonymous function. It is declared using the syntax
{ formal parameters => statements expression }. For example, { int i => i + 1 }
is a function that takes a single int argument and returns its value incremented by 1. A closure can be invoked by the invoke
method. For example, { int i => i + 1 }.invoke(1)
will call the closure with argument 1. Below is the Java class implementing closure.[9]
public class SimpleClosure { public static void main(String[] args) { // function with one argument; return value is the passed value incremented by 1 int answer = {int x => x+1 }.invoke(10); System.out.println(answer); // answer = 11 } }
In the above main
function, the anonymous function accepts an argument, increments passed value by 1 and returns it. In the above example answer will be 11.
Each function that takes multiple arguments can be transformed into a function that takes a single argument. For example, function plus
can be transformed into anotherPlus
:[10]
{ int, int => int } plus = { int x, int y => x + y }; { int => { int => int } } anotherPlus = { int x => { int y => x + y } }; int threePlusFour = anotherPlus.invoke(3).invoke(4); // threePlusFour = 7
Thus Java implements function currying using anonymous functions.
Comparison
Comparison of closures in different languages can be done on the basis of the factors defined in section 2. Below is the tabular comparison.
Factor | Closures in Ruby | Closures in Lisp | Closures in ML | Closures in C | Closures in C++ | Closures in Java |
---|---|---|---|---|---|---|
First-class function | Yes | Yes | Yes | Yes | Yes | Yes |
Out of context variable reference | Yes | Yes | Yes | No | Yes | Yes |
Function currying | Yes | Yes | Yes | No | Yes | No |
Control structure using closures | Yes | No | No | No | Yes | No |
Conclusion
Closures are extensively used in Ruby. Not only advanced features but very simple language features like control structures are implemented using closure. As a result Ruby has always less lines of code compared to other languages.
Closure is becoming popular day by day and hence it is being introduced and used in non-functional languages also.