- class
Flow(php\util\Flow) - package
std - source
php/util/Flow.php
Description
A special class to work with arrays and iterators under flows. Flows are used for the lazy array/iterator operations, to save the RAM memory.
Class Flow, Immutable
Flow ::ofEmpty()Flow ::of()- Creates a new flow for an array of IteratorFlow ::ofRange()- Creates a new flow for a number rangeFlow ::ofString()- Creates a new flow for the stringFlow ::ofStream()- Creates a new flow for the Stream object
->__construct()- Create a new flow, you can also useof()method->withKeys()- Enables to save keys for the next operation->onlyKeys()->append()- Appends a new collection to the current flow,->anyMatch()- Returns whether any elements of this stream match the provided->allMatch()- Returns whether all elements of this stream match the provided predicate.->noneMatch()- Returns whether no elements of this stream match the provided predicate.->find()- Finds elements by using the $filter callback,->findOne()- Finds the first element by using the $filter callback,->findValue()->group()->each()- Iterates elements.->eachSlice()- Iterates elements as slices (that are passing as arrays to $callback).->map()- Iterates elements and returns a new flow of the result->keys()- Create a new flow by using the keys of the current flow->skip()- Skips $n elements in the current collection->limit()- Limits collection with $count->reduce()- Iterates elements and gets a result of this operation->max()- Get max of elements.->min()- Get min of elements.->avg()- Get avg number of elements.->median()- Get median of elements.->numMedian()->sum()- Get sum of elements.->concat()- Get concatenation of all elements.->sort()- Sort the last result of the flow, also see:php\\lib\\items::sort()->sortByKeys()- The same method assort()only based on keys insteadof values->toArray()- Convert elements to an array->toMap()- Convert element to an array with keys.->toString()- Join elements to a string similar toimplode()in PHP->count()->current()->next()->key()->valid()->rewind()->__clone()- Class is immutable, the disallowed clone method
Flow::ofEmpty(): php\util\FlowFlow::of(iterable $collection): php\util\FlowCreates a new flow for an array of Iterator
Flow::ofRange(int $from, int $to, int $step): php\util\FlowCreates a new flow for a number range
Flow::ofString(string $string, int $chunkSize): php\util\FlowCreates a new flow for the string
Flow::ofStream(php\io\Stream $stream, int $chunkSize): php\util\FlowCreates a new flow for the Stream object
__construct(iterable $collection): voidCreate a new flow, you can also use of() method
withKeys(): php\util\FlowEnables to save keys for the next operation
onlyKeys(iterable $keys, bool $ignoreCase): php\util\Flowappend(iterable $collection): php\util\FlowAppends a new collection to the current flow, do not remember that you can pass a flow to this method
anyMatch(callable $predicate): boolReturns whether any elements of this stream match the provided
predicate. May not evaluate the predicate on all elements if not
necessary for determining the result. If the flow is empty then
false is returned and the predicate is not evaluated.
allMatch(callable $predicate): boolReturns whether all elements of this stream match the provided predicate.
May not evaluate the predicate on all elements if not necessary for
determining the result. If the flow is empty then true is
returned and the predicate is not evaluated.
noneMatch(callable $predicate): boolReturns whether no elements of this stream match the provided predicate.
May not evaluate the predicate on all elements if not necessary for
determining the result. If the flow is empty then true is
returned and the predicate is not evaluated.
find(callable $filter): php\util\FlowFinds elements by using the $filter callback,
elements - for each iteration that returns true
findOne(callable $filter): mixedFinds the first element by using the $filter callback,
when $filter will return the first true
findValue(mixed $value, bool $strict): int|null|stringgroup(callable $callback): php\util\Floweach(callable $callback): intIterates elements.
It will break if $callback returns false strongly
eachSlice(int $sliceSize, callable $callback, bool $withKeys): intIterates elements as slices (that are passing as arrays to $callback).
It will break if $callback returns false strongly
map(callable $callback): php\util\FlowIterates elements and returns a new flow of the result Example::
$newFlow = Flow::of([1,2,3])->map(function($el){ return $el * 10 }); // the new flow will contain 10, 20 and 30
keys(): php\util\FlowCreate a new flow by using the keys of the current flow
skip(int $n): php\util\FlowSkips $n elements in the current collection
limit(int $count): php\util\FlowLimits collection with $count
reduce(callable $callback): intIterates elements and gets a result of this operation It can be used for calculate some results, for example::
// calculates a sum of elements $sum = .. ->reduce(function($result, $el){ $result = $result + $el });
max(callable|null $comparator): mixedGet max of elements.
min(callable|null $comparator): mixedGet min of elements.
avg(): int|floatGet avg number of elements.
median(callable|null $comparator): mixedGet median of elements.
numMedian(callable|null $comparator): int|floatsum(): int|floatGet sum of elements.
concat(): stringGet concatenation of all elements.
sort(callable $comparator): arraySort the last result of the flow, also see: php\\lib\\items::sort()
.. note:: use the withKeys() method to save keys
sortByKeys(callable $comparator): arrayThe same method as sort() only based on keys insteadof values
.. note:: use the withKeys() method to save keys
toArray([ bool|null $withKeys): arrayConvert elements to an array
.. note:: use the withKeys() method to save keys
toMap(): arrayConvert element to an array with keys.
toString(string $separator): stringJoin elements to a string similar to implode() in PHP
count(): intcurrent(): mixednext(): voidkey(): mixedvalid(): boolrewind(): void__clone(): voidClass is immutable, the disallowed clone method