Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...)

From: Date: Mon, 31 Aug 2020 02:08:05 +0000
Subject: Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...)
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-111735@lists.php.net to get a copy of this message
I was actually working on this sort of thing recently. _Technically_, you can support all, any, and first by using a single function: function find_first(iterable $of, callable($value, $key): bool $thatSatistifes): Iterator It converts the $iterable into an Iterator, then calls the callback for each key/value pair until one returns true, and then always returns the iterator at the current position. 1. This allows you to know both key and value when making a decision. 2. By returning an iterator the caller can get both key and value. 3. By returning an iterator it can handle both the empty case and not found cases with $result->valid() === false. 4. By returning an iterator it might be useful for processing the remainder of the list somehow. I'm not sure that in practice it would be that friendly, but it's worth pointing out for discussion at least.

« previous php.internals (#111735) next »