Skip to content

Commit 34262dd

Browse files
committed
feat(support): add partition to array functions
1 parent 0c553d4 commit 34262dd

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Tempest/Support/src/Arr/ManipulatesArray.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,30 @@ public function slice(int $offset, ?int $length = null): static
724724
return $this->createOrModify(namespace\slice($this->value, $offset, $length));
725725
}
726726

727+
/**
728+
* Returns a pair containing lists for which the given predicate returned `true` and `false`, respectively.
729+
*
730+
* @param (Closure(T): bool) $predicate
731+
*
732+
* @return static<array<T>, array<T>>
733+
*/
734+
function partition(Closure $predicate): static
735+
{
736+
$success = [];
737+
$failure = [];
738+
739+
foreach ($this->value as $value) {
740+
if ($predicate($value)) {
741+
$success[] = $value;
742+
continue;
743+
}
744+
745+
$failure[] = $value;
746+
}
747+
748+
return $this->createOrModify([$success, $failure]);
749+
}
750+
727751
/**
728752
* Executes callback with the given `$value` and returns the same `$value`.
729753
*

src/Tempest/Support/src/Arr/functions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,33 @@ function slice(iterable $array, int $offset, ?int $length = null): array
11451145
return array_slice($array, $offset, $length);
11461146
}
11471147

1148+
/**
1149+
* Returns a pair containing lists for which the given predicate returned `true` and `false`, respectively.
1150+
*
1151+
* @template T
1152+
*
1153+
* @param iterable<T> $iterable
1154+
* @param (Closure(T): bool) $predicate
1155+
*
1156+
* @return array{0: array<T>, 1: array<T>}
1157+
*/
1158+
function partition(iterable $iterable, Closure $predicate): array
1159+
{
1160+
$success = [];
1161+
$failure = [];
1162+
1163+
foreach ($iterable as $value) {
1164+
if ($predicate($value)) {
1165+
$success[] = $value;
1166+
continue;
1167+
}
1168+
1169+
$failure[] = $value;
1170+
}
1171+
1172+
return [$success, $failure];
1173+
}
1174+
11481175
/**
11491176
* Wraps the specified `$input` into an array. If the `$input` is already an array, it is returned.
11501177
* As opposed to {@see \Tempest\Support\Arr\to_array}, this function does not convert {@see Traversable} and {@see Countable} instances to arrays.

src/Tempest/Support/tests/Arr/ManipulatesArrayTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,4 +1786,9 @@ public function test_at(array $input, int $index, mixed $expected, mixed $defaul
17861786
$this->assertSame($expected, arr($input)->at($index, $default));
17871787
$this->assertSame($expected, arr($input)->nth($index, $default));
17881788
}
1789+
1790+
public function test_partition(): void
1791+
{
1792+
$this->assertSame([[true, true], [false]], arr([true, true, false])->partition(fn (bool $value) => $value === true)->toArray());
1793+
}
17891794
}

0 commit comments

Comments
 (0)