In: Computer Science
The encapsulation of Abstract data type is similar like it gathers the data of an object and puts them into a package but the closure ADT is used to encapsulate the constructors's arguments also which act as local state for the procedures.
this form of closure is different from the kind commonly found in functional language like lisp for 2 resons, first it possibly form closure over function but not ercords that is the reason record must be simulated as ann explicit case over a set.
there is a code given below for bettter understading-
nil = recursive self = record
null? = true
head = error;
tail = error;
cons = fun(y) Cell(y, self);
equal = fun(m) m.null?
cell(x, l) = recursive self = record
null? = false
head = x;
tail = l;
cons = fun(y) Cell(y, self);
equal = fun(m) (not m.null?)
above code is only for the understaning the concept that closure encapsulate the constructors arguments.
This kind of encapsulation is necessary because the closure itself has a reference to threshold and it can use that variable each time filter function calls it.This filter function itself might vbe defined in a completely separate file.
the below code will help to understand -
function_derivative(f, dx) { return function (x) { return (f(x + dx) - f(x)) / dx;
the reason is that , closure in this case outlives the execution of the function that creates it and he variables f and dx live on after the function returns derivaives.