Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

currying (was: Re: A trivial question about print)

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


newsfroups at jerf

Apr 12, 2002, 2:52 PM

Post #1 of 4 (1048 views)
Permalink
currying (was: Re: A trivial question about print)

Huaiyu Zhu wrote:
> def f(a, b, c): return a+b+c
> f1 = curry(f, a, b)
> f1(c)
> map(curry(f, x, y), a) # map(lambda z:f(x, y, z), a)
>
> Huaiyu

Has anyone done any work on creating a more general curry function? One
that might for instance curry the second of three positional arguments?
Using the above definitions:

f2 = curry2(f, a, c)
f2(b)

I've used one-off curry functions for this, but I'm surprised that
nobody seems to have considered this. (Maybe everybody just writes
wrappers around functions?

def f2(a, c, b): return f(a,b,c)
f3 = curry(f, a, c)

)

Not a biggie, just curious. But I have had instances where I needed this
type of currying.


huaiyu at us

Apr 12, 2002, 4:50 PM

Post #2 of 4 (999 views)
Permalink
currying (was: Re: A trivial question about print) [In reply to]

Jeremy Bowers <newsfroups [at] jerf> wrote:
>Has anyone done any work on creating a more general curry function? One
>that might for instance curry the second of three positional arguments?
>Using the above definitions:
>
>f2 = curry2(f, a, c)
>f2(b)

In general you must have a way to indicate a subset. Like

g = curry([2, 3, 5], f, a1, a4, a6)
g(a2, a3, a5)

You might even allow something like
g = curry([2, 5, 3], f, a1, a4, a6)
g(a2, a5, a3)

Is this easy to do? I don't know. If it adds a for-loop for each function
call, it might be too costly.

Huaiyu


Scott.Daniels at Acm

Apr 14, 2002, 2:32 PM

Post #3 of 4 (979 views)
Permalink
currying (was: Re: A trivial question about print) [In reply to]

On Fri, 12 Apr 2002 23:50:27 +0000 (UTC), huaiyu [at] gauss (Huaiyu Zhu) wrote:
> Jeremy Bowers <newsfroups [at] jerf> wrote:
> >Has anyone done any work on creating a more general curry function? One
> >that might for instance curry the second of three positional arguments?
> >Using the above definitions:
> >f2 = curry2(f, a, c)
> >f2(b)
>
> In general you must have a way to indicate a subset. Like
> g = curry([2, 3, 5], f, a1, a4, a6)
> g(a2, a3, a5)
> ...
I had kind of thought that the keyword args would take care of this.
Sure there are cases where this wouldn't be true, but I'd hate to have
to read code that included ... curry([2, 3, 5], f, a1, a4, a6) ..., I'd have
the devil's own time determining which args were specified. IMHO the
real questions are (1) front or back (default last or first), and (2) call-time
vs. curry-time keyword superceding. You of course know which I'd
choose (the ones in the recipe).

On issue (1), providing finals args back to front interferes with keyword args,
so I'd forget that. Providing call-time before curry time in a "flexible" order is
worse: you cannot read the curry-time args and know what is covered. I
concede most programming styles leave the "fiddley" args -- perfect curry
fodder -- at the end of the argument list. Even so, I'd prefer the earlier args be
the curry args, since you more readily know what args are being consumed.
I'd argue in favor of making those "fiddley" args keyword args.

Issue (2) is more debatable: call-time seems to me more pythonic, but I can
see logic for either side. The keyword stuff can be seen as setting defaults
if call-time supercedes (which is useful in and of itself). More than anything
else, this is what tips me in favor of call-supercedes-curry. Raising an
exception on keyword conflict is another possibility, but this seems more
like the strong typing / declared constants view: don't trust the user to do
it right.

Of course I'd love to see curry show up in the built-ins (I always forget to
import it before I use it).

As to Alex's comment on the recipe -- of course he's right; I just hadn't seen
the possibility of curry by closure until nested scopes showed up after I
had written curry.

-Scott David Daniels
Scott.Daniels [at] Acm


newsfroups at jerf

Apr 14, 2002, 8:40 PM

Post #4 of 4 (996 views)
Permalink
currying (was: Re: A trivial question about print) [In reply to]

Scott David Daniels wrote:
> I had kind of thought that the keyword args would take care of this.
> Sure there are cases where this wouldn't be true, but I'd hate to have
> to read code that included ... curry([2, 3, 5], f, a1, a4, a6) ..., I'd have
> the devil's own time determining which args were specified.

In the case I'm using it, it would still be a net gain. Basically, I
have a whole list of function calls (to set up bindings), and I'm trying
to make the calls as simple as possible.

(Why not use a data file? Well, I'm basically using the Python code
itself as my data file. Python code understands the Python runtime data
structures better then any data file interpreter I could possibly cook up.)

I was mostly curious if anybody had come up with a good way of
expressing it; obviously, with your curry example in hand, creating the
code to do what you want is the easy part. It's the specs that are hard.

> As to Alex's comment on the recipe -- of course he's right; I just hadn't seen
> the possibility of curry by closure until nested scopes showed up after I
> had written curry.

BTW, thanks for that recipe. Not only have I found it useful code, but
it taught me what currying *is*. I'd seen the pure functional kind, in
lambda calculus style, but I found that hard to follow, so I tried
http://www.google.com/search?hl=en&q=python+curry on a whim, and lo,
there it was. (I think that's also how I found the whole recipes database.)

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.