λ Functional

Functional programming treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is declarative, which means expressions instead of statements.

Siler is bundled with the Siler\Functional namespace. It brings some function declarations that aids the work with another first-class and high-order PHP functions!

identity()

Returns a Closure that returns its given arguments.

use Siler\Functional as λ;

array_map(λ\identity(), [1, 2, 3]);
// [1, 2, 3]
circle-info

Doesn't seem useful at first, but working with the functional paradigm, you'll find the reason shortly.

always($value)

Almost like identity(), but it always returns the given value.

use Siler\Functional as λ;

array_map(λ\always('foo'), range(1, 3));
// [foo, foo, foo]

if_else(callable $cond) -> $then -> $else

A functional if/then/else.

partial(callable $callable, ...$partial)

Partial application refers to the process of fixing a number of arguments to a function, producing another function of smaller arityarrow-up-right. Given a function{\displaystyle f\colon (X\times Y\times Z)\to N}, we might fix (or 'bind') the first argument, producing a function of type {\displaystyle {\text{partial}}(f)\colon (Y\times Z)\to N}. https://en.wikipedia.org/wiki/Partial_applicationarrow-up-right

Nothing like a good example:

Works with any callable:

match(array $matches)

A pattern-match attempt. Truthy Closure evaluations on the left calls and short-circuits evaluations on the right.

circle-info

There are a lot more of them. A good place it check it out are the testsarrow-up-right.

Last updated