miroshnikov / idles

PHP functional utility library, port of javascript Lodash/Ramda, a set of side-effect free, immutable and automatically curried functions that support lazy evaluation.

Home Page:https://idlephp.tech

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IDLES - a PHP functional library

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.

Requirements

PHP 7.4 or higher

Installation

composer require miroshnikov/idles

Roadmap

Note

Idles is currently under active development. Roadmap is to add all methods from Lodash and Ramda libraries and some functional tools.

Documentation

Array

concat(?iterable $array, $value): iterable

Concatinates $array with additional iterables/values

drop(int $n, ?iterable $collection): iterable

Skips the first $n elemens and returns the rest of the iterable

dropRight(int $n, ?iterable $collection): iterable

Skips the last $n elements

findIndex(callable $predicate, ?iterable $collection): int

Like find but returns the index of the first element predicate returns truthy for, -1 if not found

findLastIndex(callable $predicate, ?iterable $collection): int

Like find but returns the index of the last element predicate returns truthy for, -1 if not found

flatten(?iterable $collection): iterable

Flattens iterable a single level deep.

flattenDeep(?iterable $collection): iterable

Recursively flattens iterable.

flattenDepth(int $depth, ?iterable $collection): iterable

Recursively flatten array up to depth times.

fromPairs(?iterable $collection): array

Creates 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): int

Returns the index of the first occurrence of $value in $collection, else -1.

intersection(?iterable $record1, ?iterable $record2): array

Returns unique values that are included in both records

intersectionBy(callable $iteratee, ?iterable $record1, ?iterable $record2): array

Like intersection but invokes $iteratee for each element before comparison.

intersectionWith(callable $comparator, ?iterable $record1, ?iterable $record2): array

Like intersection but invokes $comparator to compare elements.

join(string $separator, ?iterable $collection): string

Joins 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): array

Removes items from $iterable starting at $start and containing $count elements.

slice(int $start, int ?$end, ?iterable $collection): iterable

Retruns a slice of $iterable from $start up to, but not including, $end.

take(int $n, ?iterable $collection): iterable

Takes n first elements from iterable

takeRight(int $n, ?iterable $collection): array

Returns a slice of iterable with n elements taken from the end.

uniq(?iterable $collection): array

Removes duplicates using ===

uniqBy(callable $iteratee, ?iterable $collection): array

Like uniq but apply $iteratee fist

uniqWith(callable $predicate, ?iterable $collection): array

Like uniq but uses $predicate to compare elements

without(array $values, ?iterable $collection): iterable

Returns $iterable without $values

zip(iterable $a, iterable $b): iterable

Creates 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): iterable

Like zip except that it accepts $iteratee to specify how grouped values should be combined.

Collection

all(?callable $predicate, ?iterable $collection): bool

Checks if $predicate returns truthy for all elements of $collection. Stop once it returns falsey

any(callable $predicate, ?iterable $collection): bool

Checks if $predicate returns truthy for any element of $collection. Stops on first found.

each(callable $iteratee, ?iterable $collection): iterable

Iterates over elements of $collection. Iteratee may exit iteration early by returning false.

filter(callable $predicate, ?iterable $collection): iterable

Returns elements $predicate returns truthy for.

find(?callable $predicate, ?iterable $collection)

Returns the first element $predicate returns truthy for.

flatMap(callable $iteratee, ?iterable $collection): iterable

Maps then flatten

flatMapDeep(callable $iteratee, ?iterable $collection): iterable

Like flatMap but recursively flattens the results.

flatMapDepth(callable $iteratee, int $depth, ?iterable $collection): iterable

Like flatMap but flattens the mapped results up to $depth times

groupBy(callable $iteratee, ?iterable $collection): array

Creates an array composed of keys generated from running each value through $iteratee.

includes($value, ?iterable $collection): bool

Checks if $value is in $collection.

indexBy(callable $iteratee, ?iterable $collection): iterable

Creates 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): array

Split $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): array

Adds new properties to $record using $resolvers.

sortBy(array $iteratees, ?iterable $collection): array

Sorts $collection in ascending order according to $iteratees.

values(?iterable $collection): iterable

Returns an indexed iterable of values in $collection.

Function

always($value)

Returns a function that always returns the given value.

applyTo($value, callable $interceptor)

Returns $interceptor($value).

ary(int $n, callable $fn): callable

Creates a function that invokes $fn, with up to $n arguments, ignoring any additional arguments.

attempt(callable $fn)

Calls $fn, returning either the result or the caught exception.

compose(callable ...$funcs): callable

Like pipe but invokes the functions from right to left.

curry(callable $f): callable

\Idles\_ const may be used as a placeholder.

curryRight(callable $f): callable

Like curry but arguments are prepended.

flip(callable $fn): callable

Returns a new curried function with the first two arguments reversed

juxt(array $funcs): callable

applies a list of functions to a list of values.

memoize(callable $func): callable

