Software Design/Transform data with pipeline operator
Appearance
Pipeline operator sends the expression from the left-hand side to the function on the right-hard side. It appears in Elixir[1], Elm,[2] F#[3], Haskell (via a third-party library)[4] and R [5].
Examples
[edit | edit source]In Elixir:
"The quick brown fox" |> String.upcase() |> String.split() # Result: ["THE", "QUICK", "BROWN", "FOX"]
Why
[edit | edit source]The written sequence of functions corresponds to how are applied one after another, which makes the code more natural to read.
Why not
[edit | edit source]There are many situations where the pipeline operator is not applicable: for example when the expression should be sent to a function with several parameters as a non-last parameter. Using several different function application forms can be seen as inconsistency: it's against the practice to always use the same way to do a thing.
Related
[edit | edit source]Sources
[edit | edit source]- David Thomas; Andrew Hunt (2019). The Pragmatic Programmer (20th Anniversary ed.). Addison-Wesley Professional. ISBN 978-0135957059.