The "Pipeline" Operator - Making multiple function calls look great
Gilbert B Garza
gilbertbgarza at gmail.com
Tue Nov 10 17:36:13 UTC 2015
Kevin: Ah yes, I have studied function-bind, but I have two issues with it:
- I don't like the requirement to use the keyword `this` to compose
functions. JS already has many features to support the keyword `this`:
prototypes, method invocations, function binding, arrow functions, and
probably others. I prefer a feature that assists the other side of the
spectrum.
- The fact that there are new semantics to what looks like a normal
function call (e.g. `->map(...)`) doesn't set well with me. You could argue
that it's something to get used to. Even in that case, I would expect the
first argument I give to `map` to stay the first argument.
With the pipeline operator, partial application is left to the developer.
They can choose to use arrow functions, or to curry their functions. I
think this is the best option since it keeps things simple (no new
semantics), and remains readable – see the "Function with Multiple
Arguments" section in the GitHub link
https://github.com/mindeavor/ES7-pipeline-operator
On Tue, Nov 10, 2015 at 11:15 AM, Kevin Smith <zenparsing at gmail.com> wrote:
> Hi Gilbert,
>
> Have you seen https://github.com/zenparsing/es-function-bind/ ? It's a
> function bind-syntax proposal which covers some of the same use cases
> (although by binding the `this` parameter instead of passing the first
> arg). We've also explored some alternatives more closely aligned with your
> proposal. Check out the discussion here for the latest state:
> https://github.com/zenparsing/es-function-bind/issues/26#issuecomment-154237803 and
> feel free to comment!
>
> On Tue, Nov 10, 2015 at 11: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
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151110/3e2c4269/attachment.html>
More information about the es-discuss
mailing list