« Apple Aluminum Keyboard Firmware Update 1.0 Fixes Control KeyI hate parsers »

Functional Programming in Ruby, according to Raganwald

10/07/08

Permalink 09:42:13 am, 383 words
Categories: Code

Functional Programming in Ruby, according to Raganwald

The biggest reason I don’t use Ruby is that it’s hard to learn real functional programming. There are two reasons: first, it’s a Lisp-2 with redundant and messed up scope rules (because of blocks), and second, the culture is more strongly OOP and people don’t do a lot of functional programming. The second reason is more important, of course, because it makes it hard to find good examples online.

Of course once you know a language well, it’s easy enough to shoehorn in whatever style you like, but Python and Ruby are so similar that my only reason for switching would be a substantial increase in elegance or else having to use something Ruby-specific, like Rails. Given that the comp ling group at IU uses mostly Python and C++, the latter is not likely to happen. And Ruby is marginally more elegant, but not enough to make it worth the pain of figuring out how to do functional programming in it.

But! Just in case, here is a set of pointers to Raganwald’s writing on functional programming in Ruby. Here is probably the most important piece of code up front:

class Symbol
  # Turns the symbol into a simple proc, which is especially useful for enumerations. 
  def to_proc
    Proc.new { |*args| args.shift.__send__(self, *args) }
  end
end

# use like this:
(1..100).inject(&:+)

So even though you have to quote your methods, you don’t have to lambda-lift them because of the blocks=syntax design. Still a Lisp-2, but probably easy to remember when you need to quote than in Common Lisp. I can’t this to work with functions (methods on the global object or whatever they are in Ruby), but I guess it’s probably not too hard. And you probably shouldn’t be using them much anyway. Ruby! OOP! Remember?

  1. Really Simple Anamorphisms
  2. Really Useful Anamorphisms
  3. String-to-Proc, which may in fact be Evil, but is still pretty useful in a pinch.
  4. Unary ampersand, a useful explanation of the difference between blocks and procs and how to convert between the two.
  5. Closures and Higher-Order Functions, not as useful, because it’s just basic un-Ruby-like lambdas and things.

I probably missed some. This is a start, and if I really do need to get going in Ruby, I can come back here later and add the rest.

2 comments

Comment from: tc [Visitor] Email
"probably easy to remember when you need to quote than in Common Lisp"? "I can’t this to work with functions"?

Me need proofread? :-)
10/07/08 @ 17:39
Comment from: sandersn [Member]
Yeah, sorry. I should add another category called "braindump" so that people won't expect my otherwise superlative level of quality.
11/07/08 @ 07:15

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
powered by b2evolution free blog software