Hi Stefan,
The point was not about array filtering or LINQ or any of that. It was about the plausibility of using a shorthand for closures.
Instead of this.....
array_filter($source, function($x){ return $x < 5; });
Being able to do this..... (or something like it).
array_filter($source, $x => $x < 5);
This would provide a much more friendly way of using callbacks. Lambdas have great uses, but I feel like they are underutilized in PHP. C#, for example, has made excellent use of a lambda-friendly syntax like the one above, and Java is planning something similar (for Java 8, I believe).
From: Stefan Neufeind neufeind@php.net
Date: June 28, 2011 2:20:39 PM CDT
To: internals@lists.php.net
Subject: Re: [PHP-DEV] Inline Lambda Functions
There are two projects that I've been following for awhile now: PLINQ
and PHPLinq.
http://plinq.codeplex.com/
http://phplinq.codeplex.com/
Both of them have made very solid attempts at providing LINQ-like
functionality to PHP but with both, I've been a little frustrated
with the implementations, due to the wordy syntax that PHP lambda
functions require.
[...]
Hi,
well, for your examples: Why not just array_filter()
with an appropriate
callback? Should handle it like you describe plinq does it, right?
Regards,
Stefan
Wed, Jun 29, 2011 at 9:36 AM, Jarrod Nettles jarrod@squarecrow.com wrote:
Hi Stefan,
The point was not about array filtering or LINQ or any of that. It was
about the plausibility of using a shorthand for closures.Instead of this.....
array_filter($source, function($x){ return $x < 5; });
Being able to do this..... (or something like it).
array_filter($source, $x => $x < 5);
This would provide a much more friendly way of using callbacks. Lambdas
have great uses, but I feel like they are underutilized in PHP. C#, for
example, has made excellent use of a lambda-friendly syntax like the one
above, and Java is planning something similar (for Java 8, I believe).From: Stefan Neufeind neufeind@php.net
Date: June 28, 2011 2:20:39 PM CDT
To: internals@lists.php.net
Subject: Re: [PHP-DEV] Inline Lambda FunctionsThere are two projects that I've been following for awhile now: PLINQ
and PHPLinq.
http://plinq.codeplex.com/
http://phplinq.codeplex.com/
Both of them have made very solid attempts at providing LINQ-like
functionality to PHP but with both, I've been a little frustrated
with the implementations, due to the wordy syntax that PHP lambda
functions require.[...]
Hi,
well, for your examples: Why not just
array_filter()
with an appropriate
callback? Should handle it like you describe plinq does it, right?
Jarrod
I think is a very good idea.
Which you think the syntax will be for...
- multiples arguments
- multiples sentences
- no return value
closures could be kind of wordy but they are very clear about those.
Martin Scotta
Regards,
Stefan
Instead of this.....
array_filter($source, function($x){ return $x < 5; });
Being able to do this..... (or something like it).
array_filter($source, $x => $x < 5);
This really isn't clear. Deciphering the intent of the code requires looking at all the surrounding stuff, because it could mean something totally different in an array. In other words,
// This would be a lambda
array_filter($source, $x => $x < 5);
// This would not
array($source, $x => $x < 5);
// and this would not
call_user_func_array('array_filter', array($source, $x => $x < 5));
The fact that the syntax for an expression isn't portable in places where (so far) every other expression has been, is a serious concern.
Additionally, the proposed syntax is unusable for functions with more than one line, making it similar in badness to if statements without braces. IMO the current syntax for inline functions is perfectly sufficient, even for the PLINQ example given earlier, and has massive advantages over this one in terms of readability, affordance, flexibility, and language clarity.
John Crenshaw
Priacta, Inc.
Well, we could take python's approach and use a syntax similar to this:
array_filter($source, lambda $x: $x < 5);
array_map($source, lambda $x, $y: $x < $y);
However, I would question the need. Pythonic lambdas cannot contain
anything but expressions. Therefore they cannot have any meaningful
side effects. While this is good, it also raises the question as to
the need for it. PHP has first-class anonymous functions, something
which python does not (it has local functions, or lambdas, but no true
anonymous function, every non-lambda has a name). So the need in PHP
for a lambda style construct is greater than PHP's.
And what's the gain for PHP? A few less characters to type? I'm not
sure that readability is gained, compare:
array_filter($array, function($element) { return $element < 5; });
and
array_filter($array, lambda $element: $element < 5);
I feel the first is more readable at a glance.
Anthony
Instead of this.....
array_filter($source, function($x){ return $x < 5; });
Being able to do this..... (or something like it).
array_filter($source, $x => $x < 5);
This really isn't clear. Deciphering the intent of the code requires looking at all the surrounding stuff, because it could mean something totally different in an array. In other words,
// This would be a lambda
array_filter($source, $x => $x < 5);
// This would not
array($source, $x => $x < 5);
// and this would not
call_user_func_array('array_filter', array($source, $x => $x < 5));The fact that the syntax for an expression isn't portable in places where (so far) every other expression has been, is a serious concern.
Additionally, the proposed syntax is unusable for functions with more than one line, making it similar in badness to if statements without braces. IMO the current syntax for inline functions is perfectly sufficient, even for the PLINQ example given earlier, and has massive advantages over this one in terms of readability, affordance, flexibility, and language clarity.
John Crenshaw
Priacta, Inc.