paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 84 forks source link

Use fj-curry instead of Ramda curry. #59

Open RangerMauve opened 9 years ago

RangerMauve commented 9 years ago

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.

avesus commented 9 years ago

:+1:

paldepind commented 9 years ago

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.

RangerMauve commented 9 years ago

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. :/

RangerMauve commented 9 years ago

Would a PR for this be welcome?

StreetStrider commented 9 years ago

@RangerMauve, other modules can rely on this.

kwijibo commented 9 years ago

@RangerMauve http://rollup.js/ could be a way to selectively import only R.curry

RangerMauve commented 9 years ago

@kwijibo Thanks for the link, however flyd already just imports curry. I just like it when only the code needed is installed at all.

monfera commented 8 years ago

@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.

pygy commented 8 years ago

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.