Functional utils
Determines whether all elements of the array satisfies the predicate.
Futils\all(fn($x) => $x > 100)([101, 102, 103]) // => true
Creates a function that always returns a given value.
Futils\always(true)() // => true
Determines whether any element of the array satisfies the predicate.
Futils\any(fn($x) => $x == 100)([1, 2, 100]) // => true
Function composition (fn -> ... -> f2 -> f1).
Futils\compose(fn($x) => $x + 1, fn($x) => $x * 100)(2) // => 201
Check whether a value is contained in a array.
Futils\contains(1337)([1, 1337, 2]) // => true
Computes the difference of arrays.
Futils\difference([1, 2, 3, 4])([1, 2]) // => [3, 4]
Drops the first n elements off the front of the array.
Futils\drop(2)([1, 2, 3, 4]) // => [3, 4]
Filters elements of an array using a callback function.
Futils\filter(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [1, 2]
Find first element of the array satisfies the predicate.
Futils\find(fn($x) => $x > 10)([1, 2, 11]) // => 11
Flattens nested arrays.
Futils\flatten([1, [2, [3, [4]]]]) // => [1, 2, 3, 4]
Check if exists element with this key in array.
Futils\has("test")(["test" => 1]) // => true
Get head of array.
Futils\head([1, 2, 3]) // => 1
Get first index of value in array.
Futils\indexOf("test")([1, "test", 3]) // => 1
Join array elements with a string.
Futils\join([1, 2, 3])("|") // => "1|2|3"
Get last element of array.
Futils\last([1, 2, 3, 4]) // => 4
Applying function to each element of array.
Futils\map(fn($x) => $x + 1)([1, 2, 3]) // => [2, 3, 4]
Merge two arrays.
Futils\merge([1, 2])([3, 4]) // => [1, 2, 3, 4]
Create partial function.
Futils\partial(fn($x, $y, $z) => $x + $y + $z)(1, 2)(3) // => 6
Equivalent to [(filter f, arr), (reject f, arr)]
Futils\partition(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [[1, 2], [-1, -2]]
Function composition (f1 -> f2 -> ... -> fn).
Futils\pipe(fn($x) => $x + 1, fn($x) => $x * 100)(1) // => 200
Reduce the array to a single value using a callback function.
Futils\reduce(fn($x, $y) => $x + $y)([1, 2, 3]) // => 6
Like filter, but the new array is composed of all the items which fail the function.
Futils\reject(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [-1, -2]
Replaces elements from passed arrays into the first array.
Futils\replace([1, 2, 3])([1 => 3, 2 => 2]) // => [1, 3, 2]
Get tail of array
Futils\tail([1, 2, 3, 4, 5]) // => [2, 3, 4, 5]
Get n first elements from array.
Futils\take(2)([1, 2, 3, 4]) // => [1, 2]
Only when predicate is true then appling function to argument, else return argument.
Futils\when(fn($x) => $x > 100)(fn($x) => $x + 5)(1000) // => 1005
Zips together its two arguments into a array of arrays.
Futils\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 Futils\IdentityMonad(100))
->bind(fn($x, $n) => $x * $n, 2)
->bind("strval")
->extract() // => "200"
MaybeMonad - Encapsulates the type of an undefined value.
(new Futils\MaybeMonad("test"))
->bind(fn() => null)
->bind(fn($x) => $x + 1)
->extract() // => null
ListMonad - Abstracts away the concept of a list of items.
(new Futils\ListMonad([1, new IdentityMonad(2), new MaybeMonad(3), new ListMonad([4])]))
->bind(fn($x) => $x + 100)
->extract() // => [101, 102, 103, [104]]