PHP code example of denprog / river-flow

1. Go to this page and download the library: Download denprog/river-flow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

denprog / river-flow example snippets




declare(strict_types=1);

use function Denprog\RiverFlow\Pipes\{map, filter, toList};
use function Denprog\RiverFlow\Strings\{trim, toUpperCase};

$result = [10, 15, 20, 25, 30]
    |> filter(fn (int $n) => $n % 2 === 0) // [10, 20, 30]
    |> map(fn (int $n) => $n / 10)         // [1, 2, 3]
    |> toList();                           // [1, 2, 3]

$text = "  river flow  "
    |> trim()
    |> toUpperCase(); // "RIVER FLOW"

use function Denprog\RiverFlow\Pipes\{map, filter, toList};

$res1 = toList(map(filter([1,2,3,4], fn($x)=>$x%2===0), fn($x)=>$x*10))); // [20, 40]

$res2 = [1,2,3,4]
    |> filter(fn($x) => $x % 2 === 0)
    |> map(fn($x) => $x * 10)
    |> toList(); // [20, 40]

use function Denprog\RiverFlow\Utils\{compose, pipe};

$sum = fn (int $a, int $b): int => $a + $b;   // right‑most may be variadic
$inc = fn (int $x): int => $x + 1;
$dbl = fn (int $x): int => $x * 2;

$f = compose($dbl, $inc, $sum); // dbl(inc(sum(a,b)))
assert($f(3, 4) === 16);

$out = pipe(5, fn($x) => $x + 3, fn($x) => $x * 2, 'strval');
assert($out === '16');

use function Denprog\RiverFlow\Pipes\{filter, map, take, toList, toArray, flatten, uniq, groupBy, values, sortBy, zipWith, range, repeat, times, tail, init, scan, scanRight, partitionBy, distinctUntilChanged, intersperse, pairwise, countBy};

// Transform → filter → take → materialize
$topSquares = [1,2,3,4,5,6,7,8,9]
    |> map(fn (int $n) => $n * $n)
    |> filter(fn (int $x) => $x % 2 === 0)
    |> take(3)
    |> toList(); // [4, 16, 36]

// Flatten nested, uniquify
$flatUnique = [[1,2], [2,3, [3,4]], 4]
    |> flatten(2)
    |> uniq()
    |> toList(); // [1,2,3,4]

// Group, traverse group values and sort by size
$byFirstLetter = ['apple','apricot','banana','blueberry','avocado']
    |> groupBy(fn (string $s) => $s[0])
    |> values()
    |> map(fn (array $xs) => $xs)
    |> toList()
    |> sortBy(fn (array $xs) => \count($xs));

// Zip in pipelines
$zipped = [1, 2, 3]
    |> zipWith(['a','b'], ['X','Y','Z'])
    |> toList(); // [[1,'a','X'], [2,'b','Y']]

// Numeric ranges and generation
$nums = range(0, 5) |> toList(); // [0,1,2,3,4]

// Infinite repetition capped with take
$threes = repeat(3) |> take(4) |> toList(); // [3,3,3,3]

// Produce values by index
$squares = times(5, fn (int $i): int => $i * $i) |> toList(); // [0,1,4,9,16]

$rest = ['a'=>1,'b'=>2,'c'=>3]
    |> tail()
    |> toArray(); // ['b'=>2,'c'=>3]

$allButLast = ['x'=>10,'y'=>20,'z'=>30]
    |> init()
    |> toArray(); // ['x'=>10,'y'=>20]

// Inclusive prefix and suffix scans (lazy, keys preserved)
$prefix = [1, 2, 3, 4]
    |> scan(fn (?int $c, int $v) => ($c ?? 0) + $v, 0)
    |> toList(); // [1, 3, 6, 10]

$suffix = [1, 2, 3]
    |> scanRight(fn (?int $c, int $v) => ($c ?? 0) + $v, 0)
    |> toList(); // [6, 5, 3]

// Partition into contiguous groups by discriminator (lazy)
$groups = ['ant', 'apple', 'bear', 'bob', 'cat']
    |> partitionBy(fn (string $s) => $s[0])
    |> toList();
// [[0=>'ant',1=>'apple'], [2=>'bear',3=>'bob'], [4=>'cat']]

// Skip consecutive duplicates (preserves first keys of runs)
$d = ['ant', 'apple', 'bear', 'bob', 'cat']
    |> distinctUntilChanged(fn (string $s) => $s[0])
    |> toArray(); // [0=>'ant', 2=>'bear', 4=>'cat']

// Intersperse a separator (keys discarded)
$withPipes = ['a','b','c']
    |> intersperse('|')
    |> toList(); // ['a','|','b','|','c']

// Pairwise (consecutive pairs)
$pairs = [1,2,3]
    |> pairwise()
    |> toList(); // [[1,2],[2,3]]

// Count by classifier (eager)
$counts = ['apple','apricot','banana','blueberry','avocado']
    |> countBy(fn (string $s) => $s[0]); // ['a' => 3, 'b' => 2]

use function Denprog\RiverFlow\Strings\{trim, replacePrefix, toLowerCase, toUpperCase, split, join, length};

$title = "  River FLOW: Intro  "
    |> trim()
    |> toLowerCase()
    |> replacePrefix('river ', 'river ');
// "river flow: intro"

$csv = ' foo | Bar |BAZ '
    |> trim()
    |> toLowerCase()
    |> split('|')
    |> join(','); // "foo , bar ,baz"

$n = '  Hello  ' |> trim() |> toUpperCase() |> length(); // 5

use function Denprog\RiverFlow\Utils\{tap, identity};
use function Denprog\RiverFlow\Strings\{trim, toUpperCase};

$result = '  Hello  '
    |> trim()
    |> tap(fn (string $s) => error_log("after trim: $s"))
    |> toUpperCase()
    |> tap(fn (string $s) => error_log("after upper: $s"));
// 'HELLO'

$val = 10
    |> identity()
    |> (fn (int $x) => $x + 5)
    |> (fn (int $x) => $x * 2); // 30

use function Denprog\RiverFlow\Structs\{pick, omit, getPath, setPath, evolve, zipAssoc};

$user = ['name' => 'Alice', 'email' => '[email protected]', 'age' => 30, 'password' => 'secret'];

// Pick specific keys
$partial = pick(['name', 'email'], $user);
// ['name' => 'Alice', 'email' => '[email protected]']

// Omit sensitive keys
$safe = omit(['password'], $user);
// ['name' => 'Alice', 'email' => '[email protected]', 'age' => 30]

// Safe deep access (returns null when path missing)
$data = ['user' => ['profile' => ['bio' => 'Hello world']]];
$bio = getPath(['user', 'profile', 'bio'], $data); // 'Hello world'
$missing = getPath(['user', 'settings', 'theme'], $data); // null

// Immutable deep set (creates nested arrays as needed)
$updated = setPath(['user', 'profile', 'bio'], 'Updated bio', $data);
// $data remains unchanged, $updated has new bio

// Transform with spec — apply functions to specific keys
$evolved = evolve([
    'age' => fn($a) => $a + 1,
    'name' => fn($n) => strtoupper($n),
], $user);
// ['name' => 'ALICE', 'email' => '...', 'age' => 31, 'password' => 'secret']

// Zip keys with values
$result = zipAssoc(['a', 'b', 'c'], [1, 2, 3]);
// ['a' => 1, 'b' => 2, 'c' => 3]