PHP code example of dbrans / stream-pipeline
1. Go to this page and download the library: Download dbrans/stream-pipeline 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/ */
dbrans / stream-pipeline example snippets
$input = [' B1 ', ' B2', 'a1 ', ' a2 ', 'a3', ' b1', ' b2', 'b3'];
$elements = [];
foreach ($input as $e) {
$elem = strtoupper(trim($e));
if (substr($e, 0, 1) === 'B' && !in_array($elem, $elements)) {
$elements[] = $elem;
}
}
return implode(',', $elements);
return Stream::of(' B1 ', ' B2', 'a1 ', ' a2 ', 'a3', ' b1', ' b2', 'b3')
->map(Strings::trim())
->map(Strings::toUpper())
->filter(Strings::startsWith('B'))
->distinct()
->collect(Collectors::join(','));
use StreamPipeline\Stream;
use StreamPipeline\Stream;
$arrStream = Stream::fromIterable($myArray);
// or
$arrStream = Stream::of(' B1 ', ' B2', 'a1 ', ' a2 ', 'a3', ' b1', ' b2', 'b3')
use StreamPipeline\Collectors;
use StreamPipeline\Iterators\NumberGenerator;
use StreamPipeline\Operations\Strings;
// ...
$arrStream
->map(Strings::trim())
->map(Strings::toUpper())
->filter(Strings::startsWith('B'))
->distinct()
->forEach(function ($e) {
echo $e;
});
$arrStream
->filter(function ($e) {
return $e % 2 === 0;
})
->map(function ($e) {
return $e + 10;
})
->toArray();
$arrStream
->filter(Numbers::isEven())
->map(Numbers::plus(10))
->collect(Collectors::sum());
$arrStream
->map(function ($elem, $i, $index) {
return ...
})
->toArray();
Stream::of('a', 'b', 'c', 'd', 'e', 'f')
->limit(5)
->collect(Collectors::join(','));
Stream::iterate(1, new NumberGenerator(1))
->filter(Numbers::isEven())
->skip(5)
->limit(11);