kikytokamuro / futils
Functional utils
Requires
- php: >=8.4
Requires (Dev)
- phpunit/phpunit: ^13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Functional utils
Examples:
use Kikytokamuro\Futils\Func; use Kikytokamuro\Futils\Monad;
All
Determines whether all elements of the array satisfies the predicate.
Func::all(fn($x) => $x > 100)([101, 102, 103]) // => true
Always
Creates a function that always returns a given value.
Func::always(true)() // => true
Any
Determines whether any element of the array satisfies the predicate.
Func::any(fn($x) => $x == 100)([1, 2, 100]) // => true
Compose
Function composition (fn -> ... -> f2 -> f1).
Func::compose(fn($x) => $x + 1, fn($x) => $x * 100)(2) // => 201
Contains
Check whether a value is contained in a array.
Func::contains(1337)([1, 1337, 2]) // => true
Chunk
Split an array into chunks of the given size.
Func::chunk(2)([1, 2, 3, 4, 5]) // => [[1, 2], [3, 4], [5]]
Difference
Computes the difference of arrays.
Func::difference([1, 2, 3, 4])([1, 2]) // => [3, 4]
Drop
Drops the first n elements off the front of the array.
Func::drop(2)([1, 2, 3, 4]) // => [3, 4]
Filter
Filters elements of an array using a callback function.
Func::filter(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [1, 2]
Find
Find first element of the array satisfies the predicate.
Func::find(fn($x) => $x > 10)([1, 2, 11]) // => 11
Flatten
Flattens nested arrays.
Func::flatten([1, [2, [3, [4]]]]) // => [1, 2, 3, 4]
Has
Check if exists element with this key in array.
Func::has("test")(["test" => 1]) // => true
Head
Get head of array.
Func::head([1, 2, 3]) // => 1
IndexOf
Get first index of value in array.
Func::indexOf("test")([1, "test", 3]) // => 1
Intersection
Computes the intersection of arrays.
Func::intersection([1, 2, 3, 4])([2, 3, 5]) // => [2, 3]
Join
Join array elements with a string.
Func::join([1, 2, 3])("|") // => "1|2|3"
Last
Get last element of array.
Func::last([1, 2, 3, 4]) // => 4
Map
Applying function to each element of array.
Func::map(fn($x) => $x + 1)([1, 2, 3]) // => [2, 3, 4]
Memoize
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 result
Merge
Merge two arrays.
Func::merge([1, 2])([3, 4]) // => [1, 2, 3, 4]
Omit
Returns an array without the specified keys.
Func::omit(['b'])(['a' => 1, 'b' => 2, 'c' => 3]) // => ['a' => 1, 'c' => 3]
Partial
Create partial function.
Func::partial(fn($x, $y, $z) => $x + $y + $z)(1, 2)(3) // => 6
Pick
Returns an array with only the specified keys.
Func::pick(['a', 'c'])(['a' => 1, 'b' => 2, 'c' => 3]) // => ['a' => 1, 'c' => 3]
Partition
Equivalent to [(filter f, arr), (reject f, arr)]
Func::partition(fn($x) => $x > 0)([-1, -2, 1, 2]) // => [[1, 2], [-1, -2]]
Pipe
Function composition (f1 -> f2 -> ... -> fn).
Func::pipe(fn($x) => $x + 1, fn($x) => $x * 100)(1) // => 200
Reduce
Reduce the array to a single value using a callback function.
Func::reduce(fn($x, $y) => $x + $y)([1, 2, 3]) // => 6
Reject
Like 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]
Replace
Replaces elements from passed arrays into the first array.
Func::replace([1, 2, 3])([1 => 3, 2 => 2]) // => [1, 3, 2]
Tail
Get tail of array
Func::tail([1, 2, 3, 4, 5]) // => [2, 3, 4, 5]
Once
Creates a function that is restricted to invoking the given function once.
$init = Func::once(fn() => doSomething()); $init(); // => executes $init(); // => returns cached result, no execution
Tap
Invokes the interceptor and returns the value.
Func::tap(fn($x) => log($x))($value) // => returns $value, calls log($x) as side effect
Take
Get n first elements from array.
Func::take(2)([1, 2, 3, 4]) // => [1, 2]
Union
Computes the union of arrays (with deduplication).
Func::union([1, 2, 3])([3, 4, 5]) // => [1, 2, 3, 4, 5]
When
Only when predicate is true then appling function to argument, else return argument.
Func::when(fn($x) => $x > 100)(fn($x) => $x + 5)(1000) // => 1005
Zip
Zips together its two arguments into a array of arrays.
Func::zip([1, 2, 3])([4, 5, 6]) // => [[1, 4], [2, 5], [3, 6]]
Monads
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() // => null
ListMonad - 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"