All functions are unitaries, exception when the second argument is optional. It has constants to help you to work with point-free style.
all()
allPass()
always()
any()
anyPass()
append()
contains()
compose()
cond()
divide()
drop()
equals()
filter()
find()
- flatten()
- gt()
- gte()
- has()
- head()
- id()
- ifElse()
- isEmpty()
- indexOf()
- init()
- join()
- last()
- lt()
- lte()
- map()
- memoize()
- merge()
- multiply()
- none()
- not()
- partial()
- pipe()
- prepend()
- prop()
- props()
- reduce()
- replace()
- slice()
- split()
- tail()
- take()
- toString()
- typeof()
- unless()
- when()
- keys
- values
- flip
- sum
- isNull
- isInt
- isFloat
- isNumeric
- isString
- isBool
- isScalar
- isArray
- isObject
- isCallable
function all(callable $pred): \Closure;
function => closure([array]) => boolean
use const Prelude\isScalar;
use function Prelude\all;
$even = function ($n) {
return $n % 2 === 0;
};
all($even)([2, 4, 6, 8, 10, 12]); // => true
all(isScalar)([1, 2, 3, 4]); // => true
all(isScalar)([1, 2, [], 4]); // => false
function allPass(array $preds): \Closure;
function => closure => boolean
use function Prelude\partial;
use function Prelude\allPass;
use function Prelude\get;
use function Prelude\has;
$placeholders = [
'to' => '[email protected]',
'from' => '[email protected]'
];
$propEq = partial(function ($k, $v, array $xss) {
return ($xss[$k] ?? false) === $v;
});
$y = allPass([has('from'), has('to')]);
$y($placeholders); // => true
$x = allPass([$propEq('rank', 'Q'), $propEq('suit', '♠︎')]);
$x(['rank' => 'Q', 'suit' => '♠︎']); // => true
$x(['rank' => 'Q', 'suit' => '♣︎︎']); // => false
function always($x): \Closure
{
return function () use ($x) {
return $x;
};
}
function any(callable $pred): \Closure;
function => closure([array]) => boolean
use function Prelude\any;
$isEven = function ($n) { return $n % 2 === 0; };
$isOdd = function ($n) { return $n % 2 === 1; };
any($isEven)([1, 3, 4, 5, 7]); // => true
any($isEven)([1, 3, 5, 7]); // => false
any($isOdd)([1, 3, 5, 7]); // => true
any($isOdd)([2, 4, 6]); // => false
any('is_array')([2, []]); // => true
any('is_array')(['s', 'x']); // => false
function anyPass(array $preds): \Closure;
[array] => closure(callable) => boolean
use function Prelude\anyPass;
use function Prelude\partial;
use function Prelude\equals;
use function Prelude\has;
$gt = partial(function ($a, $b) { return $a > $b; });
$gte = anyPass([$gt(3), equals(3)]);
$gte(2); // => true
$gte(3); // => true
$gte(4); // => false
$has = anyPass([has('user'), has('mobile')]);
$has(['user' => '']); // => true
$has(['mobile' => '']); // => true
$has([]); // => false
function append($x): \Closure;
any => closure([array]) => [array]
use function Prelude\append;
append('tests')(['write', 'more']); // => ['write', 'more', 'tests']
$append = append(['tests']);
$append(['write', 'more']); // => ['write', 'more', ['tests']]
function contains($x): \Closure;
any => closure([array]) => bool
use function Prelude\Contains;
$ls = ['name' => 'James'];
contains('James')($ls); // => true
contains('Kirk')($ls); // => false
$nums = [10, 20, 30];
contains(10)($nums); // => true
function compose(callable ...$callbacks): \Closure;
use function Prelude\compose;
$compose = compose(
function ($x) {
return "foo($x)";
},
function ($x) {
return "bar($x)";
},
function ($x) {
return "baz($x)";
}
);
$compose('x'); // => foo(bar(baz(x)))
function cond(array $pairs): \Closure;
use const Prelude\id;
use function Prelude\cond;
use function Prelude\equals;
use function Prelude\always;
$fn = cond([
[equals(0), always('water freezes at 0°C')],
[equals(100), always('water boils at 100°C')],
[always(true), function ($temp) {
return 'nothing special happens at '.$temp.'°C';
}]
]);
$fn(0); // => 'water freezes at 0°C'
$fn(50); // => 'nothing special happens at 50°C'
$fn(100); // => 'water boils at 100°C'
$fn = cond([
[equals([]), always(true)]
]);
$fn(true); // => \Prelude\Exception\CondClauseException
function divide($x): \Closure;
use function Prelude\divide;
divide(10)(10); // => 1
divide(7)(2); // => 3.5
function drop(int $n): \Closure;
use function Prelude\drop;
drop(3)([1, 2, 3, 4, 5]); // => [4, 5]
drop(5)([1, 2, 3, 4, 5]); // => []
function equals($x): \Closure;
use function Prelude\equals;
equals(true)(true); // => true
equals(1)(1); // => true
equals("1")(1); // => false
equals("true")(true); // => false
$isTrue = equals(true);
$isTrue(true); // => true
$isTrue(false); // => false
function filter(callable $callback): \Closure;
use function Prelude\filter;
use function Prelude\equals;
$names = ['James' => true, 'Kirk' => false];
$istrue = filter(equals(true));
$istrue($names); // => ['James' => true]
function find(callable $callback): \Closure;
use function Prelude\find;
$values = [100, 200, 300, 400];
$isGreaterThan200 = function (int $n) { return $n > 200; };
find($isGreaterThan200)($values); // => 300