Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3774924
D3221.id8254.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D3221.id8254.diff
View Options
diff --git a/omnitools/src/Collections/WithCollection.php b/omnitools/src/Collections/WithCollection.php
--- a/omnitools/src/Collections/WithCollection.php
+++ b/omnitools/src/Collections/WithCollection.php
@@ -2,6 +2,9 @@
namespace Keruald\OmniTools\Collections;
+use Keruald\OmniTools\Reflection\CallableElement;
+
+use InvalidArgumentException;
use OutOfRangeException;
trait WithCollection {
@@ -24,4 +27,74 @@
};
}
+ ///
+ /// HOF
+ ///
+
+ /**
+ * Determines if at least an element of the collection satisfies a condition.
+ *
+ * The execution of callbacks stop after a callable returned true.
+ *
+ * @param callable $callable A method returning a boolean with key and value
+ * or only value as arguments.
+ *
+ * @return bool True if callback is true for at least one of the elements
+ * @throws \ReflectionException if the callable does not exist.
+ */
+ public function any (callable $callable) : bool {
+ $argc = (new CallableElement($callable))->countArguments();
+ $items = $this->toArray();
+
+ foreach ($items as $key => $value) {
+ $result = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+
+ // PHP standard or extensions functions can sometimes throw
+ // mixed result, for example true or a constant. Any other
+ // result than the boolean true is interpreted as falsy.
+ if ($result === true) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Determines if all elements of the collection satisfies a condition.
+ *
+ * The execution of callbacks stop after a callable returned false.
+ *
+ * @param callable $callable A method returning a boolean with key and value
+ * or only value as arguments.
+ *
+ * @return bool True if callback is true for all the elements
+ * @throws \ReflectionException if the callable does not exist.
+ */
+ public function all (callable $callable) : bool {
+ $argc = (new CallableElement($callable))->countArguments();
+ $items = $this->toArray();
+
+ foreach ($items as $key => $value) {
+ $result = match($argc) {
+ 0 => throw new InvalidArgumentException(self::CB_ZERO_ARG),
+ 1 => $callable($value),
+ default => $callable($key, $value),
+ };
+
+ // PHP standard or extensions functions can sometimes throw
+ // mixed result, for example true or a constant. Any other
+ // result than the boolean true is interpreted as falsy.
+ if ($result !== true) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
}
diff --git a/omnitools/tests/Collections/HashMapTest.php b/omnitools/tests/Collections/HashMapTest.php
--- a/omnitools/tests/Collections/HashMapTest.php
+++ b/omnitools/tests/Collections/HashMapTest.php
@@ -457,6 +457,34 @@
$this->assertEquals($expected, $actual);
}
+ ///
+ /// HOF methods - WithCollection
+ ///
+
+ public function testAny () : void {
+ $author_is_Iain_Banks = fn($author) => $author === "Iain Banks";
+
+ $this->assertTrue($this->map->any($author_is_Iain_Banks));
+ }
+
+ public function testAnyWithAllFalseValues () : void {
+ $author_is_Isaac_Asimov = fn($author) => $author === "Isaac Asimov";
+
+ $this->assertFalse($this->map->any($author_is_Isaac_Asimov));
+ }
+
+ public function testAll () : void {
+ $author_contains_space = fn($author) => str_contains($author, " ");
+
+ $this->assertTrue($this->map->all($author_contains_space));
+ }
+
+ public function testAllWithFalseValue () : void {
+ $author_is_Iain_Banks = fn($author) => $author === "Iain Banks";
+
+ $this->assertFalse($this->map->all($author_is_Iain_Banks));
+ }
+
///
/// ArrayAccess
///
diff --git a/omnitools/tests/Collections/VectorTest.php b/omnitools/tests/Collections/VectorTest.php
--- a/omnitools/tests/Collections/VectorTest.php
+++ b/omnitools/tests/Collections/VectorTest.php
@@ -242,6 +242,30 @@
$this->assertEquals([0, 2, 4], array_keys($actual));
}
+ public function testAny () : void {
+ $is_even = fn(int $item) : bool => $item % 2 === 0;
+
+ $this->assertTrue($this->vector->any($is_even));
+ }
+
+ public function testAnyWithAllFalseValues () : void {
+ $is_zero = fn(int $item) : bool => $item === 0;
+
+ $this->assertFalse($this->vector->any($is_zero));
+ }
+
+ public function testAll () : void {
+ $under_ten = fn(int $item) : bool => $item < 10;
+
+ $this->assertTrue($this->vector->all($under_ten));
+ }
+
+ public function testAllWithFalseValue () : void {
+ $is_even = fn(int $item) : bool => $item % 2 === 0;
+
+ $this->assertFalse($this->vector->all($is_even));
+ }
+
public function testChunk () : void {
$vector = new Vector([1, 2, 3, 4, 5, 6]);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 15:11 (16 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2262692
Default Alt Text
D3221.id8254.diff (5 KB)
Attached To
Mode
D3221: Implement any() and all() for all collections
Attached
Detach File
Event Timeline
Log In to Comment