The "Pipeline" Operator - Making multiple function calls look great
Alexander Fritze
alex at onilabs.com
Tue Nov 10 17:09:45 UTC 2015
I agree 100% that this would make a great addition to JS!
We've had the pipeline operator in Stratified JS for a while - see
https://conductance.io/reference#sjs:%23language/syntax::double-dot -
, and in our experience it makes a lot of code much more readable.
We call it '..' (the 'doubledot operator') instead of '|>' - IMO this
feels more aligned with existing JS syntax and less jarring than '|>'.
On Tue, Nov 10, 2015 at 10:54 AM, Gilbert B Garza
<gilbertbgarza at gmail.com> wrote:
> Hello, I'm a JavaScript programmer and instructor who loves functional
> programming and writing concise, readable code. I think in general
> JavaScript supports programming in a functional style quite well. However,
> there is one small missing piece that I miss from other FP languages: the
> simple-yet-useful pipeline operator.
>
> Similar to F#, Elixir, Elm, and other FP languages, the pipeline operator |>
> helps make multiple function invocations more readable. Basically,
> `sqrt(64)` is equivalent to `64 |> sqrt`. For example, given the following
> functions:
>
> ```js
> function doubleSay (str) { return str + ", " + str; }
> function capitalize (str) { return str[0].toUpperCase() + str.substring(1);
> }
> function exclaim (str) { return str + '!'; }
> ```
>
> you could use the pipeline operator to expand your invocations for
> readability:
>
> ```js
> // old way:
> // var result = exclaim(capitalize(doubleSay("hello")));
>
> // new way:
> var result = "hello"
> |> doubleSay
> |> capitalize
> |> exclaim;
>
> // or, if you like one-liners:
> var result = "hello" |> doubleSay |> capitalize |> exclaim
>
> result //=> "Hello, hello!"
> ```
>
> You can see a few more examples, including an advanced example with
> Promises, here: https://github.com/mindeavor/ES7-pipeline-operator
>
> I'm inclined to think this feature is small and straight-forward to
> implement. Other than the operator, there are no new semantics. The syntax
> transformation is simple, and all existing code would remain unaffected.
>
> Although small, this feature increases the expressiveness of JavaScript
> significantly by opening more API design possibilities. You can see this in
> the link I included above.
>
> Thanks for reading. Any thoughts or comments?
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
--
Alexander Fritze
http://onilabs.com
More information about the es-discuss
mailing list