Sphream is a functional php library. It is inspired by Haskell Lists, Java8 Streams and underscore.js.
Sphream allows you to manipulate arrays and Traversables in a more intuitive way.
$integers = [ 3, -19, 9, 1, 5, 392, 29, -13, 29, -2, -4, 234999 ];
$newIntegers = Sphream\Sphream::of($integers)
->filter(function ($item) { return 0 < $item && $item < 10; })
->takeWhile(function ($item) { return $item > 1; })
->map(function ($item) { return $item * 100; })
->toArray();
// $newIntegers is equal to [ 300, 900 ]
composer require pepijno/sphream
Creates a Sphream from an array or a generator.
Using an array:
$array = [ 3, 5, 9 ];
$sphream = Sphream\Sphream::of($array);
// $sphream contains items 3, 5 and 9.
When using generators:
$generator = function () {
yield 3;
yield 5;
yield 9;
};
$sphream = Sphream\Sphream::of($generator());
// $sphream contains items 3, 5 and 9.
Creates an empty Sphream.
$sphream = Sphream\Sphream::mempty();
//$sphream is empty
Creates a Sphream containing a range of integers between a start and an end. The end is not inclusive in the Sphream.
$sphream = Sphream\Sphream::range(6, 11);
// $sphream contains the elements 6, 7, 8, 9 and 10.
$sphream = Sphream\Sphream::range(-21, -17);
// $sphream contains the elements -21, -20, -19 and -18.
Creates a Sphrem by repeating a value a certain N
times.
$sphream = Sphream\Sphream::repeat("Hello", 8);
// $sphream contains the string "Hello" 8 times.
Creates an infinite Sphream, the elements are generated by repeatedly executing the callback.
$callback = function () { return 2; };
$sphream = Sphream\Sphream::generate($callback);
// $sphream contains of an inifite amount of 2's.
Returns true if a Sphream is empty and false otherwise.
Sphream\Sphream::of([])->isEmpty(); // returns true
Sphream\Sphream::of([2])->isEmpty(); // returns false
Returns true if a Sphream is closed and false otherwise.
$sphream = Sphream\Sphream::of([1, 2]);
$sphream->isClosed(); // returns false
$sphream->toArray();
$sphream->isClosed(); // returns true
Applies a callback to each element of the Sphream.
$sphream = Sphream\Sphream::of([2, 3])
->map(function ($item) { return $item * 2; });
// $sphream consist of items 4 and 6.
Filters all elements from the Sphream for which the callback returns false.
$sphream = Sphream\Sphream::of([1, 2, 3, 4])
->filter(function ($item) { return ($item % 2) == 0; });
// $sphream contains elements 2 and 4.
Takes the first N
items of the Sphream, discarding the rest of the Sphream.
$sphream = Sphream\Sphream::of([2, 4, 9, 1, 3])
->take(3);
// $sphream contains elements 2, 4 and 9.
Drops the first N
items of the Sphream.
$sphream = Sphream\Sphream::of([2, 4, 9, 1, 3])
->drop(2);
// $sphream contains elements 9, 1 and 3.
Takes elements from the Sphream as long as the callback returns true.
$sphream = Sphream\Sphream::of([2, 4, 9, 1, 3])
->takeWhile(function ($item) { return $item < 9; });
// $sphream contains elements 2 and 4.
Drops elements from the Sphream as long as the callback returns true.
$sphream = Sphream\Sphream::of([2, 4, 9, 1, 3])
->takeWhile(function ($item) { return $item != 1; });
// $sphream contains elements 1 and 3.