proposal-built-in-modules icon indicating copy to clipboard operation
proposal-built-in-modules copied to clipboard

Auto-currying

Open harrysolovay opened this issue 6 years ago • 4 comments

Functional patterns in JavaScript are COOOOL. Depending on whether proposals such as these get approved, it might be a good idea to boil more fp into a standard library. Aka:

import { len, map } from "std:builtins";

const ar = [1, 2, 3];
const st = new Set([4, 5, 6, 7]);
const mp = new Map([["e", 8], ["n", 9], ["z", 0], ["o", 1], ["t", 2]);

len(ar); // => 3
len(st); // => 4
len(mp); // => 4

+ const mapDoubled = map((val, idx) => val * 2); // fn
- map(ar, (val, idx) => val * 2); // => Array {2, 4, 6}
+ mapDoubled(ar) // => Array {2, 4, 6}
- map(st, (val, idx) => val * 2); // => Set {8, 10, 12, 14}
+ mapDoubled(st) // => Set {8, 10, 12, 14}
- map(mp, (val, key) => val * 2); // => Map { e: 16, n: 18, z: 0, o: 2 }
+ mapDoubled(mp) // => Map { e: 16, n: 18, z: 0, o: 2 }

Sorry if this is redundant... you've probably thought this out. I'm just a huge fan of libraries like Ramda and would love to see more fp in JavaScript's future.

Kind regards,

Harry

harrysolovay avatar Nov 29 '18 15:11 harrysolovay

Love this idea.

Curry, Compose, Pipe, and Combine would be amazingly useful additions tbh

abritinthebay avatar Dec 18 '18 20:12 abritinthebay

this synergizes nicely with any tc39/proposal-pipeline-operator.

import { filter, map } from "std:builtins";

const loggedInUsers = users
  |> filter(u => u.loggedIn)
  |> map(u => u.userName)

silesky avatar Dec 21 '18 22:12 silesky

So to be clear, this could only work for non-varadic functions. So we'd be saying all builtin functions are non-varadic. Which is fine. Just I think needs to be explicitly stated

togakangaroo avatar Jun 13 '19 20:06 togakangaroo

Wouldn't this mean we're basically exporting a list of all the functions already available on the prototype of different classes? What I think would be cool is if we just had a syntax shorthand for picking off a function from a prototype and calling it.

i.e.

Array::map(myArray, x => x + 1)
// is the same as
Array.prototype.map.call(myArray, x => x + 1)

Then, we could achieve this same functional idea, without duplicating all of these existing functions in a separate stdlib module.

const loggedInUsers = users
  |> Array::filter(u => u.loggedIn)
  |> Array::map(u => u.userName)

Though now I'm getting pretty off-topic from the objectives of this proposal 🤪️.

theScottyJam avatar Jul 07 '21 19:07 theScottyJam