I would like to see a function composition operator make it into the language. Currently, there is:
- Lodash: `_.flow` and `_.flowRight` (lodash/fp alias: `_.compose` and `_.composeRight`) - Underscore: `_.compose` - Ramda: `R.compose` - Tons of npm modules: https://www.npmjs.com/search?q=function+composition - Numerous manual implementations Function composition could be far more efficiently implemented in the engine, in ways not possible at the language level: 1. They can create pipelines to optimize multiple composition chains together. 2. They can avoid most of the closure allocation cost internally. 3. The returned functions can internally use a separate call path to avoid some of the [[Call]] boilerplate when called and when calling the functions themselves (you don't need to verify twice). Here's what I propose: a new infix operator `>=>` (operator and direction can change) for composing two functions. It would do the following: 1. Verify both operands are callable. 2. Create a callable-only function that calls its left operand with the original arguments and `this`, then calling its right operand with the result and the same `this`. 3. Sets its length to the left operand. 4. Return the new function. The reason I suggested an operator instead of a function: 1. Fewer parentheses is always a plus. 2. It allows engines to statically optimize functions in the middle (avoid an extra function allocation), like with `f >=> x => console.log("x:" + x)`. 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. What do you all think?
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

