From 6ab30f3253d800aa88f8d5b5723c0c3857aa7464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lajos=20M=C3=A9sz=C3=A1ros?= Date: Mon, 10 May 2021 12:42:23 +0200 Subject: [PATCH] Added findLast() + updated readme --- README.md | 60 +++++++++++++++++++++++++++++++++---------------------- src/A.php | 11 +++++++--- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 8bc18f2..74263ad 100644 --- a/README.md +++ b/README.md @@ -143,11 +143,9 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::find(function($x) { - return $x["a"] === 10; - }, $data); + $result = A::find(fn($x) => $x["a"] > 3, $data); - // $result = ["a" => 10] + // $result = ["a" => 8] ``` ```php @@ -157,14 +155,12 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::find(function($x) { - return $x["a"] === -4; - }, $data); + $result = A::find(fn($x) => $x["a"] === -4, $data); // $result = null ``` -- **findIndex** - calls the given function on the elements of an array and returns the key for the first match. if there's no match it will return null +- **findLast** - calls the given function on the elements of an array and returns the value for the last match. if there's no match, it will return `null` ```php $data = [ @@ -173,9 +169,33 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::findIndex(function($x) { - return $x["a"] === 10; - }, $data); + $result = A::findLast(fn($x) => $x["a"] > 3, $data); + + // $result = ["a" => 12] + ``` + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::findLast(fn($x) => $x["a"] === -4, $data); + + // $result = null + ``` + +- **findIndex** - calls the given function on the elements of an array and returns the key for the first match. if there's no match it will return `null` + + ```php + $data = [ + ["a" => 8], + ["a" => 10], + ["a" => 12] + ]; + + $result = A::findIndex(fn($x) => $x["a"] === 10, $data); // $result = 1 ``` @@ -187,14 +207,12 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::findIndex(function($x) { - return $x["a"] === -4; - }, $data); + $result = A::findIndex(fn($x) => $x["a"] === -4, $data); // $result = null ``` -- **findLastIndex** - calls the given function on the elements of an array and returns the key for the last match. if there's no match it will return null +- **findLastIndex** - calls the given function on the elements of an array and returns the key for the last match. if there's no match it will return `null` ```php $data = [ @@ -203,9 +221,7 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::findIndex(function($x) { - return $x["a"] > 3; - }, $data); + $result = A::findLastIndex(fn($x) => $x["a"] > 3, $data); // $result = 2 ``` @@ -217,9 +233,7 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ["a" => 12] ]; - $result = A::findIndex(function($x) { - return $x["a"] > 500; - }, $data); + $result = A::findLastIndex(fn($x) => $x["a"] > 500, $data); // $result = null ``` @@ -229,9 +243,7 @@ Plain numeric arrays are handled best via the methods in A, while associative ar ```php $data = [2, 3, 5, 6, 7, 9, 10]; - $result = A::any(function($x) { - return $x % 5 === 0; - }, $data); + $result = A::any(fn($x) => $x % 5 === 0, $data); // $result = true ``` diff --git a/src/A.php b/src/A.php index 57b4d74..1a9080f 100644 --- a/src/A.php +++ b/src/A.php @@ -263,7 +263,7 @@ public static function filter(callable $fn, array $data): array return A::values(array_filter($data, $fn)); } - // A::find(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> ['a' => 10] + // A::find(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> ['a' => 8] public static function find(callable $fn, array $data) { $results = self::filter($fn, $data); @@ -274,6 +274,11 @@ public static function find(callable $fn, array $data) return self::first($results); } + // A::findLast(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> ['a' => 10] + public static function findLast(callable $fn, array $data) { + return self::find($fn, A::reverse($data)); + } + // A::findIndex(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> 1 public static function findIndex(callable $fn, array $data): int { if (self::isEmpty($data)) { @@ -290,9 +295,9 @@ public static function findIndex(callable $fn, array $data): int { } // A::findLastIndex(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> 1 - function findLastIndex(callable $fn, array $data): ?int + public static function findLastIndex(callable $fn, array $data): ?int { - return A::findIndex($fn, A::reverse($data)); + return self::findIndex($fn, A::reverse($data)); } // A::any(x => x.a === 10, [['a' => 8], ['a' => 10]]) -> true