OperatorsΒΆ
Operator, also known as intermediate operations, are functions which allows you
to transform, combine and/or control streams of data. Each time when you apply
operator on stream, you get new instance of
RunOpenCode\Component\Dataset\Stream. That means that you can chain
operators describing in declarative way process of data processing from the
start to the end on each individual streamed record.
As already mentioned, applying operators to a stream is essentially a composition of functions, presented in a more developer-friendly way:
1<?php
2
3use RunOpenCode\Component\Dataset\Stream;
4
5new Stream(/* ... */)
6 ->map(/* ... */)
7 ->tap(/* ... */)
8 ->takeUntil(/* ... */)
9 ->finally(/* ... */);
which maps to:
1<?php
2
3finally(takeUntil(tap(map(new Stream(...)))));
Library provides you with set of common operators which you may use out-of-the box, as well as possibility to create and use your own operators.
Each operator may be applied in object-oriented manner (as presented in example above). If you are using PHP 8.5 or higher, you can leverage the pipe operator and write stream-processing code in a functional style.
1<?php
2
3use function RunOpenCode\Component\Dataset\stream;
4use function RunOpenCode\Component\Dataset\map;
5use function RunOpenCode\Component\Dataset\tap;
6use function RunOpenCode\Component\Dataset\takeUntil;
7use function RunOpenCode\Component\Dataset\finally;
8
9stream(/* ... */)
10 |> map(/* ... */)
11 |> tap(/* ... */)
12 |> takeUntil(/* ... */)
13 |> finally(/* ... */);