Pipe-native stream functions for PHP 8.5+. Curried, lazy, zero-wrapper.
use function Stann\Stream\{filter, map, take, toArray};
$result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|> filter(fn(int $n) => $n % 2 === 0)
|> map(fn(int $n) => $n * 10)
|> take(3)
|> toArray();
// [20, 40, 60]No Collection object. No method chaining. Just pure functions designed for the PHP 8.5 pipe operator (|>).
Each function is curried: it takes its configuration and returns a Closure that accepts an iterable. The pipe operator does the wiring.
data |> transform(config) |> transform(config) |> terminator(config)
- Data-last β like Ramda (JS) or Elixir pipes
- Lazy by default β transformations return Generators, nothing executes until consumed
- Zero dependency β just PHP 8.5
composer require stann/streamRequires PHP 8.5+ (for the pipe operator |>).
use function Stann\Stream\{filter, map, take, toArray};
// Simple pipeline
$emails = $users
|> filter(fn(User $u) => $u->isActive())
|> map(fn(User $u) => $u->email)
|> map(trim(...))
|> toArray();
// Lazy evaluation β only 100 elements processed, not all
$result = $hugeDataset
|> filter(fn($row) => $row['status'] === 'ok')
|> map(fn($row) => transform($row))
|> take(100)
|> toArray();Full documentation with signatures and examples: docs/API.md
Lazy (Generator-based) unless noted as blocking.
mapβ Apply a callback to each elementfilterβ Keep elements matching a predicate (without callback: remove falsy values)flatMapβ Map then flatten one levelflattenβ Flatten one level of nested iterablestakeβ Take the first N elementstakeWhileβ Take while predicate holdsskipβ Skip the first N elementsskipWhileβ Skip while predicate holdschunkβ Split into fixed-size chunksgroupByβ Group by key function (blocking)sortByβ Sort by key function (blocking)uniqueβ Remove duplicateszipβ Combine two iterables into pairsconcatβ Append another iterableenumerateβ Pair elements with their indexscanβ Running fold (intermediate values)reverseβ Reverse elements (blocking)keysβ Extract keysvaluesβ Extract valuespluckβ Extract a property/key from each elementtapβ Side effect without altering the stream
Consume the iterable and return a final value.
toArrayβ Convert to arrayreduceβ Fold into a single valuefirstβ First element (optionally matching predicate)lastβ Last element (optionally matching predicate)countβ Count elementssumβ Sum elementsminβ Minimum elementmaxβ Maximum elementjoinβ Join into a stringcontainsβ Check if value existseveryβ All match predicate?someβ Any match predicate?partitionβ Split into two arrays by predicateeachβ Consume with side effect (void)
$page3 = $items
|> sortBy(fn(Item $i) => $i->name)
|> skip(($page - 1) * $perPage)
|> take($perPage)
|> toArray();$items
|> chunk(50)
|> map(fn(array $batch) => processBatch($batch))
|> flatMap(fn(array $results) => $results)
|> toArray();$byCountry = $customers
|> filter(fn(Customer $c) => $c->revenue > 1000)
|> groupBy(fn(Customer $c) => $c->country)
|> map(fn(array $group) => $group |> sum(fn($c) => $c->revenue));$total = $prices
|> zip($quantities)
|> map(fn(array $pair) => $pair[0] * $pair[1])
|> sum();$runningTotal = $transactions
|> map(fn(Transaction $t) => $t->amount)
|> scan(fn(float $acc, float $v) => $acc + $v, 0.0)
|> toArray();The pipe is open by design. Any function that takes an iterable and returns an iterable (or a final value) fits right in β no interface to implement, no class to extend.
function removeNulls(iterable $items): Generator {
foreach ($items as $value) {
if ($value !== null) {
yield $value;
}
}
}
$data |> removeNulls(...) |> map(fn($v) => $v * 2) |> toArray();function olderThan(int $minAge): Closure {
return static function (iterable $items) use ($minAge): Generator {
foreach ($items as $user) {
if ($user->age >= $minAge) {
yield $user;
}
}
};
}
$users
|> olderThan(18)
|> filter(fn(User $u) => $u->isActive())
|> map(fn(User $u) => $u->email)
|> map(trim(...))
|> toArray();Both approaches mix naturally with the library's functions. The only rule: the pipe expects a single-argument callable (iterable β something).
Every function follows the same pattern:
function map(callable $fn): Closure
{
return static function (iterable $items) use ($fn): Generator {
foreach ($items as $key => $value) {
yield $key => $fn($value, $key);
}
};
}- Takes configuration (the callback, size, etc.)
- Returns a Closure that accepts
iterable - The pipe operator passes the data
This is currying β you fix the transformation, and the pipe injects the data.
| Lazy (Generator) | Blocking (array) |
|---|---|
map, filter, flatMap, flatten, take, takeWhile skip, skipWhile, chunk, zip, concat, enumerate scan, unique, keys, values, pluck, tap |
sortBy, groupBy, reverse |
Blocking operations need all data upfront (you can't sort without seeing everything). Lazy operations process elements one by one.
composer install
composer test # PHPUnit
composer phpstan # Static analysis (level 8)
composer cs-check # Code style check
composer cs-fix # Auto-fix code styleMIT