Delphi's anonymous functions in Free Pascal

classic Classic list List threaded Threaded
125 messages Options
12345 ... 7
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Michael Fuchs-5
Am 19.10.2011 20:30, schrieb Florian Klämpfl:

> 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.

It is maybe easier to write a anonymous function inline than declaring a
function and passing it with a reference to this function.

Sometimes I miss them in FPC, but I am also not sure if this feature
destroys the beauty of Pascal.

Not easy.


Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Andrew Pennebaker
In reply to this post by Florian Klämpfl
>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

On Wed, Oct 19, 2011 at 2:30 PM, Florian Klämpfl <[hidden email]> 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Sven Barth-2
Am 19.10.2011 23:28, schrieb Andrew Pennebaker:
>  >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.

Pascal isn't a functional language. You'll need to find other ways to do
this in Pascal as other Pascal users have found ways to implement their
code. You could try object oriented programming for example (though I
don't know a specific solution for "map").

Regards,
Sven

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Felipe Monteiro de Carvalho
In reply to this post by Andrew Pennebaker
On Wed, Oct 19, 2011 at 11:28 PM, Andrew Pennebaker
<[hidden email]> wrote:
> 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.

So just make a function instead of a procedure?

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Florian Klämpfl
In reply to this post by Andrew Pennebaker
Am 19.10.2011 23:28, schrieb Andrew Pennebaker:
>>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.

They are called procedure variables but can also contain functions.

> In functional languages, it's handy to do several nested map(map(map f
> ... calls.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Gregory M. Turner
In reply to this post by Florian Klämpfl
----- Original Message -----
> 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 (lambda (x) (+ x 1)) '(1 2 3))
> >
> > -> (2 3 4)

[snip!]

> 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.

[not_sure_if_serious.png]

Interesting discussion -- at least for me, I didn't know Delphi had this feature.

In case anyone really doesn't see the point, consider that this thing:

  http://www.youtube.com/watch?v=cYw2ewoO6c4

has all the capabilities of Object Pascal (with less flowery syntactic sugar to mess up the purity of the system).

:P

--
Greg Turner

P.S., hello, list!

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Florian Klämpfl
Am 20.10.2011 21:09, schrieb Gregory M. Turner:

>
> Interesting discussion -- at least for me, I didn't know Delphi had this feature.
>
> In case anyone really doesn't see the point, consider that this thing:
>
>   http://www.youtube.com/watch?v=cYw2ewoO6c4
>
> has all the capabilities of Object Pascal (with less flowery syntactic sugar to mess up the purity of the system).

If anybody does not see why increasing complexity without a good reason
should be avoided, I recommend:

http://www.youtube.com/watch?v=kYUrqdUyEpI
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Gregory M. Turner


----- Original Message -----

> Am 20.10.2011 21:09, schrieb Gregory M. Turner:
>
> > In case anyone really doesn't see the point, consider that this
> > thing:
> >
> >   http://www.youtube.com/watch?v=cYw2ewoO6c4
>
> If anybody does not see why increasing complexity without a good
> reason
> should be avoided, I recommend:
>
> http://www.youtube.com/watch?v=kYUrqdUyEpI

:)

Good point.  I guess nothing's O(free lunch).

--

-gmt
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Juha Manninen
In reply to this post by Florian Klämpfl
2011/10/20 Florian Klämpfl <[hidden email]>
If anybody does not see why increasing complexity without a good reason
should be avoided, I recommend:

http://www.youtube.com/watch?v=kYUrqdUyEpI


:-)

There is lots of confusion about anonymous functions. The name is misleading, they are called closures in other languages.
There was a similar discussion earlier, see :

The origin is from functional languages but it is fine to borrow it to OP, the same way classes and object features were borrowed earlier from other languages. Besides I think the closure addition is almost as important.

They have some very important uses, like making multithreading easier.
I copy text from Delphi help page again here because it looks so cool:

---------------------------------------------------------
Using Code for a Parameter

Anonymous methods make it easier to write functions and structures parameterized by code, not just values.

Multithreading is a good application for anonymous methods. if you want to execute some code in parallel,
you might have a parallel-for function that looks like this:

type
TProcOfInteger = reference to procedure(x: Integer);

procedure ParallelFor(start, finish: Integer; proc: TProcOfInteger);

