Contents
-
Compose functions. Output of one function will be passed as input to another function.
$aToB = fn(int $a): bool => true; $bToC = fn(bool $b): string => (string) $b; $cTod = fn(string $c): float => (float) $c; /** @var callable(int): float $result */ $result = compose($aToB, $bToC, $cTod);
-
Partial application from first function argument. Pass callback and N callback arguments. These N arguments will be locked at corresponding places (callback parameters) from left-side and new callback will be returned with fewer arguments.
$callback = fn(int $a, string $b, bool $c): bool => true; /** @var callable(bool): bool $result */ $result = partial($callback, 1, "string"); /** @var callable(bool): bool $result */ $result = partialLeft($callback, 1, "string");
-
Partial application from last function argument Pass callback and N callback arguments. These N arguments will be locked at corresponding places (callback parameters) from right-side and new callback will be returned with fewer arguments.
$callback = fn(int $a, string $b, bool $c): bool => true; /** @var callable(int): bool $result */ $result = partialRight($callback, true, "string");
-
Returns true if there is collection element which satisfies the condition and false otherwise
<?php use function Fp\Collection\exists; exists([1, 2, 3], fn(int $value) => $value === 2); // true
-
Find element by its key
O(1) for arrays and classes which implement ArrayAccess. O(N) for other cases
<?php use function Fp\Collection\at; /** @var Option<Foo|int> $result */ $result = at([new Foo(), 2, 3], 1);
-
Returns true if every collection element satisfies the condition and false otherwise
<?php use function Fp\Collection\every; every([1, 2], fn(int $v) => $v === 1); // false
-
Filter collection by condition. Do not preserve keys by default
<?php use function Fp\Collection\filter; filter([1, 2], fn(int $v): bool => $v === 2); // [2]
-
Filter not null elements. Do not preserve keys by default
<?php use function Fp\Collection\filterNotNull; filterNotNull([1, null, 2]); // [1, 2]
-
Find first element which satisfies the condition
<?php use function Fp\Collection\first; /** @var Option<int> $result */ $result = first([1, 2], fn(int $v): bool => $v === 2);
-
Flat map Consists of map and flatten operations
<?php use function Fp\Collection\flatMap; /** * 1) map [1, 4] to [[0, 1, 2], [3, 4, 5]] * 2) flatten [[0, 1, 2], [3, 4, 5]] to [0, 1, 2, 3, 4, 5] */ flatMap([1, 4], fn(int $x) => [$x - 1, $x, $x + 1]); // [0, 1, 2, 3, 4, 5]
-
Group collection elements by key returned by function
<?php use function Fp\Collection\groupBy; $result = groupBy([1, 2, 3], fn(int $v): int => $v); // [1 => [1], 2 => [2], 3 => [3]]
-
Returns collection first element
<?php use function Fp\Collection\head; $result = head([1, 2, 3]); // Some(1) $result = head([]); // None
-
Returns list of collection keys
<?php use function Fp\Collection\keys; keys(['a' => 1, 'b' => 2]); // ['a', 'b']
-
Returns last collection element and None if there is no last element
<?php use function Fp\Collection\last; /** @var Option<int> $result */ $result = last([1, 2, 3]);
-
Produces a new array of elements by mapping each element in collection through a transformation function (callback).
<?php use function Fp\Collection\map; map([1, 2, 3], fn(int $v) => (string) $v); // ['1', '2', '3']
-
Divide collection by given classes
<?php use Tests\Mock\Foo; use Tests\Mock\Bar; use function Fp\Collection\partitionT; // inferred as array{list<Foo>, list<Bar>, list<Foo|Bar>} $result = partitionT( [new Foo(), new Bar()], fn($i) => $i instanceof Foo, fn($i) => $i instanceof Bar, );
-
Pop last collection element and return tuple containing this element and other collection elements. If there is no last element then returns None
<?php use function Fp\Collection\pop; [$head, $tail] = pop([1, 2, 3])->get(); // [3, [1, 2]]
<?php use Fp\Functional\Option\Option; use function Fp\Collection\pop; Option::do(function () use ($collection) { [$head, $tail] = yield pop($collection); return doSomethingWithHeadAndTail($head, $tail); })
-
Fold multiple elements into one. Returns None for empty collection
<?php use function Fp\Collection\fold; $result = fold('', ['a', 'b', 'c'])(fn($acc, string $cur) => $acc . $cur); // 'abc'
-
Produces a new array of elements by assigning the values to keys generated by a transformation function (callback).
<?php use function Fp\Collection\reindex; reindex([1, 'a' => 2], fn (int $value) => $value); // [1 => 1, 2 => 2]
-
Copy collection in reversed order
<?php use function Fp\Collection\reverse; reverse([1, 2, 3]); // [3, 2, 1]
-
Returns second collection element. None if there is no second collection element
<?php use function Fp\Collection\second; second([1, 2, 3])->get(); // 2
-
Shift first collection element and return tuple containing this element and other collection elements. If there is no first element then returns None
<?php use function Fp\Collection\shift; [$head, $tail] = shift([1, 2, 3])->get(); // [1, [2, 3]]
<?php use Fp\Functional\Option\Option; use function Fp\Collection\shift; Option::do(function () use ($collection) { [$head, $tail] = yield shift($collection); return doSomethingWithHeadAndTail($head, $tail); })
-
Returns every collection element except first
<?php use function Fp\Collection\tail; tail([1, 2, 3]); // [2, 3]
-
Returns an iterable collection formed from this iterable collection and another iterable collection by combining corresponding elements in pairs.
If one of the two collections is longer than the other, its remaining elements are ignored.
<?php use function Fp\Collection\zip; zip([1, 2, 3], ['a', 'b']); // [[1, 'a'], [2, 'b']]
-
Copy collection as array
<?php declare(strict_types=1); use function Fp\Cast\asArray; /** @psalm-return iterable<string, int> */ function getCollection(): array { return []; } /** @var array<string, int> $result */ $result = asArray(getCollection());
-
Try cast boolean like value. Returns None if cast is not possible
<?php declare(strict_types=1); use function Fp\Cast\asBool; /** @var Option<bool> $result */ $result = asBool('yes');
-
Try cast float like value. Returns None if cast is not possible
<?php declare(strict_types=1); use function Fp\Cast\asFloat; /** @var Option<float> $result */ $result = asFloat('1.1');
-
Try cast integer like value. Returns None if cast is not possible
<?php declare(strict_types=1); use function Fp\Cast\asInt; /** @var Option<int> $result */ $result = asInt(1);
-
Copy one or multiple collections as list
<?php declare(strict_types=1); use function Fp\Cast\asList; $result = asList([1], ['prop' => 2], [3, 4]); // [1, 2, 3, 4]
-
Try cast collection to new non-empty-array. Returns None if there is no first collection element
<?php declare(strict_types=1); use function Fp\Cast\asNonEmptyArray; /** @psalm-return iterable<string, int> */ function getCollection(): array { return []; } /** @var Option<non-empty-array<string, int>> $result */ $result = asNonEmptyArray(getCollection());
-
Try cast collection to new non-empty-list. Returns None if there is no first collection element
<?php declare(strict_types=1); use function Fp\Cast\asNonEmptyList; /** @psalm-return iterable<string, int> */ function getCollection(): array { return []; } /** @var Option<non-empty-list<int>> $result */ $result = asNonEmptyList(getCollection());
-
Prove that given value is of array type
<?php declare(strict_types=1); use function Fp\Evidence\proveArray; function getMixed(): mixed { return []; } // inferred as Option<array<array-key, mixed>> $result = proveArray(getMixed());
Type params from any iterable type will be preserved:
<?php declare(strict_types=1); use function Fp\Evidence\proveArray; /** @return iterable<string, int> */ function getCollection(): iterable { return []; } // inferred as Option<array<string, int>> $result = proveArray(getCollection());
Key and value type can be proved separately:
<?php declare(strict_types=1); use function Fp\Evidence\proveArray; use function Fp\Evidence\proveString; use function Fp\Evidence\proveInt; /** @return iterable<mixed, mixed> */ function getCollection(): iterable { return []; } // inferred as Option<array<string, int>> $result = proveArray(getCollection(), proveString(...), proveInt(...));
-
Prove that given collection is of non-empty-array type
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyArray; /** @return iterable<string, int> */ function getCollection(): array { return []; } // Inferred as Option<non-empty-array<string, int>> $result = proveNonEmptyArray(getCollection());
Type params from any iterable type will be preserved:
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyArray; /** @return iterable<string, int> */ function getCollection(): iterable { return []; } // inferred as Option<non-empty-array<string, int>> $result = proveNonEmptyArray(getCollection());
Key and value type can be proved separately:
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyArray; use function Fp\Evidence\proveString; use function Fp\Evidence\proveInt; /** @return iterable<mixed, mixed> */ function getCollection(): iterable { return []; } // inferred as Option<non-empty-array<string, int>> $result = proveNonEmptyArray(getCollection(), proveString(...), proveInt(...));
-
Prove that given value is of list type
<?php declare(strict_types=1); use function Fp\Evidence\proveList; function getMixed(): mixed { return []; } // Inferred as Option<list<mixed>> $result = proveList(getMixed());
Type params from any iterable type will be preserved:
<?php declare(strict_types=1); use function Fp\Evidence\proveList; /** @return iterable<int, string> */ function getCollection(): iterable { return []; } // inferred as Option<list<string>> $result = proveList(getCollection());
Value type can be proved separately:
<?php declare(strict_types=1); use function Fp\Evidence\proveList; use function Fp\Evidence\proveInt; function getMixed(): mixed { return []; } // Inferred as Option<list<int>> $result = proveList(getMixed(), proveInt(...));
-
Prove that given value is of non-empty-list type
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyList; function getMixed(): mixed { return []; } // Inferred as Option<non-empty-list<mixed>> $result = proveNonEmptyList(getMixed());
Type params from any iterable type will be preserved:
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyList; /** @return iterable<int, string> */ function getCollection(): iterable { return []; } // Inferred as Option<non-empty-list<string>> $result = proveNonEmptyList(getCollection());
Value type can be proved separately:
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyList; use function Fp\Evidence\proveInt; function getMixed(): mixed { return []; } // Inferred as Option<non-empty-list<int>> $result = proveNonEmptyList(getMixed(), proveInt(...));
-
Prove that subject is of boolean type
<?php declare(strict_types=1); use function Fp\Evidence\proveBool; // Inferred as Option<bool> $result = proveBool($subject);
-
Prove that subject is of boolean type, and it's value is true
<?php declare(strict_types=1); use function Fp\Evidence\proveTrue; // Inferred as Option<true> $result = proveTrue($subject);
-
Prove that subject is of boolean type, and it's value is false
<?php declare(strict_types=1); use function Fp\Evidence\proveFalse; // Inferred as Option<false> $result = proveFalse($subject);
-
Prove that subject is of string type
<?php declare(strict_types=1); use function Fp\Evidence\proveString; // Inferred as Option<string> $result = proveString($subject);
-
Prove that subject is of given class
<?php declare(strict_types=1); use function Fp\Evidence\proveNonEmptyString; $possiblyEmptyString = ''; // Inferred as Option<non-empty-string> $result = proveNonEmptyString($possiblyEmptyString);
-
Prove that subject is of callable-string type
<?php declare(strict_types=1); use function Fp\Evidence\proveCallableString; // Inferred as Option<callable-string> $result = proveCallableString($subject);
-
Prove that subject is of class-string type
<?php declare(strict_types=1); use function Fp\Evidence\proveClassString; // Inferred as Option<class-string> $result = proveClassString($subject);
-
Prove that subject is subtype of given class-string
<?php declare(strict_types=1); use function Fp\Evidence\proveClassStringOf; // Inferred as Option<class-string<Collection>> $result = proveClassStringOf(ArrayList::class, Collection::class);
-
Prove that subject is of float type
<?php declare(strict_types=1); use function Fp\Evidence\proveFloat; // Inferred as Option<float> $result = proveFloat($subject);
-
Prove that subject is of int type
<?php declare(strict_types=1); use function Fp\Evidence\proveInt; // Inferred as Option<int> $result = proveInt($subject);
-
Prove that subject is of given class
<?php declare(strict_types=1); use function Fp\Evidence\proveOf; // Inferred as Option<Foo> $result = proveOf(new Bar(), Foo::class);