contour-terminal / endo

7 stars 0 forks source link

support data pipelining `|>` #15

Open christianparpart opened 6 months ago

christianparpart commented 6 months ago

The |> operator (inspired from F# / Ocaml) would have the left hand side's stdout split into lines, and feed each line from left into a newly spawned process into the right.

echo "A \n B \n C" |> echo

would result into 3 echo processes, each receiving the parameter A, B, and C individually, resulting into the output:

A
B
C

That can then be chained into multiple, like in this example:

get_numbers |> times_two |> plus_one |> echo

where get_numbers prints N numbers (one per line), and times_two gets invoked N times and multiplies each number by 2, and plus_one gets invoked N times and adds 1 over the intermediate result, to then have each result printed, once per line.

notes

this is similar to what xargs -n1 does, but this syntactic shougar would not need to spawn the process xargs and have it more platform independant (e.g. xargs would not exist on Windows).

The syntax could make the code look more readable / maintainable as well.