Creates a function that memoizes the result of $func. $resolver returns map cache key, args[0] by default.

negate(callable $predicate): callable

Creates 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): callable

Creates a function that invokes $fn with $partials prepended to the arguments. \Idles\_ const may be used as a placeholder.

partialRight(callable $fn, array $partials): callable

Like partial but $partials are appended.

pipe(callable ...$funcs): callable

Left-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): array

Calls 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): callable

ary(1, $fn)

Logic

allPass(array $predicates): callable

Returns a function that checks if its arguments pass all $predicates.

anyPass(array $predicates): callable

Returns a function that checks if its arguments pass any of the $predicates.

both(callable $func1, callable $func2): callable

Resulting function returns $func1(...$args) if it is falsy or $func2(...$args) otherwise, short-circuited

cond(array $pairs): callable

Iterates 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): callable

Resulting function returns $func1(...$args) if it is truthy or $func2(...$args) otherwise, short-circuited.

ifElse(callable $predicate, callable $onTrue, callable $onFalse): callable

Resulting function returns $onTrue(...$args) if $predicate(...$args) is truthy or $onFalse(...$args) otherwise.

not($a): bool

returns !$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

Math

add(int|float $a, int|float $b): int|float

$a + $b

dec(int $number): int

Returns $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): int

Returns $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): float

Rounds $numberto specified $precision

subtract(int|float $a, int|float $b): int|float

$a - $b

sum(?iterable $collection)

Sums elements in iterable

sumBy(?callable $iteratee, ?iterable $collection)

Like sum but $iteratee is invoked for each element in iterable to generate the value to be summed.

Record

assignDeep(array $iterables): array

Merges properties recursively, numeric keys are overwritten.

defaults(?iterable $record1, ?iterable $record2): array

Merges properties from right to left, numeric keys are overwritten.

evolve(array $transformations, ?iterable $record): array

Creates a new record by recursively calling transformation functions with $record properties.

extend(?iterable $source1, ?iterable $source2): array

Merges properties, numeric keys are overwritten.

has(string|int $key, ?iterable $record): bool

Checks if $record has $key

hasPath(string|int|array $path, ?iterable $record): bool

Checks if $path exists in $record

invert(?iterable $collection): array

Replaces keys with values. Duplicate keys are overwritten.

keys(?iterable $record): iterable

Returns an indexed iterable of keys in $record.

merge(?iterable $source1, ?iterable $source2): array

Merges properties, numeric keys are appended.

mergeDeep(array $iterables): array

Merges properties recursively, numeric keys are appended.

mergeLeft(?iterable $left, ?iterable $right): array

calls merge($right, $left)

mergeWith(callable $customizer, ?iterable $left, ?iterable $right): array

Like 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): array

Creates an array containing a single key => value pair.

omit(array $keys, ?iterable $collection): iterable

The opposite of pick. Returns record without $keys.

omitBy(callable $predicate, ?iterable $record): iterable

The 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.

pick(array $keys, ?iterable $collection): iterable

Returns record containing only $keys

pickBy(callable $predicate, ?iterable $record): iterable

Returns 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): bool

Returns $record[$key] == $value

props(array $paths, ?iterable $collection): array

Keys in, values out. Order is preserved.

setPath($path, $value, ?iterable $record)

Return copy $record with $path set with $value

toPairs(?iterable $record): iterable

Converts a record into an array of [$key, $value]

where(array $spec, ?iterable $record): bool

Checks if $record satisfies the spec by invoking the $spec properties with the corresponding properties of $record.

whereAny(array $spec, ?iterable $record): bool

Checks 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): bool

Check if the $test satisfies the $spec

String

escape(string $s): string

Converts the characters "&", "<", ">", '"', and "'" to their corresponding HTML entities.

escapeRegExp(string $regexp): string

Escapes regular expression

split(string $separator, string $s): array

Splits string by $separator.

startsWith(string $target, string $s): bool

If string starts with $target.

toLower(string $s): string

Converts string to lower case

toUpper(string $s): string

Converts string to upper case

words(string $pattern, string $string): array

Splits string into an array of its words.

Util

collect(?iterable $iterable): array

Collects any iterable into array

eq($a, $b): bool

$a == $b

equals($a, $b): bool

$a === $b

F(...$args): bool

Always returns false

identity($value)

Returns the first argument it receives.

iterate(callable $f, $value): iterable

Returns a generator of $value, $f($value), $f($f($value)) etc.

just($value): Optional

Returns an Optional with the specified non-null value

nothing(): Optional

Returns an empty Optional

now(): int

Returns the timestamp of the number of seconds

just(mixed $value): Optional

Maybe/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): int

Returns size of a countable, number of parameters of a function, lenght of string or number of properties of an object

T(...$args): bool

Always returns true

About

PHP functional utility library, port of javascript Lodash/Ramda, a set of side-effect free, immutable and automatically curried functions that support lazy evaluation.

https://idlephp.tech

License:MIT License


Languages

Language:PHP 100.0%