Skip to content

Latest commit

 

History

History
503 lines (362 loc) · 9.75 KB

File metadata and controls

503 lines (362 loc) · 9.75 KB

Flow

  • 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


Static Methods

  • Flow ::ofEmpty()
  • Flow ::of() - Creates a new flow for an array of Iterator
  • Flow ::ofRange() - Creates a new flow for a number range
  • Flow ::ofString() - Creates a new flow for the string
  • Flow ::ofStream() - Creates a new flow for the Stream object

Methods

  • ->__construct() - Create a new flow, you can also use of() 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 as sort() 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 to implode() in PHP
  • ->count()
  • ->current()
  • ->next()
  • ->key()
  • ->valid()
  • ->rewind()
  • ->__clone() - Class is immutable, the disallowed clone method

Static Methods

ofEmpty()

Flow::ofEmpty(): php\util\Flow

of()

Flow::of(iterable $collection): php\util\Flow

Creates a new flow for an array of Iterator


ofRange()

Flow::ofRange(int $from, int $to, int $step): php\util\Flow

Creates a new flow for a number range


ofString()

Flow::ofString(string $string, int $chunkSize): php\util\Flow

Creates a new flow for the string


ofStream()

Flow::ofStream(php\io\Stream $stream, int $chunkSize): php\util\Flow

Creates a new flow for the Stream object


Methods

__construct()

__construct(iterable $collection): void

Create a new flow, you can also use of() method


withKeys()

withKeys(): php\util\Flow

Enables to save keys for the next operation


onlyKeys()

onlyKeys(iterable $keys, bool $ignoreCase): php\util\Flow

append()

append(iterable $collection): php\util\Flow

Appends a new collection to the current flow, do not remember that you can pass a flow to this method


anyMatch()

anyMatch(callable $predicate): bool

Returns 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()

allMatch(callable $predicate): bool

Returns 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()

noneMatch(callable $predicate): bool

Returns 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()

find(callable $filter): php\util\Flow

Finds elements by using the $filter callback, elements - for each iteration that returns true


findOne()

findOne(callable $filter): mixed

Finds the first element by using the $filter callback, when $filter will return the first true


findValue()

findValue(mixed $value, bool $strict): int|null|string

group()

group(callable $callback): php\util\Flow

each()

each(callable $callback): int

Iterates elements. It will break if $callback returns false strongly


eachSlice()

eachSlice(int $sliceSize, callable $callback, bool $withKeys): int

Iterates elements as slices (that are passing as arrays to $callback). It will break if $callback returns false strongly


map()

map(callable $callback): php\util\Flow

Iterates 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()

keys(): php\util\Flow

Create a new flow by using the keys of the current flow


skip()

skip(int $n): php\util\Flow

Skips $n elements in the current collection


limit()

limit(int $count): php\util\Flow

Limits collection with $count


reduce()

reduce(callable $callback): int

Iterates 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()

max(callable|null $comparator): mixed

Get max of elements.


min()

min(callable|null $comparator): mixed

Get min of elements.


avg()

avg(): int|float

Get avg number of elements.


median()

median(callable|null $comparator): mixed

Get median of elements.


numMedian()

numMedian(callable|null $comparator): int|float

sum()

sum(): int|float

Get sum of elements.


concat()

concat(): string

Get concatenation of all elements.


sort()

sort(callable $comparator): array

Sort the last result of the flow, also see: php\\lib\\items::sort()

.. note:: use the withKeys() method to save keys


sortByKeys()

sortByKeys(callable $comparator): array

The same method as sort() only based on keys insteadof values

.. note:: use the withKeys() method to save keys


toArray()

toArray([ bool|null $withKeys): array

Convert elements to an array

.. note:: use the withKeys() method to save keys


toMap()

toMap(): array

Convert element to an array with keys.


toString()

toString(string $separator): string

Join elements to a string similar to implode() in PHP


count()

count(): int

current()

current(): mixed

next()

next(): void

key()

key(): mixed

valid()

valid(): bool

rewind()

rewind(): void

__clone()

__clone(): void

Class is immutable, the disallowed clone method