Hi!

A few weeks ago I resurrected a two year old proposal for adding two array
functions, namely array_every() and array_some(), modelled after their
JavaScript equivalent.

https://github.com/php/php-src/pull/1385

The most notable comment was that it would be nice to support Traversable
as well as arrays; instead of only supporting this for my own functions,
I've generalised this so that other functions can take advantage of this as
well. These are the additions:

1. A ZPP argument; the "t" (for traversable) argument type has been added
that checks for either an array or implementation of zend_ce_traversable.

2. A generic iteration function, called php_traverse(); it accepts:
  a. the zval* to iterate over,
  b. a step-wise function that receives the value and key, and returns a
boolean that determines whether iteration should continue or not,
  c. a traversal mode (only values, or keys and values),
  d. a context that's sent to the step-wise function.

3. A concrete implementation of a step-wise iteration function,
 php_traverse_until, that gets called as part of array_every() and
array_some().

The implementation of above proposal can be found in the PR itself. With
more functions adopting this idea, you could see this work:

var_dump(array_reduce((function() {
    yield 1;
    yield 2;
    yield 3;
})(), function($total, $current) {
    return $total + $current;
}, 0); // int(6)

Let me know what you think!

Reply via email to