The difference between currying and partial application is that you can call a curried function f with 1 argument that needs N arguments and the return value is a function f_1 that needs N-1 arguments and when called again with 1 argument will return a function f_2 that takes N-2 arguments, etc. until some point where N-K=0 and it returns a value instead of another function.
It's effectively a transform that wraps a function f(a, b, c) with something like this: function (a) { return function (b) { return function (c) { return f(a, b, c); } } } Partial application explicitly takes a function with N arguments and returns a function that takes N-K arguments but the resulting function behaves the same as any other function in JS without that "magic". I'm not a real big fan of currying in languages where it's not built-in. It's easy to make a mistake by calling a function with too few arguments and you get a harder to track down bug. It also doesn't work well with languages that have default arguments or the equivalent (e.g. using the arguments object) because you don't know exactly when to stop currying. On Wed, Dec 17, 2008 at 11:31 PM, Per Cederberg <cederb...@gmail.com> wrote: > > The names "curry" and "uncurry" were a bit confusing to me, so it took > me a while to understand these two functions... > > http://en.wikipedia.org/wiki/Currying > > To me (and probably other non-Haskell users) the names imply the same > thing as bind or partial. It's a confusing world... :-( > > In JavaScript, I think plain apply + MochiKit.Base.bind does the same thing: > > var test = [ [10, 1], [20, 2], [30, 3] ]; > var addArray = bind("apply", operator.add, null); > assertEqual(map(addArray, test), [11, 22, 33]); > > It's no beauty, so perhaps this particular variant of bind merits an > alias? Uncurrying is just the same as the built-in apply function, so > that seems unnecessary. > > Cheers, > > /Per > > On Wed, Dec 17, 2008 at 5:22 PM, Arnar Birgisson <arna...@gmail.com> wrote: >> >> One thing I think could be useful is to port Haskell's curry and >> uncurry. This is basically a convenience method for (un)wrapping an >> .apply on a function object: >> >> function curry(f) { >> return function () { >> // first convert arguments to a regular array >> var args = Array.prototype.slice.call(arguments); >> return f(args); >> } >> } >> >> function uncurry(f) { >> return function (args) { >> return f.apply(this, args); >> } >> } >> >> Example use: >> test = [ [10, 1], >> [20, 2], >> [30, 3] ]; >> >> assertEqual(map(uncurry(operator.plus), test), [11, 22, 33]); >> >> // assume join is a function that takes a list and returns a string >> // with the elements joined with some delimiter >> >> f = curry(partial(join, _, ", ")) >> assert(f("Bond", "James Bond") == "Bond, James Bond") >> >> Does anyone else think this could be useful? What module would it fit? >> Base already has a lot of functional stuff (compose, partial, map & >> friends) - I'm wondering if it fits there or if all the functional >> stuff should be in a seperate module MochiKit.Functional - as Python >> seems to be heading. >> >> cheers, >> Arnar >> >> > >> > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~----------~----~----~----~------~----~------~--~---