A PHP functional utility library, port of Javascript Lodash/Fp and Ramda libraries to PHP.
All functions are side-effect free and automatically curried, data is immutable.
The iterable collection parameter is usually supplied last to make currying convenient.
Lazy / delayed evaluation is supported in functional pipelines.
Go to https://idlephp.tech for more details, documentation and examples.
PHP 7.4 or higher
composer require miroshnikov/idles
Note
Idles is currently under active development. Roadmap is to add all methods from Lodash and Ramda libraries and some functional tools.
concat(?iterable $array, $value): iterableConcatinates $array with additional iterables/values
count(callable $predicate, ?iterable $collection): intCounts the number of items in $collection matching the $predicate
countBy(callable $iteratee, ?iterable $collection): arrayReturns an array: [$iteratee($value) => number of times the $iteratee($value) was found in $collection]
drop(int $n, ?iterable $collection): iterableSkips the first $n elemens and returns the rest of the iterable
dropRight(int $n, ?iterable $collection): iterableSkips the last $n elements
findIndex(callable $predicate, ?iterable $collection): intLike find but returns the index of the first element predicate returns truthy for, -1 if not found
findLastIndex(callable $predicate, ?iterable $collection): intLike find but returns the index of the last element predicate returns truthy for, -1 if not found
flatten(?iterable $collection): iterableFlattens iterable a single level deep.
flattenDeep(?iterable $collection): iterableRecursively flattens iterable.
flattenDepth(int $depth, ?iterable $collection): iterableRecursively flatten array up to depth times.
fromPairs(?iterable $collection): arrayCreates a new record from a list key-value pairs. The inverse of toPairs.
head(?iterable $collecton)Gets the first element of iterable
indexOf($value, ?iterable $collection): intReturns the index of the first occurrence of $value in $collection, else -1.
intersection(?iterable $record1, ?iterable $record2): arrayReturns unique values that are included in both records
intersectionBy(callable $iteratee, ?iterable $record1, ?iterable $record2): arrayLike intersection but invokes $iteratee for each element before comparison.
intersectionWith(callable $comparator, ?iterable $record1, ?iterable $record2): arrayLike intersection but invokes $comparator to compare elements.
join(string $separator, ?iterable $collection): stringJoins iterable elements separated by $separator
last(?iterable $collecton)Gets the last element of iterable
nth(int $offset, ?iterable $collection)Returns the $offset element. If $offset is negative the element at index length + $offset is returned.
remove(int $start, int $count, ?iterable $iterable): arrayRemoves items from $iterable starting at $start and containing $count elements.
slice(int $start, int ?$end, ?iterable $collection): iterableRetruns a slice of $iterable from $start up to, but not including, $end.
take(int $n, ?iterable $collection): iterableTakes n first elements from iterable
takeRight(int $n, ?iterable $collection): arrayReturns a slice of iterable with n elements taken from the end.
uniq(?iterable $collection): arrayRemoves duplicates using ===
uniqBy(callable $iteratee, ?iterable $collection): arrayLike uniq but apply $iteratee fist
uniqWith(callable $predicate, ?iterable $collection): arrayLike uniq but uses $predicate to compare elements
without(array $values, ?iterable $collection): iterableReturns $iterable without $values
zip(iterable $a, iterable $b): iterableCreates an iterable of grouped elements, the first of which contains the first elements of the given iterables, the second of which contains the second elements, and so on.
zipWith(callable $iteratee, iterable $a, iterable $b): iterableLike zip except that it accepts $iteratee to specify how grouped values should be combined.
all(?callable $predicate, ?iterable $collection): boolChecks if $predicate returns truthy for all elements of $collection. Stop once it returns falsey
any(callable $predicate, ?iterable $collection): boolChecks if $predicate returns truthy for any element of $collection. Stops on first found.
each(callable $iteratee, ?iterable $collection): iterableIterates over elements of $collection. Iteratee may exit iteration early by returning false.
filter(callable $predicate, ?iterable $collection): iterableReturns elements $predicate returns truthy for.
find(?callable $predicate, ?iterable $collection)Returns the first element $predicate returns truthy for.
flatMap(callable $iteratee, ?iterable $collection): iterableMaps then flatten
flatMapDeep(callable $iteratee, ?iterable $collection): iterableLike flatMap but recursively flattens the results.
flatMapDepth(callable $iteratee, int $depth, ?iterable $collection): iterableLike flatMap but flattens the mapped results up to $depth times
groupBy(callable $iteratee, ?iterable $collection): arrayCreates an array composed of keys generated from running each value through $iteratee.
includes($value, ?iterable $collection): boolChecks if $value is in $collection.
indexBy(callable $iteratee, ?iterable $collection): iterableCreates a record composed of keys generated from the results of running each element of $collection through $iteratee.
map(callable $iteratee, ?iterable $collection)Run each element in $collection through $iteratee.
orderBy(array $iteratees, array $orders, ?iterable $collection)Like sortBy but allows specifying the sort orders
partition(callable $predicate, ?iterable $collection): arraySplit $collection into two groups, the first of which contains elements $predicate returns truthy for, the second of which contains elements $predicate returns falsey for.
reduce(callable $iteratee, $accumulator, ?iterable $collection)Reduces $collection to a value which is the accumulated result of running each element in collection through $iteratee
resolve(array $resolvers, array $record): arrayAdds new properties to $record using $resolvers.
sort(array $comparator, ?iterable $collection): arraySorts $collection using $comparator comparison ($a <=> $b) function
sortBy(array $comparators, ?iterable $collection): arraySorts $collection in ascending order according to $comparators.
sortWith(array $comparators, ?iterable $collection): arraySorts a $collection according to an array of comparison ($a <=> $b) functions
values(?iterable $collection): iterableReturns an indexed iterable of values in $collection.
always($value)Returns a function that always returns the given value.
applyTo($value, callable $interceptor)Returns $interceptor($value).
ary(int $n, callable $fn): callableCreates a function that invokes $fn, with up to $n arguments, ignoring any additional arguments.
ascend(callable $func, $a, $b): callableMakes an ascending comparator function out of a function that returns a value that can be compared with <=>
attempt(callable $fn)Calls $fn, returning either the result or the caught exception.
compose(callable ...$funcs): callableLike pipe but invokes the functions from right to left.
curry(callable $f): callableIdles_ const may be used as a placeholder.
curryRight(callable $f): callableLike curry but arguments are prepended.
descend(callable $func, $a, $b): callableMakes an descending comparator function out of a function that returns a value that can be compared with <=>
flip(callable $fn): callableReturns a new curried function with the first two arguments reversed
juxt(array $funcs): callableApplies a list of functions to a list of values.
memoize(callable $func): callableCreates a function that memoizes the result of $func. $resolver returns map cache key, args[0] by default.
negate(callable $predicate): callableCreates a function that negates the result of the $predicate function.
once(callable $fn): callable$fn is only called once, the first value is returned in subsequent invocations.
partial(callable $fn, array $partials): callableCreates a function that invokes $fn with $partials prepended to the arguments. Idles_ const may be used as a placeholder.
partialRight(callable $fn, array $partials): callableLike partial but $partials are appended.
pipe(callable ...$funcs): callableLeft-to-right function composition. The first argument may have any arity; the remaining arguments must be unary.
tap(callable $interceptor, $value)Calls $interceptor($value) then returns the original $value
times(callable $iteratee, int $n): arrayCalls the iteratee $n times, returning an array of the results of each invocation.
tryCatch(callable $tryer, callable $catcher, $value)Calls $tryer, if it throws, calls $catcher
unary(callable $fn): callableary(1, $fn)
allPass(array $predicates): callableReturns a function that checks if its arguments pass all $predicates.
anyPass(array $predicates): callableReturns a function that checks if its arguments pass any of the $predicates.
both(callable $func1, callable $func2): callableResulting function returns $func1(...$args) if it is falsy or $func2(...$args) otherwise, short-circuited
cond(array $pairs): callableIterates over $pairs and invokes the corresponding function of the first predicate to return truthy.
defaultTo($default)($value)Returns $value ?? $default
either(callable $func1, callable $func2): callableResulting function returns $func1(...$args) if it is truthy or $func2(...$args) otherwise, short-circuited.
ifElse(callable $predicate, callable $onTrue, callable $onFalse): callableResulting function returns $onTrue(...$args) if $predicate(...$args) is truthy or $onFalse(...$args) otherwise.
not($a): boolreturns !$a
unless(callable $predicate, callable $whenFalse, mixed $value)Returns $predicate($value) ? $value : $whenFalse($value)
when(callable $predicate, callable $whenTrue, mixed $value)Returns $predicate($value) ? $whenTrue($value) : $value
add(int|float $a, int|float $b): int|float$a + $b
dec(int $number): intReturns $number - 1
divide(int|float $a, int|float $b): int|float$a / $b
gt($a, $b): bool$a > $b
gte($a, $b): bool$a >= $b
inc(int $number): intReturns $number + 1
lt($a, $b): bool$a < $b
lte($a, $b): bool$a <= $b
modulo(int|float $a, int|float $b): int$a % $b
multiply(int|float $a, int|float $b): int|float$a * $b
round(int $precision, int|float $number): floatRounds $numberto specified $precision
subtract(int|float $a, int|float $b): int|float$a - $b
sum(?iterable $collection): int|floatSums elements in iterable
sumBy(?callable $iteratee, ?iterable $collection): int|floatLike sum but $iteratee is invoked for each element in iterable to generate the value to be summed.
assignDeep(array $iterables): arrayMerges properties recursively, numeric keys are overwritten.
defaults(?iterable $record1, ?iterable $record2): arrayMerges properties from right to left, numeric keys are overwritten.
evolve(array $transformations, ?iterable $record): arrayCreates a new record by recursively calling transformation functions with $record properties.
extend(?iterable $source1, ?iterable $source2): arrayMerges properties, numeric keys are overwritten.
has(string|int $key, ?iterable $record): boolChecks if $record has $key
hasPath(string|int|array $path, ?iterable $record): boolChecks if $path exists in $record
invert(?iterable $collection): arrayReplaces keys with values. Duplicate keys are overwritten.
keys(?iterable $record): iterableReturns an indexed iterable of keys in $record.
merge(?iterable $source1, ?iterable $source2): arrayMerges properties, numeric keys are appended.
mergeDeep(array $iterables): arrayMerges properties recursively, numeric keys are appended.
mergeLeft(?iterable $left, ?iterable $right): arraycalls merge($right, $left)
mergeWith(callable $customizer, ?iterable $left, ?iterable $right): arrayLike merge but if a key exists in both records, $customizer is called to the values associated with the key
modifyPath(array|string|int $path, callable $updater, ?iterable $record)Creates new record by applying an $updater function to the value at the given $path.
objOf(string $key, $value): arrayCreates an array containing a single key => value pair.
omit(array $keys, ?iterable $collection): iterableThe opposite of pick. Returns record without $keys.
omitBy(callable $predicate, ?iterable $record): iterableThe opposite of pickBy. Returns properties of $record that $predicate returns falsey for.
path(array|string $path, ?iterable $collection)Retrieve the value at a given path.
paths(array $paths, ?iterable $collection): arrayKeys in, values out. Order is preserved.
pick(array $keys, ?iterable $collection): iterableReturns record containing only $keys
pickBy(callable $predicate, ?iterable $record): iterableReturns record containing only keys $predicate returns truthy for.
pluck(string|int $key, ?iterable $collection)Returns a new array by plucking the same named property off all records in the array supplied.
prop(string|int $key, ?iterable $record)Return the specified property.
propEq(string|int $key, $value, ?iterable $record): boolReturns $record[$key] == $value
setPath($path, $value, ?iterable $record)Return copy $record with $path set with $value
toPairs(?iterable $record): iterableConverts a record into an array of [$key, $value]
where(array $spec, ?iterable $record): boolChecks if $record satisfies the spec by invoking the $spec properties with the corresponding properties of $record.
whereAny(array $spec, ?iterable $record): boolChecks if $record satisfies the spec by invoking the $spec properties with the corresponding properties of $record. Returns true if at least one of the predicates returns true.
whereEq(array $spec, ?iterable $test): boolCheck if the $test satisfies the $spec
escape(string $s): stringConverts the characters "&", "<", ">", '"', and "'" to their corresponding HTML entities.
escapeRegExp(string $regexp): stringEscapes regular expression
split(string $separator, string $s): arraySplits string by $separator.
startsWith(string $target, string $s): boolIf string starts with $target.
toLower(string $s): stringConverts string to lower case
toUpper(string $s): stringConverts string to upper case
words(string $pattern, string $string): arraySplits string into an array of its words.
collect(?iterable $iterable): arrayCollects any iterable into array
eq($a, $b): bool$a == $b
equals($a, $b): bool$a === $b
F(...$args): boolAlways returns false
identity($value)Returns the first argument it receives.
iterate(callable $f, $value): iterableReturns a generator of $value, $f($value), $f($f($value)) etc.
just($value): OptionalReturns an Optional with the specified non-null value
nothing(): OptionalReturns an empty Optional
now(): intReturns the timestamp of the number of seconds
just(mixed $value): OptionalMaybe/Option monad (container) which may or may not contain a non-null value. Has methods:
isPresent(): bool - true if not empty
isEmpty(): bool - true if empty
get(): mixed - returns value, throw exception if empty
orElse(mixed $default): mixed - returns the contained value if the optional is nonempty or $default
orElseThrow(Exception $e) - returns the contained value, if present, otherwise throw an exception
map(callable $f): Optional - If a value is present, apply the $f to it, and if the result is non-null, return an Optional describing the result
flatMap(callable $f): Optional - use instead of map if $f returns Optional
filter(callable $predicate): Optional - if a value is present and matches the $predicate, return an Optional with the value, otherwise an empty Optional.
size(array|Countable|object|string|callable $value): intReturns size of a countable, number of parameters of a function, lenght of string or number of properties of an object
T(...$args): boolAlways returns true