Thursday, July 24, 2008

Coinflips with Chef

You may have seen this humorous job posting regarding a supposed quantitative hedge fund position. Implementing 50 coinflips is clearly trivial in most any programming language, but I decided to give it a stab in Chef, which is an esoteric programming language where the code looks like a recipe.

Here it is:

Coinflip Lime Tofu Hot Dogs a la Sous-vide with Pickle Lemon Garnish.

This recipe returns 50 coin flips, using an auxiliary recipe for pickle garnish to pull the random draws. The edibility of the result is dubious, but apparently it's a favorite for hedge fund gurus.

Ingredients.
50 blocks of tofu

Method.
Press the blocks of tofu. Serve with pickle lemon garnish. Push the blocks of tofu until pressed. Liquify contents of the mixing bowl. Pour contents of mixing bowl into the baking dish.

Serves 1.


Pickle Lemon Garnish.

Ingredients.
84 g diced pickles
72 g diced tomatoes
1 squashed lemon

Method.
Clean the mixing bowl. Put diced pickles into mixing bowl. Put diced tomatoes into mixing bowl. Mix the mixing bowl well. Fold squashed lemon into mixing bowl.

And yes, it actually runs. Install the Acme::Chef Perl module and copy the code into a text file (e.g. coinflip.chef) and you can run it with: chef coinflip.chef

And you'll get output like: THHHTHTHTTHTTHHHTTHHHHHHHHHHTHHTTHTTTHHTHHTHTTHTTT

Note that to get it to work with the Perl module I actually had to use the "liquify" keyword while the official specs say it's deprecated in favor of "liquefy." I also used an auxiliary recipe rather than multiple bowls as referring to the 1st/2nd bowl didn't seem to work according to specs either.

As for how it works, I'll let you figure that out, but Chef is basically a verbose version of Brainf*ck where you have to work everything out in terms of manipulating the stack. It was fun to do though, and I might try to throw together a more serious Chef program at some point. I also was thinking of implementing the coinflips in Shakespeare but there doesn't seem to be an adequate randomizing function for it...

Tuesday, July 15, 2008

Relearning Java

Many moons ago I studied computer science. I made it through all three "gateway" classes - the ones intended fail half the students and scare people off from majoring in the subject. I'm likely the only person who did this and then decided to major in something else.

But I digress - the first two of these classes used Java. I did well in the classes and even mostly enjoyed them, but I have since forgotten most of what I learned (both about Java and computer science). Java has also changed a bit in the interim, although not as much as I expected.

Since apparently Java is the most popular programming language, and since I remember it being a pretty safe playground, I decided to go ahead and dive back in. I haven't done anything major yet - a few simple classes and applets - but I do find that what I have learned in other languages (particularly R) has helped me better understand both the strengths and weaknesses of Java.

I won't go into too many details as I'm sure the Internet is full of people with more knowledge than me opining about the qualities of programming languages. I will say that, apart from the verbosity, I am enjoying Java. It does enforce a great deal of structure but I am sympathetic to its mindset, and I absolutely love working in Eclipse.

I've been toying with ideas for more significant projects - a Java rendition of Devil Bunny Needs a Ham, perhaps. Of course applets are evil (we are far too impatient to wait 5 seconds for the JVM to load) so if I actually wanted to make a popular game I should use something else. Maybe I'll write it in Java and use the Google Web Toolkit to make it all Ajaxy...

So anyway, stay tuned for potential Java-related updates, and if you have fun project ideas (or if you, yaknow, exist) feel free to comment.