Thursday, September 07, 2006

» Spicey curry in your ruby +

I've been learning OCaml for the past few days, and for being a member of the evil functional programming paradigm (invented by people who actually liked doing calculus!), it's not too bad. It has some cool features like pattern matching, subtyping (variants) and some other fun stuff. But over all, it's just too much of a pain to use (speaking personally). If I ever had to do something that absolutely needed speed or type safety, like write a program that drives my car around town for me and does my errands, then I would probably use it over C/C++; but I honestly can't see myself using it that often. But as I said, it does have some nice features. More than a couple times as I worked through the tutorial I was heard to say "nice", "nifty" and I think I even said "funtastic" once (I talk to myself, out lound, often -- the meds aren't working, heh).

One of the great things about OCaml (and most of its functionalistical kindred) is currying (or "partial application"). Currying has something to do with Lambda Calculus and Category Theory and other fields of knowledge that are only useful to people who can count to infinity on their fingers using only self-reference and let bindings. The only thing mortals like us need to know is that it is where you take a method that has multiple arguments, partially apply it (i.e., make a new method with one of the arguments consumed), and return a new method that takes n-1 arguments. So, for example, method m:

def m(a, b, c)
print a, b, c

Could be curried (doesn't actually work in ruby, just showing the idea) like so:

m_bc = m('value a')

And what that does is, in essence, make a new method named m_bc, that looks like:

def m_bc(b, c)
print 'value a', b, c

In other words, m with parameter a consumed. Pretty cool little feature of most functional languages.

Well, python will be getting curry support and some other goodies in 2.5 via the functools module (PEP 309), and you can already find some implementations of curry for python floating around the net, such as:

curry = lambda func, *args, **kw:\
lambda *p, **n:\
func(*args + p, **dict(kw.items() + n.items()))

I just think ruby should have some curry, too! Curry chicken and saffron pilaf for ruby, I say! So to that end, here is a (probably buggy -- I just wrote in in about 10 minutes) implementation of curry for ruby.

class Object
def curry(*args)
if args.size == 1
m = self
a = args.first
elsif args.size == 2
m, a = args
"`curry': wrong number of " +
"arguments (#{args.size} for 2)",
def _unbnd(m, a)
lambda { |*b| m.bind(a).call(*b) }
def _proc(m, a)
lambda { |*b|, *b) }
if m.is_a?(UnboundMethod)
fun = _unbnd(m, a)
elsif not m.is_a?(Proc)
m = method(m)
fun = _proc(m, a)
rescue NoMethodError, NameError
m = self.instance_method(m)
fun = _unbnd(m, a)
fun = lambda {}
fun = _proc(m, a)

def add(a, b, c=0)
a + b + c

## here's how you use it
add2 = curry(:add, 2)
p add2[4] # => 6
p add2[6] # => 8
add4 = curry(:add, 4)
p # => 4
add6 = add4.curry(2) # currying other curries
p # => 8

## you can also curry class/instance methods
## ps. these examples are stupid
arr_rev = Array.curry(:reverse, [1, 2, 3])
p # => [3, 2, 1]
arr_len = curry(Array.instance_method(:length), [1, 2, 3])
p # => 3
arr_new = Array.curry(:new, [4, 5, 6])
p # => [4, 5, 6]

Anyway, it's not like curry is so amazingly wonderful and let's you do all kinds of things you can't already do, but it's nice for creating little custom Procs out of methods quickly.

In fact, here is one actually useful way to use curry:

debug = curry(:print, '*** DEBUG -> ')
# ... bad stuff
debug["Oh no! You broke something (again)!\n"]
# ... crud, better fix this!
debug["Dude, you totally suck at programming...\n"]


Anonymous David Lowenfels said...

here is a ruby gem that provides curry:
December 29, 2007 at 3:55 PM


Post a Comment

<< Home