proposal-functional-operators icon indicating copy to clipboard operation
proposal-functional-operators copied to clipboard

Python style solution?

Open hax opened this issue 3 years ago • 4 comments

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 Math is ok for most operators, and we already have Math.pow correspond to **, but some operators like in, instanceof, ?? seems not fit for Math. Other options are Function.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 to x=>x+1.

hax avatar Apr 28 '22 03:04 hax

or a template tag:

const sum = array.reduce(Math.operator`+`);

or a function:

const sum = array.reduce(Math.operator('+'));

ljharb avatar Apr 28 '22 04:04 ljharb

@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 :-)

hax avatar May 01 '22 16:05 hax

Slightly less verbose than just being a function :-p

ljharb avatar May 01 '22 16:05 ljharb