proposal-built-in-modules
proposal-built-in-modules copied to clipboard
Auto-currying
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
Love this idea.
Curry, Compose, Pipe, and Combine would be amazingly useful additions tbh
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)
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
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 🤪️.