proposal-functional-operators
proposal-functional-operators copied to clipboard
Python style solution?
Python provides operator module, if JS adopt such style, it would be look like:
import {plus} from "@operator" // built-in module
let sum = array.reduce(plus)
Even built-in modules are controversial , put them on some global namespaces is always an option:
let sum = array.reduce(Math.plus)
Pros:
- No extra syntax.
Cons:
- Need remember names, eg. "plus" or "add" or "sum"? Some operators like
??may have trouble to find simple name. - Which namespace should we use? It seems
Mathis ok for most operators, and we already haveMath.powcorrespond to**, but some operators likein,instanceof,??seems not fit forMath. Other options areFunction.xxx, or introduce a new namespace:Operator.xxx. - Do not support partial application by itself. With partial application proposal,
Math.plus~(?,1)is ok, but seems have no benefit compare tox=>x+1.
or a template tag:
const sum = array.reduce(Math.operator`+`);
or a function:
const sum = array.reduce(Math.operator('+'));
@ljharb Template tag is an interesting idea, though it's even much verbose ... 😅
But u inspired me, it could also be function.plus or do.plus (meta method :-)
Slightly less verbose than just being a function :-p