4 ms·
This will be even more aesthetically pleasing once PHP 5.4 is released with support for array literals: __::map([1, 2, 3]), function($n) { return $n * 2; }
by yahelc 15y ago
This will be even more aesthetically pleasing once PHP 5.4 is released with support for array literals:
__::map([1, 2, 3]), function($n) { return $n * 2; });
__([1, 2, 3])->map(function($n) { return $n * 2; });
That, combined with array dereferencing (ie, explode(",",$str)[0] ), PHP will start to look a lot like JavaScript.
- masklinn 15y ago> This will be even more aesthetically You're asserting it's currently pleasing... And most of those functions generally are not used with array literals (because the array is the data you're workin on/transforming, just do the transformation statically if you have an array literal), so array literals don't have much value for such a library, it only improves the examples.
- bkudria 15y ago> PHP will start to look a lot like Perl. You had a typo, there.
- armandososa 15y agoThis. is. not. reddit. No FTFY type of comments please.
- deleted 15y ago[deleted]
- jerf 15y agoFor all of its other sins, simple functional programming (that is, first-order functions and the basic uses thereof, rather than immutability and the other more modern FP characteristics) is easier than in most languages. In fact it's easier in Perl than in Erlang, a putatively-functional language! map { $_ * 2 } (1, 2, 3) - Perl lists:map(fun (X) -> X * 2 end, [1, 2, 3]) - Erlang map (*2) [1, 2, 3] - Haskell The last one for comparison. Erlang can do better with a list comprehension, though: [X * 2 | X <- [1, 2, 3]] which except for case also works for Haskell. But that exclusively works with only map and filter (in Erlang [1]), Erlang is still surprisingly klunkier than Perl for everything else (folds, actually passing functions around, etc). [1]: Haskell recently re-added monad comprehensions, so comprehensions aren't as limited anymore. http://blog.n-sch.de/2010/11/27/fun-with-monad-comprehensions/ http://blog.n-sch.de/2010/11/27/fun-with-monad-comprehension...