Stream
Stream
is lazy sequence of elements which may be infinitely long.
Stream
is composed of a head and a tail. Head contains first value, tail is lazy evaluated - subsequent values are calculated as needed.
The stream has two implementations available:
EmptyStream
which stand for empty stream (Empty
is reserved keyword in PHP)Cons
which stand for a stream with one or more elements (consisting of a head and tail)
#
ConstructionBecause a stream can be infinite there are several other interesting ways to create it:
#
CollectorsCommon use case for stream is to iterate over some kind of source (potentially infinite) filter it and collect values to list:
#
ExamplesFind the sum of the first ten squares of even numbers (from php 7.4):