Functional utils
use Kikytokamuro\Futils\Func;
use Kikytokamuro\Futils\Monad;Determines whether all elements of the array satisfies the predicate.
Func::all(fn($x) => $x > 100)([101, 102, 103]) // => trueCreates a function that always returns a given value.
Func::always(true)() // => trueDetermines whether any element of the array satisfies the predicate.
Func::any(fn($x) => $x == 100)([1, 2, 100]) // => trueFunction composition (fn -> ... -> f2 -> f1).
Func::compose(fn($x) => $x + 1, fn($x) => $x * 100)(2) // => 201Check whether a value is contained in a array.
Func::contains(1337)([1, 1337, 2]) // => trueSplit an array into chunks of the given size.
Func::chunk(2)([1, 2, 3, 4, 5]) // => [[1, 2], [3, 4], [5]]Computes the difference of arrays.
Func::difference([1, 2, 3, 4])([1, 2]) // => [3, 4]Drops the first n elements off the front of the array.
Func::drop(2)([1, 2, 3, 4]) // => [3, 4]Filters elements of an array using a callback function.
Func::filter(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [1, 2]Find first element of the array satisfies the predicate.
Func::find(fn($x) => $x > 10)([1, 2, 11]) // => 11Flattens nested arrays.
Func::flatten([1, [2, [3, [4]]]]) // => [1, 2, 3, 4]Check if exists element with this key in array.
Func::has("test")(["test" => 1]) // => trueGet head of array.
Func::head([1, 2, 3]) // => 1Get first index of value in array.
Func::indexOf("test")([1, "test", 3]) // => 1Computes the intersection of arrays.
Func::intersection([1, 2, 3, 4])([2, 3, 5]) // => [2, 3]Join array elements with a string.
Func::join([1, 2, 3])("|") // => "1|2|3"Get last element of array.
Func::last([1, 2, 3, 4]) // => 4Applying function to each element of array.
Func::map(fn($x) => $x + 1)([1, 2, 3]) // => [2, 3, 4]Creates a memoized version of a function that caches results by arguments.
$fn = Func::memoize(fn($x) => $x * 1000);
$fn(5); // => calculates and caches
$fn(5); // => returns cached resultMerge two arrays.
Func::merge([1, 2])([3, 4]) // => [1, 2, 3, 4]Returns an array without the specified keys.
Func::omit(['b'])(['a' => 1, 'b' => 2, 'c' => 3]) // => ['a' => 1, 'c' => 3]Create partial function.
Func::partial(fn($x, $y, $z) => $x + $y + $z)(1, 2)(3) // => 6Returns an array with only the specified keys.
Func::pick(['a', 'c'])(['a' => 1, 'b' => 2, 'c' => 3]) // => ['a' => 1, 'c' => 3]Equivalent to [(filter f, arr), (reject f, arr)]
Func::partition(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [[1, 2], [-1, -2]]Function composition (f1 -> f2 -> ... -> fn).
Func::pipe(fn($x) => $x + 1, fn($x) => $x * 100)(1) // => 200Reduce the array to a single value using a callback function.
Func::reduce(fn($x, $y) => $x + $y)([1, 2, 3]) // => 6Like filter, but the new array is composed of all the items which fail the function.
Func::reject(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [-1, -2]Replaces elements from passed arrays into the first array.
Func::replace([1, 2, 3])([1 => 3, 2 => 2]) // => [1, 3, 2]Get tail of array
Func::tail([1, 2, 3, 4, 5]) // => [2, 3, 4, 5]Creates a function that is restricted to invoking the given function once.
$init = Func::once(fn() => doSomething());
$init(); // => executes
$init(); // => returns cached result, no executionInvokes the interceptor and returns the value.
Func::tap(fn($x) => log($x))($value) // => returns $value, calls log($x) as side effectGet n first elements from array.
Func::take(2)([1, 2, 3, 4]) // => [1, 2]Computes the union of arrays (with deduplication).
Func::union([1, 2, 3])([3, 4, 5]) // => [1, 2, 3, 4, 5]Only when predicate is true then appling function to argument, else return argument.
Func::when(fn($x) => $x > 100)(fn($x) => $x + 5)(1000) // => 1005Zips together its two arguments into a array of arrays.
Func::zip([1, 2, 3])([4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]IdentityMonad - Just annotates plain values and functions to satisfy the monad laws.
(new Monad\IdentityMonad(100))
->bind(fn($x, $n) => $x * $n, 2)
->bind("strval")
->extract() // => "200"MaybeMonad - Encapsulates the type of an undefined value.
(new Monad\MaybeMonad("test"))
->bind(fn() => null)
->bind(fn($x) => $x + 1)
->extract() // => nullListMonad - Abstracts away the concept of a list of items.
(new Monad\ListMonad([1, new Monad\IdentityMonad(2), new Monad\MaybeMonad(3), new Monad\ListMonad([4])]))
->bind(fn($x) => $x + 100)
->extract() // => [101, 102, 103, [104]]EitherMonad - Represents a value of one of two possible types (Left or Right). Right is the "success" case, Left is the "failure" case.
Monad\EitherMonad::right(10)
->bind(fn($x) => $x * 2)
->bind(fn($x) => $x > 15 ? Monad\EitherMonad::right($x) : Monad\EitherMonad::left("Too small"))
->fold(
fn($err) => "Error: $err",
fn($val) => "Success: $val"
); // => "Success: 20"
Monad\EitherMonad::left("error")
->bind(fn($x) => $x * 2)
->extract(); // => "error"TryMonad - Represents a computation that may either result in an exception (Failure) or return a successfully computed value (Success).
Monad\TryMonad::of(fn($json) => json_decode($json, true), '{"a":1}')
->bind(fn($data) => $data['a'])
->recover(fn($e) => null)
->extract(); // => 1
Monad\TryMonad::of(fn() => throw new \RuntimeException("error"))
->bind(fn($x) => $x * 2)
->getOrElse("default"); // => "default"