The "Pipeline" Operator - Making multiple function calls look great

Gilbert B Garza gilbertbgarza at gmail.com
Tue Nov 10 17:38:03 UTC 2015


>
> Although, I believe the ( ) are like "representations OF a function call".
> I felt like if there was something missing there!
> What about something like this, then?
>


// exclaim(capitalize(doubleSay("hello")));
> doubleSay("hello") |> capitalize |> exclaim;


Felipe: That is perfectly valid syntax under the current proposal, and you
are free to choose to write your code in that way :)

On Tue, Nov 10, 2015 at 11:15 AM, Felipe Nascimento de Moura <
felipenmoura at gmail.com> wrote:

> I find this interesting, indeed.
>
>
> For me, this feels like "calling doubleSay", then passing its results for
> capitalize, and its result to exclaim.
>
> Some interesting implementations coupd be done with async await, I believe!
>
>
>
>
> On Tue, Nov 10, 2015 at 3:09 PM, Alexander Fritze <alex at onilabs.com>
> wrote:
>
>> 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
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
>
> --
> *Felipe N. Moura*
> Senior Web Developer and System Analyst.
>
> Website:  http://felipenmoura.com
> Twitter:    @felipenmoura <http://twitter.com/felipenmoura>
> LinkedIn: http://goo.gl/qGmq
>
> Meet some of my projects:
> BrazilJS Conference <http://braziljs.com.br/>  |  BrazilJS Foundation
> <http://braziljs.org>
> ---------------------------------
> *Changing  the  world*  is the least I expect from  myself!
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151110/49dbd8bf/attachment-0001.html>


More information about the es-discuss mailing list