rfc:any_all_on_iterable
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:any_all_on_iterable [2021/02/06 14:42] – tandre | rfc:any_all_on_iterable [2021/02/22 15:28] – tandre | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: PHP\iterable\any() and all() on iterables ====== | ====== PHP RFC: PHP\iterable\any() and all() on iterables ====== | ||
- | * Version: 0.4 | + | * Version: 0.6 |
* Date: 2020-08-30 | * Date: 2020-08-30 | ||
* Author: Tyson Andre, tandre@php.net | * Author: Tyson Andre, tandre@php.net | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
* Implementation: | * Implementation: | ||
Line 49: | Line 49: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Add the functions '' | + | Add the functions '' |
(The namespace '' | (The namespace '' | ||
+ | |||
+ | **The implementation is equivalent to the following polyfill:** | ||
<code php> | <code php> | ||
namespace PHP\iterable; | namespace PHP\iterable; | ||
- | /** Determines whether any element of the iterable satisfies the predicate. */ | + | /** |
- | function any(iterable $input, ?callable $callback = null) { | + | * Determines whether any element of the iterable satisfies the predicate. |
+ | * | ||
+ | * | ||
+ | * If the value returned by the callback is truthy | ||
+ | * (e.g. true, non-zero number, non-empty array, truthy object, etc.), | ||
+ | * this is treated as satisfying the predicate. | ||
+ | * | ||
+ | * @param iterable $input | ||
+ | * @param null|callable(mixed): | ||
+ | */ | ||
+ | function any(iterable $input, ?callable $callback = null): bool { | ||
foreach ($input as $v) { | foreach ($input as $v) { | ||
if ($callback !== null ? $callback($v) : $v) { | if ($callback !== null ? $callback($v) : $v) { | ||
Line 64: | Line 76: | ||
return false; | return false; | ||
} | } | ||
- | /** Determines whether all elements of the iterable satisfy the predicate */ | + | </ |
- | function all(iterable $input, ?callable $callback = null) { | + | <code php> |
+ | /** | ||
+ | * Determines whether all elements of the iterable satisfy the predicate. | ||
+ | * | ||
+ | * If the value returned by the callback is truthy | ||
+ | * (e.g. true, non-zero number, non-empty array, truthy object, etc.), | ||
+ | * this is treated as satisfying the predicate. | ||
+ | * | ||
+ | * @param iterable $input | ||
+ | * @param null|callable(mixed): | ||
+ | */ | ||
+ | function all(iterable $input, ?callable $callback = null): bool { | ||
foreach ($input as $v) { | foreach ($input as $v) { | ||
if (!($callback !== null ? $callback($v) : $v)) { | if (!($callback !== null ? $callback($v) : $v)) { | ||
Line 80: | Line 103: | ||
- If this was provided only in userland, there' | - If this was provided only in userland, there' | ||
- If the standard library provided it, then polyfills for newer php functionality could adopt this as well, making cleaner code easier to write. | - If the standard library provided it, then polyfills for newer php functionality could adopt this as well, making cleaner code easier to write. | ||
+ | |||
+ | ==== Implementation Details ==== | ||
+ | |||
+ | When '' | ||
+ | |||
+ | When '' | ||
+ | |||
+ | |||
+ | <code php> | ||
+ | php > var_export(PHP\iterable\any([false])); | ||
+ | false | ||
+ | php > var_export(PHP\iterable\any([true])); | ||
+ | true | ||
+ | php > var_export(PHP\iterable\any([0])); | ||
+ | false | ||
+ | php > var_export(PHP\iterable\any([1])); | ||
+ | true | ||
+ | php > var_export(PHP\iterable\any([0], | ||
+ | false | ||
+ | php > var_export(PHP\iterable\any([1], | ||
+ | true | ||
+ | |||
+ | php > var_export(PHP\iterable\all([true, | ||
+ | true | ||
+ | php > var_export(PHP\iterable\all([1, | ||
+ | true | ||
+ | php > var_export(PHP\iterable\all([true, | ||
+ | false | ||
+ | php > var_export(PHP\iterable\all([1, | ||
+ | false | ||
+ | php > var_export(PHP\iterable\all([1, | ||
+ | false | ||
+ | </ | ||
==== Secondary Vote: any()/all() or any_value()/ | ==== Secondary Vote: any()/all() or any_value()/ | ||
Line 103: | Line 159: | ||
Benefits of a shorter name: | Benefits of a shorter name: | ||
- | - Conciseness for the most common use of '' | + | - Conciseness for the most common use case of checking whether a predicate is true for any/all values of an iterable array/ |
- | - Consistency with some other functions such as '' | + | - Consistency with some other functions such as '' |
- Potential to use $flags to extend this to support less common use cases like '' | - Potential to use $flags to extend this to support less common use cases like '' | ||
Benefits of a longer name: | Benefits of a longer name: | ||
- | - Descriptiveness/ | + | - A longer name would be more descriptive and make it easier |
- | - More likely | + | - This makes it likely |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 233: | Line 289: | ||
- | ===== Proposed | + | ===== Vote ===== |
+ | |||
+ | Add '' | ||
+ | |||
+ | Voting | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | \\ The following secondary vote will be used to decide between '' | ||
+ | |||
+ | <doodle title="Names to use: any()/all() or any_value()/ | ||
+ | * any()/ | ||
+ | * any_value()/ | ||
+ | </ | ||
- | Add '' | + | ==== Straw Poll ==== |
- | A secondary vote requiring a simple majority will be used to decide between '' | + | <doodle title=" |
+ | * Too small in scope | ||
+ | * Object | ||
+ | * Prefer | ||
+ | * Confused about the implementation | ||
+ | * Prefer userland solutions | ||
+ | * Other | ||
+ | * Voted for this RFC | ||
+ | </doodle> | ||
===== References ===== | ===== References ===== | ||
Line 255: | Line 335: | ||
* 0.3: Add more quotes | * 0.3: Add more quotes | ||
* 0.4: Change name to '' | * 0.4: Change name to '' | ||
+ | * 0.5: Add straw poll | ||
+ | * 0.6: Add examples of how this works, add in missing return type, clarify treatment of predicate $callback return type |
rfc/any_all_on_iterable.txt · Last modified: 2021/06/13 14:36 by tandre