[fpc-pascal] Re: Delphi's anonymous functions in Free Pascal

Florian Klämpfl florian at freepascal.org
Wed Oct 19 20:30:11 CEST 2011


Am 19.10.2011 20:23, schrieb Andrew Pennebaker:
> Practical uses for referencable anonymous functions:

For such applications one uses procedure variables in pascal.

> 
> (map f collection)
> 
> This is the prototypical way to run a function over each element in a
> collection, returning the results.
> 
> Example:
> 
> (map (lambda (x) (+ x 1)) '(1 2 3))
> 
> -> (2 3 4)
> 
> (sort compare collection)
> 
> When dealing with complex data types, the user may want to implement a
> custom compare function.
> 
> (sort (lambda (x y) (- y:employee-id x:employeeid)) (list emp1 emp2 emp3))
> 
> -> (emp3 emp1 emp2)
> 
> (zip f collection1 collection2)
> 
> Similar to map, zip runs a 2-input function over the elements of the
> collections, returning the results.
> 
> (zip (lamba (x y) (+ x y)) '(1 2 3) '(4 5 6))
> 
> -> (5 7 9)
> 
> At first glance, each of these examples may seem pointless. Can't we
> implement the same behavior without referencing anonymous functions?
> Yes, we can, but only for a specific function. If you want your database
> library to allow users to customize sorting, you'll need referencable
> anonymous functions.

I still don't see why this cannot be done by procedure variables: one
can easily pass a procedure reference to a compare function to any sort
library call. The example is far from explaining why it is needed to be
able to return a reference to an anonymous method to the outside of the
enclosing function.



More information about the fpc-pascal mailing list