flow php

Example: When odd

Topic: Transformations


Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_sequence_number, lit, ref, to_stream, when};

require __DIR__ . '/../../../autoload.php';

data_frame()
    ->read(from_sequence_number('number', 1, 100))
    ->collect()
    ->withEntry(
        'type',
        when(
            ref('number')->isOdd(),
            then: lit('odd'),
            else: lit('even')
        )
    )
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+--------+------+
| number | type |
+--------+------+
|      1 |  odd |
|      2 | even |
|      3 |  odd |
|      4 | even |
|      5 |  odd |
|      6 | even |
|      7 |  odd |
|      8 | even |
|      9 |  odd |
|     10 | even |
|     11 |  odd |
|     12 | even |
|     13 |  odd |
|     14 | even |
|     15 |  odd |
|     16 | even |
|     17 |  odd |
|     18 | even |
|     19 |  odd |
|     20 | even |
|     21 |  odd |
|     22 | even |
|     23 |  odd |
|     24 | even |
|     25 |  odd |
|     26 | even |
|     27 |  odd |
|     28 | even |
|     29 |  odd |
|     30 | even |
|     31 |  odd |
|     32 | even |
|     33 |  odd |
|     34 | even |
|     35 |  odd |
|     36 | even |
|     37 |  odd |
|     38 | even |
|     39 |  odd |
|     40 | even |
|     41 |  odd |
|     42 | even |
|     43 |  odd |
|     44 | even |
|     45 |  odd |
|     46 | even |
|     47 |  odd |
|     48 | even |
|     49 |  odd |
|     50 | even |
|     51 |  odd |
|     52 | even |
|     53 |  odd |
|     54 | even |
|     55 |  odd |
|     56 | even |
|     57 |  odd |
|     58 | even |
|     59 |  odd |
|     60 | even |
|     61 |  odd |
|     62 | even |
|     63 |  odd |
|     64 | even |
|     65 |  odd |
|     66 | even |
|     67 |  odd |
|     68 | even |
|     69 |  odd |
|     70 | even |
|     71 |  odd |
|     72 | even |
|     73 |  odd |
|     74 | even |
|     75 |  odd |
|     76 | even |
|     77 |  odd |
|     78 | even |
|     79 |  odd |
|     80 | even |
|     81 |  odd |
|     82 | even |
|     83 |  odd |
|     84 | even |
|     85 |  odd |
|     86 | even |
|     87 |  odd |
|     88 | even |
|     89 |  odd |
|     90 | even |
|     91 |  odd |
|     92 | even |
|     93 |  odd |
|     94 | even |
|     95 |  odd |
|     96 | even |
|     97 |  odd |
|     98 | even |
|     99 |  odd |
|    100 | even |
+--------+------+
100 rows

Contributors

Join us on GitHub external resource
scroll back to top