The ParallelFor procedure iterates a procedure over different threads.

Assuming this procedure is implemented correctly and efficiently using threads
or a thread pool, it could then be easily used to take advantage of multiple
processors:

procedure CalculateExpensiveThings;
var
results: array of Integer;
begin
SetLength(results, 100);
ParallelFor(Low(results), High(results),
procedure(i: Integer) // \
begin // \ code block 
results[i] := ExpensiveCalculation(i); // / used as parameter
end // /
);
// use results
end;

Contrast this to how it would need to be done without anonymous methods:
probably a "task" class with a virtual abstract method, with a concrete
descendant for ExpensiveCalculation, and then adding all the tasks to a
queue--not nearly as natural or integrated.

Here, the "parallel-for" algorithm is the abstraction that is being
parameterized by code. In the past, a common way to implement this pattern is
with a virtual base class with one or more abstract methods; consider the
TThread class and its abstract Execute method. However, anonymous methods make
this pattern--parameterizing of algorithms and data structures using code--far
easier.

Regards,
Juha Manninen


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Juha Manninen
In reply to this post by Gregory M. Turner
2011/10/20 Gregory M. Turner <[hidden email]>
Good point.  I guess nothing's O(free lunch).

Yes, the downside here is that the concept is very different from any syntax in OP. It will be difficult to learn properly.

Juha


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Florian Klämpfl
In reply to this post by Juha Manninen
Am 20.10.2011 22:31, schrieb Juha Manninen:
>
> They have some very important uses, like making multithreading easier.
> I copy text from Delphi help page again here because it looks so cool:

The same can be done with fpc (nested if needed) procedure variables and
it will be more readable (imo)? The example still does not take
advantage of properties of anonymous methods/closurs which cause
complexity like being ref. counted, putting locals on the heap etc.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

wkitty42
In reply to this post by Florian Klämpfl
On 10/20/2011 15:17, Florian Klämpfl wrote:

> Am 20.10.2011 21:09, schrieb Gregory M. Turner:
>
>>
>> Interesting discussion -- at least for me, I didn't know Delphi had this feature.
>>
>> In case anyone really doesn't see the point, consider that this thing:
>>
>>    http://www.youtube.com/watch?v=cYw2ewoO6c4
>>
>> has all the capabilities of Object Pascal (with less flowery syntactic sugar to mess up the purity of the system).
>
> If anybody does not see why increasing complexity without a good reason
> should be avoided, I recommend:
>
> http://www.youtube.com/watch?v=kYUrqdUyEpI

+1000000000000~

;)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

wkitty42
In reply to this post by Juha Manninen
On 10/20/2011 16:35, Juha Manninen wrote:
> 2011/10/20 Gregory M. Turner <[hidden email] <mailto:[hidden email]>>
>
>     Good point.  I guess nothing's O(free lunch).
>
> Yes, the downside here is that the concept is very different from any syntax in
> OP. It will be difficult to learn properly.

