A functional library for PHP programmers, similar to Ramdajs.
There are already several excellent libraries with a functional flavor. Typically, they are meant to be general-purpose toolkits, suitable for working in multiple paradigms. Slash has a more focused goal. We wanted a library designed specifically for a functional programming style, one that makes it easy to create functional pipelines, one that never mutates user data.
The primary distinguishing features of slash are:
Slash emphasizes a purer functional style. Immutability and side-effect free functions are at the heart of its design philosophy. This can help you get the job done with simple, elegant code.
Slash functions are automatically curried. This allows you to easily build up new functions from old ones simply by not supplying the final parameters.
The parameters to Slash functions are arranged to make it convenient for currying. The data to be operated on is generally supplied last.
The last two points together make it very easy to build functions as sequences of simpler functions, each of which transforms the data and passes it along to the next. Slash is designed to support this style of coding.
Requires PHP 7.4+
composer require masterfermin02/slash
Slash operations are pure functions that can be used alone.
A small usage example for the groupBy function:
<?php
use function Slash\groupBy;
$records = [
['id' => 1, 'value1' => 5, 'value2' => 10],
['id' => 2, 'value1' => 50, 'value2' => 100],
['id' => 1, 'value1' => 2, 'value2' => 2],
['id' => 2, 'value1' => 15, 'value2' => 20],
['id' => 3, 'value1' => 15, 'value2' => 20],
];
$groupById = groupBy('id');
$grouped = $groupById($records);
/*
* resultado : [
* 1 => [ [ "id" => 1, "value1" => 5, "value2" => 10 ], [ "id" => 1, "value1" => 1, "value2" => 2 ] ],
* 2 => [ [ "id" => 2, "value1" => 50, "value2" => 100 ], [ "id" => 2, "value1" => 15, "value2" => 20 ] ],
* 3 => [ [ "id" => 3, "value1" => 15, "value2" => 20 ] ]
* ];
*/
Map usage :
<?php
use Slash\Slash;
Slash\map([1, 2, 3], fn ($n) => $n * 2); // === [2, 4, 6]
Example with slash object:
<?php
use Slash\Slash;
Slash::max([1, 2, 3]) // => 3
Slash::flatten([1, [2, [3]]]) // => [1, 2, 3]
Slash::last([1, 2, 3], 2) // => [2, 3]
Here is what is available to you:
mixed defaults(mixed $object, array | mixed $defaults) |
mixed | array first(array $elements, integer $amount = 1) |
mixed | array last(array $elements, integer $amount = 1) |
integer | null size(array | Countable $value) |
Found a bug or have a suggestion? Please create a new GitHub issue. We want your feedback!