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:
EmptyStreamwhich stand for empty stream (Emptyis reserved keyword in PHP)Conswhich stand for a stream with one or more elements (consisting of a head and tail)
Construction#
Because a stream can be infinite there are several other interesting ways to create it:
Collectors#
Common use case for stream is to iterate over some kind of source (potentially infinite) filter it and collect values to list:
Examples#
Find the sum of the first ten squares of even numbers (from php 7.4):