i still fight, today, with much of my LISP code... :? :(
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Andrew Pennebaker
In reply to this post by Florian Klämpfl
If anybody does not see why increasing complexity without a good reason
should be avoided, I recommend:

http://www.youtube.com/watch?v=kYUrqdUyEpI

Lisp used for nuclear fail-safe systems. I doubt languages without map are up to the job.

Cheers,

Andrew Pennebaker

On Thu, Oct 20, 2011 at 3:17 PM, Florian Klämpfl <[hidden email]> wrote:
Am 20.10.2011 21:09, schrieb Gregory M. Turner:

>
> Interesting discussion -- at least for me, I didn't know Delphi had this feature.
>
> In case anyone really doesn't see the point, consider that this thing:
>
>   http://www.youtube.com/watch?v=cYw2ewoO6c4
>
> has all the capabilities of Object Pascal (with less flowery syntactic sugar to mess up the purity of the system).

If anybody does not see why increasing complexity without a good reason
should be avoided, I recommend:

http://www.youtube.com/watch?v=kYUrqdUyEpI
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Jürgen Hestermann
In reply to this post by Florian Klämpfl
Florian Klämpfl schrieb:
 > If anybody does not see why increasing complexity without a good reason
 > should be avoided, I recommend:

I agree wholeheartly. In the past Pascal was a simple still powerfull
language but meanwhile it has become a catchment tank for all features
of all other languages. This defeats the original intention of Nicholas
Wirth and the reason for what I love(d) the language. It seems everybody
tries to mimic Pascal into whatever he is used to do in other languages.
So we should no longer call this (Object) Pascal but "Chimera".
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Florian Klämpfl
In reply to this post by Andrew Pennebaker
Am 21.10.2011 03:49, schrieb Andrew Pennebaker:
>
> Lisp used for nuclear fail-safe systems
> I doubt languages without /map/ are up to the job.


Great, why don't you continue to use lisp then? If pascal does not
offers the language concepts you are used to, pascal is the wrong
language for you.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Juha Manninen
In reply to this post by Jürgen Hestermann
2011/10/21 Jürgen Hestermann <[hidden email]>
Florian Klämpfl schrieb:

> If anybody does not see why increasing complexity without a good reason
> should be avoided, I recommend:

I agree wholeheartly. In the past Pascal was a simple still powerfull language but meanwhile it has become a catchment tank for all features of all other languages. This defeats the original intention of Nicholas Wirth and the reason for what I love(d) the language. It seems everybody tries to mimic Pascal into whatever he is used to do in other languages. So we should no longer call this (Object) Pascal but "Chimera".

Please look at some old Pascal code from 80's. Lots of shortstring manipulation with pointers. Very much comparable to C, and as prone to errors as C.
It is good to realize that Pascal would have died already without the class/object additions made by Borland.
The challenge is to add only the RIGHT features. In school I must study again C++ and they clearly did it wrong. Uhhh!
IMO the OP additions have been good so far. For example generics containers eliminate ugly type-casts. It is an improvement, clearly.

Closures seem like a powerful thing which can improve code.
Now Florian mentioned that procedure variables could do the same thing. I clearly don't understand the topic enough, I must learn more.

It is funny how fashion trends also affect the evolution of programming languages.
When object oriented programming became a trend, everything had to be an object.
Java libs are a good example. Some classes (nouns) have only one method (verb) called "Execute".
Translated to human languages, it is like "man's walking executor executes" instead of "man walks".

Now the functional languages turn fashionable, maybe as a counter-effect to OO.
Some people said that programming became funny again when using a functional language (Haskel was it?).
I also want to learn and understand how to say things with only functions (verbs).

All this turbulence proves how young is the evolution of programming languages. Human languages have a much longer evolution.
You can see they have a versatile syntax. They have lots of redundancy and synonyms to ensure "error free message delivery".
The syntax (grammer) is not always simple but it is expressive.

Juha


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Graeme Geldenhuys-2
In reply to this post by Florian Klämpfl
On 2011-10-20 22:55, Florian Klämpfl wrote:
>
> The same can be done with fpc (nested if needed) procedure variables and
> it will be more readable (imo)? The example still does not take


This seldom happens, but here I fully agree with Florian. ;-) Every
example of anonymous methods I have seen so far, can easily be done with
OP's procedure variables too.  Maybe anonymous methods were introduced
in other languages because they didn't have something like OP's
procedure variables. I guess Object Pascal was yet again ahead of
everybody else. :)



Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

leledumbo
Administrator
> Maybe anonymous methods were introduced
in other languages because they didn't have something like OP's
procedure variables

IMO, it's because they're lazy to declare things before they use it. Often, an anonymous function which first used just once eventually used more than once. When that happens, users of languages with anonymous function would refactor the function by removing it from the anonymous context into a standalone named function, and re-pass it. Where as for Pascal style users, that's not needed because the function is already named and standalone.
Reply | Threaded
Open this post in threaded view
|

Re: Delphi's anonymous functions in Free Pascal

Alexander Shishkin
In reply to this post by Andrew Pennebaker
18.10.2011 0:53, Andrew Pennebaker пишет:
> Does Free Pascal have anonymous functions that you can pass around, e.g.
> to a sort(compare : function, arr : array) function?
>

anonymous functions = closures = lambdas are part of functional
paradigm. object pascal itself is not functional language.
But there is modern trend to design multiparadigm languages (usually OO
+ functional + imperative).
Delphi followed this trend. So the question is: should freepascal follow
this trend too?



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12345 ... 7