Underscore.php
brianhaveri.github.com
brianhaveri.github.com
__::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.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.
You had a typo, there.
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-comprehension...
With namespaces:
namespace _;
function each() {
}
// chaining can be achieved with this:
class Wrapper {
private $coll;
function __construct($coll) {
$this->coll = $coll;
}
function __call($name, $args) {
array_unshift($this->coll,$args);
return new self(call_user_func_array('_\\'.$name, $args));
}
}
function __($coll) {
return new Wrapper($coll);
}
use _;
_\each(...);
// or
__($coll)->each(...);
// I can add new functions
namespace _;
function something(){}
With static methods: class __ {
function each() {
}
}
// throws strict errors
__::each(...);
// can't add functions
Also it seems that the normal way to use the library is to call isntance methods statically, which triggers many warnings with E_STRICT :(https://github.com/lstrojny/functional-php also brings some functional stuff to php.
__::mixin(array(
'capitalize'=> function($string) { return ucwords($string); }
));
__::capitalize('moe'); // 'Moe'Where there is something not-much-OOP about something, PHP supplies global functions (compare md5() to 5 lines of Java code) instead of classes. So, using global functions would. in many ways, be much more PHP-ish than using static class methods (which is more Java-ish if you ask me).
Of course, you don't want to pollute the global namespace too much, so using the '_' namespace sounds excellent. I really like the idea.
I'm also not entirely opposed to the namespace approach, but syntactically I would rather call:
__::foo('bar');
than use _;
__\foo('bar'); PHP reserves all function names starting with __ as magical. It is
recommended that you do not use function names with __ in PHP unless you
want some documented magic functionality.
It's only explicitly reserved for functions, so you're technically ok here, but it still may cause confusion.You do know that PHP has built-in min and max functions, right?
And array_map.
And array_walk.
And array_filter.
And array_reduce.
And array_slice.
And array_unique.
And array_search.
And range.
And empty.
And is_object.
And is_array.Unfortunately, there's not much in the way of documentation as of now, but a look at the API should prove illuminating. For starters, the API is meant to be inherently both chainable and extensible with room for implementations based on eg. SPL classes. The idea is for the collections not to be throwaways for intermediate processing but to instead grow into a part of your application's internal API. Although the code should be in perfect working order (we're using it in one production system currently), the concept is a bit experimental at this stage. I'd very much appreciate any comments you have.
Perhaps if one attempted to pull the parser out of Facebook's Hiphop and use that..
Some standalone:
https://github.com/nikic/PHP-Parser http://pear.php.net/package/PHP_Parser
Some part of other projects:
https://github.com/facebook/hiphop-php https://github.com/facebook/pfff/tree/master/lang_php/parsin... https://github.com/manuelpichler/pdepend http://sourceforge.net/projects/phpsecaudit/ http://lwn.net/Articles/239941/ http://www.score.is.tsukuba.ac.jp/~minamide/phpsa/ http://www.program-transformation.org/PHP/PhpSat https://www.fortify.com/ssa-elements/threat-intelligence/rat... http://lara.epfl.ch/web2010/phantm http://www.phpcompiler.org/ http://www.roadsend.com/home/index.php?pageID=compiler
And probably many others.
How do I force a line break ? Where is the syntax doc ?
You can't use ruby or python to build wordpress plugins, for instance.
> Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects. It's the tie to go along with jQuery's tux.
Underscore is both a utility belt and a bowtie?
Whenever I've done PHP, I've always used a hand-rolled functional library. I will certainly be using this now, though.
I can earn a living with PHP in my tool-kit that Scheme/Haskell/Clojure alone will not provide.
Assertion 1: people who understand right and left folding are fairly high on the scale of CS knowledge.
Assertion 2: a person with suitably advanced CS knowledge can find a well-paying job or start a project that is rewarding and grants them a high-level of autonomy -- i.e. they don't need to uninspiring shitwork.
Assertion 3: PHP is for shitwork, and people only develop in it when they don't have a choice.
I agree with these assertions, and the question makes sense to me.
2. I don't have a CS degree, I'm a glorified designer eager to learn.
3. Now I know what right and left folding are.
I wasn't saying that merely knowing what folding is means you have CS knowledge. I was saying that such knowledge is characteristic of knowledgeable programmers.
But learn an alternative, and you're going to like programming even more.