More Monkey Business

Originally posted on another one of my blogs in ‘08, this is a follow-up to Monkey See, Monkey Do, an experiment in computer language lucidity which looked at implementations of the eponymous program in English, Groovy, Perl and Java.

I have received some contributed monkeys: today we’ll look at Ruby Monkey and Python Monkey.

Ruby Monkey

Ruby monkey quite arguably ought to have been included in the first troop of monkeys. “Ruby” is the word most likely to follow the word “like” when Groovy programmers are talking about Groovy to programmers who have never heard of Groovy. Ruby is Payton Manning to Groovy’s Eli. So without further ado, here is the Ruby Monkey contributed by Jason Felice:

1monkey = Monkey.new
2monkey.see.each{ |action| monkey.do action }

This monkey is quite similar to the Groovy Monkey, but of course there are some differences. Let’s anglicize this gem of a primate:

Monkey is monkey new.
Monkey see each action, monkey do action.

I’ll admit I may have taken a little poetic license with the placement of the comma in the second line, but I think it was worth it. This is eminently comprehensible as English. In fact I’d have to say this monkey edges past the Groovy monkey and vies with the Perl monkey for the title of Most Lucid Monkey.

Ruby Monkey Guts

Jason’s implementation of this monkey is very similar to the Groovy and Perl monkeys:

1class Monkey
2  def see
3    ["scratch", "climb", "eat banana"]
4  end
5  def do(action)
6    print "Monkey does #{action}\n"
7  end
8end

All in all, very nice. Of course Ruby monkeys have another nice feature: they can be taught! Let’s teach Jason’s Ruby monkey a new trick. Take a bow, monkey!

1class Monkey
2  def bow
3    puts "Monkey takes a bow"
4  end
5end
6
7monkey.bow

Python Monkey

This monkey was contributed by Bob Griffin.

1monkey = Monkey('lulubelle')
2[ monkey.do(action) for action in monkey.see() ]

Bob informs me that “monkey” is one of his favorite variable names. He uses “monkey” when other programmers cling stubbornly to “foo.” He also apparently prefers his monkeys to have names. So let’s see how Lulubelle sounds in English:

Monkey is monkey Lulubelle.
Monkey do action for action in monkey see.

Wow! That’s a totally different grammatical formulation! It almost sounds something like “Monkey does what monkey sees.” Obviously, Lulu marches to the beat of a different drum, preferring not to ape her counterparts from other languages.

Python Monkey Guts

 1class Monkey:
 2  name = ''
 3  def __init__(self, name):
 4    self.name = name
 5  def action(self, verb):
 6    print self.name + ' ' + verb
 7  def eat(self):
 8    self.action('eats')
 9
10  def sleep(self):
11    self.action('sleeps')
12
13  def scratch(self):
14    self.action('scratches')
15
16  def see(self):
17    #array returns functions not strings
18    return [self.eat, self.sleep, self.scratch]
19
20  def do(self,fn):
21    #functions are passed as 1st class variables
22    # and then run. Notice 'self' does not need
23    # to be prefixed as the address of the
24    # monkey instance's function was passed
25    fn()

This Python Monkey seems pretty verbose. This is partly because she has distinct methods for each of the things that the Monkey can see. But notice the difference this makes possible: other monkeys are technically seeing strings which represent actions. It’s almost more like “monkey read, monkey do.” But this Python Monkey sees methods; so Lulubelle is literally “seeing” the action which she then does herself.

Python monkeys obviously have a unique take on solving problems. Some say they can’t adhere to a spec. But Python Monkeys generally have a clear objective in mind, and they end up with some very interesting results.

What does a monkey look like in your native tongue? I have a friend pondering what a Haskell monkey would look like. But I’d also really love to see your ideas for others; what would a functional monkey look like? Clojure Monkey? Arc Monkey? Erlangutan? Or how about a Basic Monkey? Is that even a meaningful concept?

In any case if you have an example of a monkey in any other idiom, please put it (or its URL) into a comment below; I’d love to include it and compare it to the above examples.