PinkCrab Library — Function Constructors

At its core, Function Constructors is a library for building bigger functions out of smaller ones. Every helper here either partially applies a standard operation (so you can bind its configuration up front and reuse the resulting Closure) or it stitches callables together with compose / pipe.

Compose or pipe — build once, reuse everywhere

compose returns a Closure you can pass anywhere a callable is expected. pipe takes a value and runs it through the chain immediately.

// Build once.
$slugify = F\compose('trim', 'strtolower', Str\replaceWith(' ', '-'));

array_map($slugify, ['  Hello World  ', 'FOO Bar']);
// ['hello-world', 'foo-bar']

// Or run immediately.
F\pipe(
    [3, 4, 5, 6, 8],
    Arr\filter(Num\isMultipleOf(2)),
    Arr\map(Num\multiply(2))
);
// [8, 12, 16]

composeR / pipeR run callables in reverse order. composeSafe halts on the first null. composeTypeSafe halts on a custom type check.

Records — one API for arrays and objects

getProperty, hasProperty, propertyEquals, setProperty, pluckProperty all work uniformly on arrays AND objects. Partially apply them and they become named, reusable concepts.

$users = [
    ['id' => 1, 'name' => 'Ada', 'role' => 'admin'],
    ['id' => 2, 'name' => 'Bea', 'role' => 'user'],
    ['id' => 3, 'name' => 'Cal', 'role' => 'admin'],
];

$isAdmin = F\propertyEquals('role', 'admin');

array_filter($users, $isAdmin);
// [ ['id' => 1, ...], ['id' => 3, ...] ]

array_map(F\getProperty('name'), $users);
// ['Ada', 'Bea', 'Cal']

For building a fresh record from scratch, see recordEncoder + encodeProperty — walked through in the complex objects example.

Every Array function accepts iterables

The Arrays namespace functions all accept array, Iterator, or Generator interchangeably. Lazy functions (map, filter, take, takeWhile, …) stream through a Generator without materialising it. Short-circuit ones (filterFirst, filterAny, head) stop at the answer. Terminal ones (sorts, foldR, takeLast) consume everything — so never run them against an infinite source.

$naturals = function () { $i = 1; while (true) yield $i++; };

// Safe — take pulls only 5 values from the infinite generator.
foreach (Arr\take(5)($naturals()) as $n) echo "$n ";   // 1 2 3 4 5

Where next


String Functions

Array Functions

Number Functions

General Functions

Object Functions

Comparison Functions