Boost logo

Boost :

Subject: Re: [boost] painless currying
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2011-08-24 23:19:08


On Wed, Aug 24, 2011 at 6:41 PM, Joshua Juran <jjuran_at_[hidden]> wrote:

> On Aug 24, 2011, at 1:38 PM, Dave Abrahams wrote:
>
> I mean this, for a ternary function f:
>>
>> f(x) => doesn't call f
>> f(x)(y) => doesn't call f
>> f(x)(y)(z) => calls f
>>
>> That last step looks asymmetric to me.
>>
>> In a lazy language, f(x)(y)(z) *doesn't* call f... until you actually
>> use the result for something... which is more consistent-looking.
>>
>> I suppose the symmetrical non-lazy version looks like:
>>
>> f(x) => doesn't call f
>> f(x)(y) => doesn't call f
>> f(x)(y)(z) => doesn't call f
>> f(x)(y)(z)() => calls f
>>
>
> What about using [] for currying and () for calling?
>
> f[x] => returns binary function
> f[x][y] => returns unary function
> f[x][y][z] => returns nullary function
>
> All of these have the same effect:
>
> f(x, y, z)
> f[x](y, z)
> f[x][y](z)
> f[x][y][z]()
>

My vote is to Keep It Simple: Use a "curry" function to "curry-fy" an
immediate (i.e., normal C++) function F, with each application of "(arg)"
directly either (a) evaluating F with the current argument together with all
the previously curried argument (if possible); or (b) currying the
argument. This was Eric's original proposal, as far as I could tell. Just
add to this a family of "curryN" ("curry2", "curry3", ...) functions that
"curry-fy" an immediate function *and* fix its arity to N; thus, the first
N-1 "(arg)" applications would curry the argument.

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk