Curried functions
Isiah Meadows
isiahmeadows at gmail.com
Sun Oct 18 04:51:31 UTC 2015
Here's what I see as what's being proposed here. Correct me if I'm wrong.
Function currying means it can be partially applied when only given
some of its arguments. For example:
```js
var add = _.curry(function (a, b, c) { return a + b + c })
add(1)(2)(3) === 6
add(1, 2)(3) === 6
add(1)(2, 3) === 6
add(1, 2, 3) === 6
var add3 = add(3)
add3(2, 1) === 6
add3(2)(1) === 6
var add5 = add(3, 2)
add5(1) === 6
```
That's exactly the behavior in Underscore's and Lodash's `_.curry()`.
And that's exactly what I (and most others) would expect.
-----
As for Yongxu Ren's idea, that wouldn't be very performant, as engines
have never been made to handle very highly nested closures very well.
That's a great idea in statically typed, mostly pure functional
languages (Haskell and OCaml both do that), but in languages that
accept multiple arguments for a single function like Java or Python,
it's hard to optimize at all. JavaScript is no exception. It's also
cheaper to do a function length comparison than an unavoidable
`IsCallable` check for each argument passed (which that would
require).
On Fri, Oct 16, 2015 at 11:41 PM, liorean <liorean at gmail.com> wrote:
>
> On 17 Oct 2015 04:48, "Yongxu Ren" <renyongxu at gmail.com> wrote:
>>
>> How about we use a different approach?
>>
>> Instead of allowing ```a(b)(c)``` for ```let a = (b,c) => b+c;```,
>>
>> Maybe we can use ```a(b,c)``` for ```let a = b => c => b + c```?
>
> Would that do any of the useful stuff you could reasonably want either
> currying or partial application for, though? I mean the main use is that it
> allows us to do
> let d=a(b);
> ...
> d(c);
> And I don't really see how your desugaring a single multiple argument
> application into several single parameter curried functions allows that
> usefulness.
>
> It's the multiple applications to fill single parameter list thing that is
> the most useful part of it, mostly as it allows caching the closure at any
> step for reuse, not single application to fill multiple sequential single
> parameter lists. Also, consider rest parameters, defaults etc. and whether
> doing what you want for the simple example case would have weird or possibly
> ambiguous meaning. Also, what happens to this value if the functions are old
> style and not new style?
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
--
Isiah Meadows
More information about the es-discuss
mailing list