Mixing grammars

Jérémy Judéaux jeremy.judeaux at volune.net
Thu Sep 7 14:44:13 UTC 2017


> I tend to agree with peter that function-composition and pipe-operators are likely footguns
> that don't solve anything new, and that you should be careful what you wish for.

I would like to give my opinion about how the `|>` operator solves something new. It’s not the function composition, which is a nice bonus, but mostly already possible with a custom function. It’s function chaining.

In the current state of ES, IMO, function chaining is not possible in a maintainable way. I mean without wrapping/unwrapping objects or multilines parentheses blocks, or worse. The `|>` operator (or, if it is abandoned, I’ll put my hopes on the binding operator) allows function chaining.
The benefits I see from function chaining (and so from the `|>` operator):
- Better use of iterables, and probably also streams or observables or promises
- More interoperability of libraries
- More opportunities for developers to solve problems

Let me give some examples of what we may see with function chaining:
```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));
```

And I don’t think these examples are using a very different paradigm than all the ES code written so far, or would be that difficult to apprehend for new developers.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170907/14a45e7e/attachment.html>


More information about the es-discuss mailing list