Mixing grammars

Mike Samuel mikesamuel at gmail.com
Thu Sep 7 16:30:22 UTC 2017


On Thu, Sep 7, 2017 at 10:44 AM, Jérémy Judéaux
<jeremy.judeaux at volune.net> wrote:
> ```js
>
> import _ from 'lodash-for-chaining';
> import $ from 'jquery-for-chaining';
> import BB from 'bluebird-for-chaining';
> import W from 'whenjs-for-chaining';
>
> const toArray = iterable => [...iterable];
> new Set([1,2,3,4,5]).entries()
>     |> _.filter(x=>x%2)
>     |> _.map(x=>x+1)
>     |> toArray
>     |> console.log;
>
> document.querySelectorAll('a')
>     |> $.show
>     |> $.css('width', '100%');
>
> // iteroperability
> Promise.resolve([1,2,3])
>     |> W.spread((a,b,c) => a+b+c)
>     |> BB.finally(() => console.log('finally'));
>
> // personnal expectations, give developers the oportunity to solve
> // cancellation without waiting another ES feature
> fetch(url, {cancelToken: cancellableOperation.token})
>     |> cancellableOperation.then(res => res.json())
>     |> cancellableOperation.then(res => console.log(res.id));
>
> ```

These are nice examples.  Is there a side-by-side comparison of the
equivalent code without function chaining?
For example, unless I've mistranslated:

new Set([1,2,3,4,5]).entries()
     |> _.filter(x=>x%2)
     |> _.map(x=>x+1)
     |> toArray
     |> console.log;

vs

console.log(
    toArray(
        new Set([1,2,3,4,5]).entries()
           .filter(x=>x%2)
           .map(x=>x+1)));



I ask because I don't understand how pipelining makes operations easier.
The proposal says "and provides a practical alternative to extending
built-in prototypes" which seems like a nice thing to avoid but I'm
not clear on how that happens.

It seems to maybe enable more literate programming.
"We take the widget, bedazzle it, and put it on the fridge"
can be directly translated to
    widget |> bedazzle |> fridge
but
    fridge(bedazzle(widget))
maps to English "The fridge gets a bedazzled widget."
Maybe we should prefer the former since it introduce terms in the
order they're operated upon.
Is that part of the appeal?

I see https://github.com/tc39/proposal-pipeline-operator/wiki/Example-Use-Cases
which has some use cases, but some of those are a stretch, and they
don't seem to relate to avoiding extending built-in prototypes since
the Before code doesn't seem to require extensions.

For example,

"""
Usage with const

// Before
let items = base64ToJSON(response.data.content)
items = Array.isArray(items) ? items : [items]

// After
const items = base64ToJSON(response.data.content)
              |> x => Array.isArray(x) ? x : [x]
"""

shows code that nicely avoids a reassignment, but the unpipelined

    const items = (x => Array.isArray(x) ? x : [x])(
         base64ToJSON(response.data.content))

also avoids reassignment without pipelining.


More information about the es-discuss mailing list