Open RangerMauve opened 9 years ago
:+1:
The biggest difference in code size seems to be that fj-curry does not return functions with the correct length. So curry((a, b, c) => foo).length === 0
. But it should be 3. It does not seem nice but I am not sure if it really matters.
That's true. Does flyd rely on a proper length property anywhere? I mostly like that one wouldn't have to have all of Ramda installed just for the one curry function. It'd be nice if they split things up into separate modules in the future. :/
Would a PR for this be welcome?
@RangerMauve, other modules can rely on this.
@RangerMauve http://rollup.js/ could be a way to selectively import only R.curry
@kwijibo Thanks for the link, however flyd already just imports curry. I just like it when only the code needed is installed at all.
@RangerMauve
| Does flyd rely on a proper length property anywhere?
Some Ramda functions depend on the length property. So the approach may cause an issue if in a mixed flyd/ramda setting, some fj-curried function gets eventually passed to some ramda function that relies on it. So in general it's good to preserve it.
The following pattern allows to curry functions that you define yourself:
function innerCurry(args, f, context) {
return f.bind.apply(f, [context || this].concat([].slice.call(args)))
//there may be a more efficient way to build that array
}
function add(a,b,c) {
// that line is a bit unusual, but it offers maximal gzippability if the cyrrification pattern
// is repeated in many functions.
if (3 > arguments.length) return innerCurry(arguments, add)
return a + b + c
}
It as light as can be, and the length
is preserved, so that add(1).length === 2
.
The main drawback is that you can't implement curryN
on top of it, and you provide ramda's version publicly ATM...
You could provide a short curryN
that preserves the length
on top of the new Function ()
constructor, but it will not work in restricted environment, and may be slow.
Edit: I accidentaly the last sentence.
Ramda's curry seems to have a bit more code to it compared to fj-curry which is more minimal and has the same features.