Adventures in Currying

Nick Mathewson nickm at mit.edu
Sun May 14 20:43:47 EDT 2000


On 15 May 2000 00:40:14 +0100, Michael Hudson <mwh21 at cam.ac.uk> wrote:
>nickm at mit.edu (Nick Mathewson) writes:
 [...]
>> With this in mind, I
>> started writing up currying operators of various degrees of
>> complexity.  I give them below, in their approximate evolutionary
>> order.  They are all classes designed to turn python functions into
>> curried functions.
 [...]
>
>Are you aware of my bytecodehacks package?  One of the modules in it
>is "xapply", which basically curries functions.  You can see it here:
>
>http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/bytecodehacks/bytecodehacks/xapply.py?rev=1.9&content-type=text/x-cvsweb-markup&cvsroot=bytecodehacks
>
>and read about it here:
>
>http://bytecodehacks.sourceforge.net/bch-docs/bch/module-bytecodehacks.xapply.html

I've seen bytecodehacks, but I didn't know it could do this.

Reading the source...

Merciful <deity>!  Your code seems to be looking at which instructions
you're replacing, and gathering up all the loads and stores that refer
to them.  Then it replaces the ones which are never loaded with
constants, and replaces all the ones that _do_ get loaded with new
variables that are initialized at the start of the function.  It adds
the constants to the method's constant list, and reindexes all the
local variables.  It is amazing.

The only really big difference I can find between your code and mine
is that mine treats the filled-in arguments as new 'defaults', while
yours removes the arguments the arguments as they're filled in.

In other words:

  def fun(a,b): 
      return 2*a+b

  from bytecodehacks.xapply import xapply
  f = xapply(fun,b=10)
  print f(3)     # Prints 16
  print f(3,b=0) # Gives an error.


  from curry import Curry4
  f = Curry4(fun,b=10)
  print f(3)     # Prints 16
  print f(3,b=0) # Prints 6

This is, all things considered, probably a feature on your part and a
bug on mine.  

>I think it should work with 1.6 (can't think why not anyway); it
>hasn't a hope with JPython.

Ugh.  I sat for a moment, trying to think of a way to do this with
dynamicly generated Java bytecode.  My brain now hurts.

>i-sense-another-of-my-kind-ly y'rs
>M.

Rather-write-prorgams-to-write-programs-ly y'rs

-- 
Nick Mathewson     <nickm at mit.edu>     http://www.mit.edu/~nickm/



More information about the Python-list mailing list