Skip to content

Commit

Permalink
Added findLast() + updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed May 10, 2021
1 parent 9da1685 commit 6ab30f3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
60 changes: 36 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [
Expand All @@ -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
```
Expand All @@ -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 = [
Expand All @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
```
Expand Down
11 changes: 8 additions & 3 deletions src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand All @@ -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
Expand Down

0 comments on commit 6ab30f3

Please sign in to comment.