/prelude

Functional library for PHP

Primary LanguagePHPMIT LicenseMIT

Prelude

Scrutinizer Code Quality Code Coverage Build Status

Install

composer require sergiors/prelude "dev-master"

How to use

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';
    }]
]);

echo $fn(0); // => water freezes at 0°C
echo $fn(50); // => nothing special happens at 50°C
echo $fn(100); // => water boils at 100°C
use function Prelude\has;

$hasName = has('name');
echo $hasName(['name' => 'Jimi']); // => true
echo $hasName([]); // => false
use const Prelude\id;
use const Prelude\isEmpty;
use function Prelude\ifElse;
use function Prelude\always;

$fn = ifElse(isEmpty)
    (always(true))
    (always(false));
echo $fn([]); // => true
echo $fn(null); // => true
echo $fn(false); // => true
echo $fn('James'); // => false
use function Prelude\head;

echo head([1, 2, 3]); // => 1
use function Prelude\tail;

echo tail([1, 2, 3]); // => [2, 3]

API

All functions is unitary, 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()

Constants from native functions

keys 
values
flip 
sum
isNull
isInt
isFloat
isNumeric
isString
isBool
isScalar
isArray
isObject
isCallable

Thanks

Marcelo Camargo

License

MIT