Think I’m following. 

The compelling feature here for me is the idea of an “array walker” that can be 
broken out of.

all() could be and() at which point this should be equivalent.

$collection = [1, 2, 3];

If (count($collection) === count(array_filter($collection, “is_int”))

any() could be or() at which point this should be equivalent, short of the 
breaking early.

If (count(array_filter($collection, “is_int”)) > 0)

The benefit I see of the first (using count === count) is that I could store 
the result of the filter to use later based on the passing of the condition 
without iterating again. (See str_replace, where the function returns the 
result, while making count available to the caller as well: 
https://www.php.net/manual/en/function.str-replace.php 
<https://www.php.net/manual/en/function.str-replace.php> - using pass by 
reference.)

The drawback of the second (using count > 0) is that I have to iterate over the 
entire collection despite only needing one element to pass the predicate.

Would there also be an xor() or equivalent?? one() or something??

If (count(array_filter($collection, “is_int”)) === 1)

Here’s an implementation of each() I made that allows for breaking by the 
caller using a passed by ref third argument in the closure: 
https://github.com/8fold/php-shoop/blob/2a8b6fc41c545ff71690562ac2f35a12457b1514/src/Traits/EachImp.php
 
<https://github.com/8fold/php-shoop/blob/2a8b6fc41c545ff71690562ac2f35a12457b1514/src/Traits/EachImp.php>
 - I’ve since deprecated that functionality and not sure if I’ll be bringing 
that functionality back because of frustrations and philosophical arguments 
with myself. :)

Part of me wishes the “all()” and “any()” names were more descriptive of what’s 
going on. 

When I call for “all” in a database or some ORM, I *get* all - I’m not 
verifying “all pass [a predicate]”.

Cheers,
Josh

> On Aug 29, 2020, at 6:48 PM, tyson andre <tysonandre...@hotmail.com> wrote:
> 
>> The “any” check is just to if anything in the iterable passes the predicate, 
>> yeah??
>> 
>> What I find myself doing more often is wanting the first thing to satisfy 
>> the predicate - a “first” function, if you will.
> 
> There's multiple things such a function could return - The key of the entry 
> (could be false/null), the value of the entry (could be false/null), or a 
> combination of the key and value (rarely what we want).
> 
> I'm not aware of any exact matches for that - array_filter processes all 
> values, and array_search doesn't accept a callback.
> `array_search_callback()` could be added, but I don't plan to expand the 
> scope of this RFC and there are multiple ways to do that.
> 
>> This could skip the step of iterating to find of something satisfies the 
>> predicate. Then iterating again to get one or more items that do satisfy it.
>> 
>> Trying to think of a use case where I would want to check, but not do 
>> anything with that knowledge.
> 
> Anywhere where you'd want to check for membership in a short-circuiting way, 
> similar to situations where `||` or `&&` would be called,
> but with a variable number of callbacks or repeating the same operation 
> multiple times.
> It also reduces the indentation and line count needed
> 
> ```
> $this->assertTrue(all($valueList, fn($v) => $v->isValid()));
> 
> all($startupCallbacks, 'call_user_func') || exit("startup failed");
> 
> if (any($fieldList, fn($field) => in_array($field->name, ['fooEnabled', 
> 'fooEnabled2']))) {
>    doFoo();
> }
> ```
> 
> - Tyson
> 
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
> 

Reply via email to