HomeDevCentral

Implement any() and all() for all collections

Description

Implement any() and all() for all collections

Summary:
The any() and all() methods allow to determine if elements of the collection
pass a boolean test:

  • any($fn) will return true if at least one element satisfies $fn
  • all($fn) will return true if all elements of the collection satisfy $fn

Those was considered as functions for inclusion in PHP language, but rejected.

The $fn callback parameter must be a callable with one or two arguments,
fn ($value) or fn ($key, $value) and return a boolean.

Any other values than true will be falsy. That allows to cope with legacy
PHP functions returning true when the operation succeeds and an error code
if not, like Memcached::deleteMulti().

The execution of the callbacks stop when the result is definitive and can't
be affected by further values:

  • for any($fn), execution stops after the first callback returning true
  • for all($fn), execution stops after the first callback returning false

References:

Test Plan:
Unit tests provided.

Actual use in the new keruald/cache library

Reviewers: dereckson

Reviewed By: dereckson

Differential Revision: https://devcentral.nasqueron.org/D3221

Details

Provenance
derecksonAuthored on Jul 2 2023, 15:13
derecksonPushed on Jul 2 2023, 18:50
Reviewer
dereckson
Differential Revision
D3221: Implement any() and all() for all collections
Parents
rKOT624f6f9ac589: Upgrade to PHPUnit 10
Branches
Unknown
Tags
Unknown