Levi Morrison wrote on 01/10/2015 18:38:
Chain works is because the functions passed to it accept iterables as
their only parameter and return either an another iterable or a
reducing function (in this example sum is the reducer). This is why
the functions are returning closures that accept only one parameter.

OK, that makes sense of one level of indirection. It doesn't however seem to justify the nesting in reduce()

If I understand correctly, your library obliges me to write this:

    $algorithm = chain(
        map(function($x) => $x * 2),
        reduce(0)(function($acc, $val) => $acc + $val)
    );
    $algorithm([1,2,3]);


But that seems to have no advantage over this:

    $algorithm = chain(
        map(function($x) => $x * 2),
        reduce(0, function($acc, $val) => $acc + $val)
    );
    $algorithm([1,2,3]);


Which would make the body of reduce significantly simpler, because it's just one named function accepting a closure, and returning a closure, with no further nesting.

The function chaining only appears to be useful if the closure returned by reduce(0) is to be passed or applied in some other way, and I can't think of when that would be required.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to