Permalink
Partial application/curry module for Python
25 JUN 2003
By request, I'm releasing a draft of my partial.py module (as mentioned on my Python page), which does things like this:
>>> from partial import _
>>> addWorld = _ + " world!"
>>> addWorld("Hello,")
"Hello, world"
>>> mapcall(_.upper, ["list", "of", "strings"])
["LIST", "OF", "STRINGS"]
This is a work in progress, use at your own risk. I do not expect the interface to change at all but it
is not widely tested and there may very well be bugs. The module depends on updated
versions of other modules in the Xoltar Toolkit,
so I'm releasing updated versions of them as well. I'm not making a Sourceforge release
because I haven't had time to do the sort of testing I'd like to do before making a release. At least
this way, you can try them out. If you find bugs, please report them and I'll fix them (though sometimes slowly).
Here's the documentation:
Void objects are stand-ins, blanks, or holes in an expression,
which can later be filled in. For example:
v = Void()
add_one = v + 1
Now add_one is a function which takes one argument, and returns the
argument plus one. This module exports "_", a single underscore, as
an instance of Void, because it is suggestive of a blank or missing
piece. Here are some more examples:
import sys
from operator import add
adder = add(1,_)
assert map(adder, range(3)) == [1,2,3]
assert (_ + 5)(5) == 10
assert (5 + _)(5) == 10
assert (5 * _)(5) == 25
assert (_ * 5)(5) == 25
assert ((5 + _) * 5)(3) == 40, ((5 + _) * 5)(3)
assert ((5 + _) * _)(3)(5) == 40
assert ((5 + _) * _)(3, 5) == 40
lst = []
(_.append)(lst)(5)
assert lst == [5], lst
unsplit = ["foo", "ba ba", "baz"]
postsplit = [["foo"], ["ba", "ba"], ["baz"]]
assert mapcall(_.split, unsplit) == postsplit
assert mapcall(None, map(_.split, unsplit)) == postsplit
assert mapcall(_.split, unsplit, 'o') == [['f','',''], ['ba ba'], ['baz']]
assert map(_ + 5, [1,2,3]) == [6,7,8]
assert map(_['key'], [{'key':5}]) == [5]
assert map(_[0], [(1,2,3),("a", "b", "c"), (10, 20, 30)]) == [1, "a", 10]
assert map(_[1:3], [(1,2,3),("a", "b", "c"), (10, 20, 30)]) == [(2,3), ("b", "c"), (20, 30)]
if sys.version_info[0] > 2 or sys.version_info[1] > 2:
revtest = map(_[3:0:-1], [(1,2,3),("a", "b", "c"), (10, 20, 30)])
assert revtest == [(3,2), ("c", "b"), (30, 20)], revtest
assert len(((_ + 2) * "foo")(3)) == 15
Download:
partial.py
funtional.py
datastruct.py
