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

Andrew Pennebaker andrew.pennebaker at gmail.com
Wed Oct 19 23:28:35 CEST 2011


>From what I gather, procedure variables can indeed be referenced and passed
around, etc.

However, procedures do not return anything, so it's hard to chain them. In
functional languages, it's handy to do several nested map(map(map f ...
calls.

Cheers,

Andrew Pennebaker
www.yellosoft.us

On Wed, Oct 19, 2011 at 2:30 PM, Florian Klämpfl <florian at freepascal.org>wrote:

> 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.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111019/cab44636/attachment.html>


More information about the fpc-pascal mailing list