PHP code example of capci / stream

1. Go to this page and download the library: Download capci/stream 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/ */

    

capci / stream example snippets


Stream::of("a", "b", "c", "d", "e", "x")->filter(function ($value) {
    return $value !== "x";
})->map(function($value) {
    return strtoupper($value);
})->forEach(function ($value) {
    echo $value . ", ";
});
// A, B, C, D, E,

$stream = Stream::of("a", "b", "c");

$stream = Stream::ofIterable([
    "a",
    "b",
    "c",
]);

$stream = Stream::ofIterable(new DirectoryIterator("."));

$stream = Stream::ofGenerator(function () {
    yield "a";
    yield "b";
    yield "c";
});

$stream = Stream::ofIterable(range(0, 10));
$stream = $stream->filter(function ($value) {
    return $value > 5;
});
// 6 7 8 9 10

$stream = Stream::ofIterable(range(0, 5));
$stream = $stream->map(function ($value) {
    return $value * 2;
});
// 0 2 4 6 8 10

$stream = Stream::ofIterable(range(0, 5));
$stream = $stream->sorted(function ($value1, $value2) {
    return $value2 - $value1;
});
// 5 4 3 2 1 0

$stream = Stream::ofIterable(range(0, 5));
$stream->forEach(function ($value) {
    echo $value . ", ";
});
// 0, 1, 2, 3, 4, 5,

$stream = Stream::ofIterable(range(0, 5));
$array = $stream->toArray();
// [0, 1, 2, 3, 4, 5]

$stream = Stream::ofIterable(range(0, 5));
$sum = $stream->reduce(0, function ($accum, $value) {
    return $accum + $value;
});
// 15