

Lambdas & Closures in PHP 5.3 alpha1  - senthil_rajasek
http://www.php.net/archive/2008.php#id2008-08-01-1

======
shaunxcode
It's hard to reply with out profanity but I just hope this gets pushed out
onto dreamhost soonish.

~~~
jonknee
An alpha of PHP doesn't belong anywhere near something like Dreamhost. Install
it locally, hack around and report the bugs.

------
jamongkad
Now all I need is a proper How-To on how to recompile this on my personal dev
machine.

------
thwarted
I can't wait to find out the problems this creates -- more fodder for "Why PHP
sucks" lists, I'm sure. Maybe they'll fix the documentation for the
create_function function and remove the implication that it creates lambdas.

~~~
thwarted
Oh, did I get downvoted because the create_function function actually does
create lambdas?

~~~
nuclear_eclipse
Well, create_function() does create a new, randomly-named function in the
global scope, meaning that it will not fall out scope at any point, so that
you can use it as a real lambda. And because of the way that create_function()
uses a string to build the function contents, by generating a new function
everytime you call it, in effect you get "real" closures too. It could most
certainly be improved to be more "natural" and complete, but that's probably
going to break backward-compati.... oh wait, this is PHP.

~~~
thwarted
Except that without scope inheritance, the "closures" created with
create_function are kind of useless, and since functions are created in the
global scope and thus are not garbage collected, creating new ones each time
it is called ends up eating a lot of memory. This makes create_function
largely useless, I don't know anyone who uses it; it's often better to just
define the function globally yourself anyway and avoid the cost of run-time
parser invocation, which is the same cost as using eval. In fact,
create_function is implemented in terms of eval.

So you get a bunch of drawbacks and none of the real benefits, plus
misrepresentation in the documentation.

Which necessitates this patch, which includes syntax changes, of course. This
can only, ahem, be better.

------
mleonhard
First C++ and now PHP. What will be the next language to get functional
closures?

Visual Basic?

~~~
henning
Uh, Visual Basic 20008/9.0 has lambda expressions built right in.

    
    
        Dim mapped = New Integer() {1, 2, 3, 4}.Select(Function(n) n * 2)
        Dim myFunc = Function(x) mapped.ToList.IndexOf(x)
        Console.WriteLine(myFunc(4)) ' prints 1 (i.e. second element, since mapped == {2,4,6,8}
    

.NET (and therefore Visual Basic) has had delegates, which are basically
typesafe function pointers, since 2005.

If you want real functional programming on the CLR, there's always F#, which
is firmly in the ML family.

AFAIK on the CLR closures are converted into classes with a single randomly
named method (and since invoking a method on a class is designed to be quite
efficient, that means you shoulodn't pay much of a performance penalty). In
other words, it mechanically, automatically does what Java programmers would
do by hand.

