Function composition syntax

Isiah Meadows isiahmeadows at gmail.com
Thu Sep 29 02:19:39 UTC 2016


Inline.

On Wed, Sep 7, 2016, 10:44 Bergi <a.d.bergi at web.de> wrote:

> Hi,
> interesting proposal!
>
> > Here's what I propose: a new infix operator `>=>` (operator and direction
> > can change) for composing two functions.
>
> Sweet, reminds me of Kleisli composition in Haskell
>
> http://hackage.haskell.org/package/base-4.9.0.0/docs/Control-Monad.html#v:-62--61--62-
> (which does something different to functions though).
>

That was an inspiration, but that adds to the list of reasons why I would
be okay with a different operator.


> > 2. It allows engines to statically optimize functions in the middle
> (avoid
> > an extra function allocation), like with `f >=> x => console.log("x:" +
> x)`.
>
> I don't understand that one. Wouldn't `x => console.log("x:" + f(x))` be
> optimised better (and also be easier to read)?
>

Yes, but it was a trivial example just for the sake of example.


> > 3. It can simplify the internal model some to deal with a binary pair
> instead
> > of an array, especially when pipelining gets involved.
> > 4. Composition isn't usually combined as a function in JS.
>
> Can you clarify what you mean with this?
>

Things like `compose(compose, compose)` aren't frequently used in JS
(unlike Haskell's `(.) . (.)`). I almost included that example, but I
couldn't get a wording I liked.


> My questions would be
> * What precedence would the operator have? Clearly something between
> member access and assignment, but what exactly?
>    Particularly interesting cases:
>    f >=> g (x)
>    f >=> p ? g : h
>    f >=> x => x >=> g
>

I intentionally left precedence out (other than implying it's chainable
left to right), to avoid complicating the initial presentation.

* Do we also need a partial application operator to make this syntax useful?
>    I guess the discussions from
> https://github.com/tc39/proposal-bind-operator/issues/35 and
> https://github.com/tc39/proposal-bind-operator/issues/26 are relevant
> here.
>

It would help, but no, that's not required. Arrow functions can help in the
meantime, and may be more readable, and sometimes required, in some cases,
since it's more explicit.

(It's come up before, independently:
https://esdiscuss.org/topic/syntax-sugar-for-partial-application)


> Kind regards,
>   Bergi
> _______________________________________________
> 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/20160929/fad919c3/attachment.html>


More information about the es-discuss mailing list