Plot EC2 instance usage with Clojure and Incanter

26 02 2010

Have you ever wondered which instance types you use most when dealing with Amazon’s EC2? I have, so I came up with a short Clojure/Incanter program to plot a nice chart like this:

EC2 usage plotted by #incanter (@liebke) on Twitpic
(full size image)

Here’s the code, if you have any feedback please leave a comment, this was the first time I used Incanter.


Major changes in ClojureX

21 02 2010

When ClojureX was started it tried to be everything for everybody. Not only did it checkout and compile the git HEADs of clojure, clojure-contrib and jline, it also (optionally) downloaded packages for Clojure support in TextMate or Emacs, configured and installed them. This has two obvious drawbacks. First off, people end up downloading stuff they don’t need. With current disk sizes and connection speeds that’s probably not much of a problem, but some users may still find annoying. More importantly though it was hard to keep up with the upstream changes, e.g. clojure-contrib’s recent switch to Maven.

So now instead of trying to provide a one-size-fits-all solution I’m taking a totally different approach: from now on ClojureX will only contain the most recent stable releases of clojure, clojure-contrib and jline, as well as a very full-featured clj script:

I know that this potentially makes ClojureX less useful to some people, but I do believe that it’s one of the fastest and most painless ways to set up a Clojure development environment.


Scary Ruby

12 01 2010

We all know that Ruby’s a flexible beast, but just how much you can bend it is almost scary. How about inheriting from a class randomly chosen at runtime? Sure, no problem:

I dare you to find a use case for this ;-)

And in case random isn’t good enough for you, why not have the user choose the superclass for you?


Bored? Don't worry, here's a new programming language...

11 01 2010

Recently – for various degrees of recent that is – people really seem to be into programming language design and development. You’ve probably all heard about Scala, Clojure and Go already, but here are some more new languages which may be new to you (descriptions taken from the projects’ websites):

  • ANI, the experimental, high-performance, statically-safe, fully implicitly parallel, object-oriented, general-purpose dataflow programming language.
  • CoffeeScript is a little language that compiles into JavaScript. [...] it compiles into clean JavaScript (the good parts) that can use existing JavaScript libraries seamlessly, and passes through JSLint without warnings. The compiled output is quite readable — pretty-printed, with comments preserved intact.
  • Factor is a concatenative programming language where references to dynamically-typed values are passed between words (functions) on a stack. [It] is expressive, fast, supports interactive development and has a full-featured library.
  • ooc, a modern, object-oriented, functional-ish, high-level, low-level, sexy programming language. It’s translated to pure C with a source-to-source compiler. It strives to be powerful, modular, extensible, portable, yet simple and fast.
  • Yeti, ML style functional programming language, that runs on the JVM.

Simple AWS scripting with boto

28 12 2009

boto is a very nice Python interface to the Amazon Web Services which I can heartily recommend for your cloud scripting needs. Here’s a quick example program I whipped up around 2 weeks ago (be gentle, I’m not a Python guy and this is just a throwaway quickie), which will list all your AMIs (excluding public ones), used S3 buckets plus a list of currently running instances with some information like instance type, start time etc. The nice part is that you can connect to any instance via SSH directly from this lsit which I find more convenient than copy/pasting the public DNS name from the AWS console:


happynerds.net is online!

13 12 2009

In my previous blog post I mentioned how much fun I have in creating a small web project with Sinatra, Heroku and MongoHQ and today said project finally went online:

Happynerds – Programming Links for Kids

If you wonder why I’m doing this, RailsBridge has a good summary on the topic.


Fun with Sinatra, Heroku and MongoDB

06 12 2009

I’ve been toying around with the idea for a small website lately, but never actually got around to do it. In an attempt to clear out my personal to do list I finally motivated myself to start yesterday, and I have to say the following combination of tools is not only insanely efficient, but also really fun to work with:


  • Sinatra, “a DSL for quickly creating web applications in Ruby with minimal effort”. I recommend using it in combination with Mongrel, Shotgun and Haml.

  • Heroku, a Ruby cloud hosting platform. Small sites are for free and Heroku’s git-based workflow just feels so natural. You can also manage your gem dependencies and set up environment variables, which made my life a whole lot easier.

  • MongoDB, “a scalable, high-performance, open source, schema-free, document-oriented database”. In this specific case I used MongoHQ for the database hosting, since they were nice enough to provide me with a beta account. I stored all the connection information for MongoHQ in environment variables as described above:


With this combination I was able to go from zero to mostly finished (I still need to write some of the content and make/steal a stylesheet) in very little time, while actually having fun! Thanks everybody for providing the Ruby community which such awesome tools! :-)


Gravedigging why the lucky stiff: Io Has A Very Clean Mirror

01 12 2009

Io is a small small, prototype-based OO programming language that’s fun to play with. Unfortunately there’s not too much information about it out there and quite predictably googling for “io” gets you lots of results you are not looking for. But today I remembered that why once did a nice little article on the language, and thanks to the amazing Internet Archive I actually managed to find it! So with out further ado, here we go:

Io Has A Very Clean Mirror


Fun with Twitter and Ruby

23 11 2009

I’m currently toying around with some ideas for Twitter bots etc. so I had a look at some of the available gems out there. Sometimes Ruby makes stuff almost too simple… ;-) Here are a few nice snippets of what you could do with a couple of minutes time and some Ruby:

Automatically translate all tweets for a given keyword:

Daemonize the above:
Alternatively you can also follow a userid instead of a keyword and daemonize the whole thing, thus making it super easy to create a bot which posts translations of Matz’s tweets:

If you put this into a file like transbot.rb, the command ruby transbot.rb will give you all the regular daemon commands like start, stop, etc.

Ruby is trendy
Curious of getting a list of current trends on Twitter, without hashtags? There you go:

Combining all these ideas you could easily write a bot that follows the current Twitter trends and posts translations of them in only a couple of lines. :-)


Using Java libraries from Clojure

06 09 2009

After reading Scripting Java Libraries With Ruby on the Engine Yard blog, I decided to translate the two example programs to Clojure. Please keep in mind though that I'm pretty much a Clojure newbie myself, so the code shown here might not be very idiomatic. In case you have any suggestions on how to improve the code examples presented here, please leave a comment, thank you! To get the most out of this post, you should probably read the section on Java Interoperability in Mark Volkmann's excellent Clojure tutorial first.

A MIDI Keyboard

The first example listens for a line of text which then gets converted into a playable MIDI sequence. Let's start with importing the classes we need. (ns midi/keyboard (:import [javax.sound.midi MidiSystem Sequence MidiEvent ShortMessage]) (:use [clojure.contrib.seq-utils :only (indexed)]))
Next we set up a loop and an exit clause. In case the user enters the word "exit", we call System.exit to shut down all the threads that got started by Java's MIDI support. (loop [line (read-line)] (if (re-find #"exit" line) (System/exit 0)) ... other code snipped... (recur (read-line)))
The main part of the application constructs a new MIDI sequence by creating a NOTE_ON event for every character in the entered text and then finishing the sequence with a STOP event. The original article states that it favors simplicity over efficiency, so the same also holds true for my "translation". (def sq (Sequence. Sequence/PPQ 2)) (def track (.createTrack sq)) (doseq [[idx note] (indexed line)] (let [msg (ShortMessage.)] (.setMessage msg ShortMessage/NOTE_ON (int note) 64) (.add track (MidiEvent. msg idx)))) (let [msg (ShortMessage.)] (.setMessage msg ShortMessage/STOP) (.add track (MidiEvent. msg (+ (.length line) 1))))
To finish off our first program we play the sequence before prompting for more input (see the loop snippet above). (doto (MidiSystem/getSequencer) (.open) (.setSequence sq) (.start))
Full source for the first example

Making It More Interactive

While the first example was fun, it wasn't as interactive as it could be. Therefore the second program launches a GUI window to receive keystroke events, turning a note on or off when a key is pressed or released. Since we don't have to create MIDI sequences this time, our import statement becomes a lot shorter: (ns midi/interactive (:import [javax.sound.midi MidiSystem] [javax.swing JFrame] [java.awt.event KeyListener]))
This example directly accesses the system synthesizer. The following lines open the default synthesizer's channel 0 (and also create a JFrame object which we'll use later): (let [synth (MidiSystem/getSynthesizer) frame (JFrame. "Music Frame")] (.open synth) (let [channel (aget (.getChannels synth) 0)]
We then launch a GUI frame as a simple and cross-platform way to receive keystroke events. The KeyListener interface is implemented using Clojure's proxy macro, using keyPressed and keyReleased events to turn notes on or off. (doto frame (.setSize 300 300) (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE) (.addKeyListener (proxy [KeyListener] [] (keyPressed [e] (.noteOn channel (int (.getKeyChar e)) 64)) (keyReleased [e] (.noteOff channel (int (.getKeyChar e)))) (keyTyped [e])))
Last but not least we display our frame to let the fun begin. (.setVisible true)))) Full source for the second example

I hope this article helped to illustrate how easy it is to use the plethora of existing Java libraries from Clojure. In case you have any feedback or questions, please feel free to leave a comment!

First small Clojure program

26 08 2009

After I recently got interested in Clojure, I decided to use it for a small “project”, namely a script which will download all the enclosures from an RSS 2.0 feed to the current working directory. The program itself is very concise, with only 9 lines of actual code (the rest are comments and library imports).

My impressions: for somebody with little previous exposure to Lisp (some CL and very little Scheme and no Java background the learning curve can be a bit steep in the beginning, especially since documentation is still a bit (too) sparse. However, if you are willing to spend some time on Google or reading the actual source code of Clojure and clojure-contrib you’ll generally find what you are looking for. Besides that people in the Clojure community seem to be quite friendly and my question on #clojure got satisfactorily answered within a minute.

It’s a definitely too early for any real assessment of the language, but I quite like what I saw so far, so there probably will be some more Clojure-related posts in the near future.

Some links to get you started:
Clojure API
An overview of clojure.contrib
Clojure – Functional Programming for the JVM
20 Clojure links to get you up to speed
Learning Clojure and Emacs


Pascal vs. Ruby

29 06 2009

On ruby-talk somebody was asking for a method which will be used to generate a random string, by returning either a lowercase letter or one of the digits 0-9 when called. As an example he posted the following Pascal program:

function GetRandomChar: char;
var
 r: integer;
begin
 r := random(36);
 case r of
   0..25: result := chr(ord('a') + r);
   else : result := chr(ord('0') + r);
 end;
end;

“Translating” this to Ruby, this is what I got:

In your face, Niklaus Wirth! ;-)

P.S. Actually I do have fond memories of the good old Turbo Pascal days.


Quick and dirty lazy lists in Ruby

18 06 2009

Hash.new with a block is a sexy beast: you can specify default hash values and when a key is first used, the value will be calculated and inserted into the Hash. Based on that behavior I took a couple of minutes to hack together a small proof of concept class for lazy lists:

As I said, this is quick and dirty, especially since you have to pass the entire Hash.new block to LazyList.new right now. Other than that I quite like it though. I know there are at least two other lazy list implementations for Ruby, but it seems like both haven’t been updated in a while and I don’t particularly like their interfaces. Dunno, if I manage to simplify list definition, I may actually try to develop this into something. Don’t hold your breath though…


Unsavory updates

16 06 2009

Today I felt like playing around with unsavory a bit more, which led to some minor improvements:


  • code reorganization: the delicious class got moved to a separate file in lib/ and got a new method for easier retrieval of the URLs array

  • login credentials can now be read from a config file, thus making HighLine optional


Next on my list is packaging this up as a gem so people can simply install it with ‘gem install unsavory’.


Review: The Well-Grounded Rubyist (Manning Publications)

09 06 2009

As part of Rubylearning’s current book promotion I’m reading The Well-Grounded Rubyist by David A. Black. Here are my thoughts on the book:

If you are new to Ruby, this may very well be the book you are looking for, since the author was really serious about the “well-grounded” in the title. Together the first six chapters form Part 1, aptly called “Ruby Foundations”. Here you’ll learn about objects, modules, classes, self and control-flow techniques. Although this part may not be the most interesting for more experienced Rubyists, it’s certainly well-written and manages to present a lot of very fundamental Ruby right at the beginning. True, at first I was a bit surprised to see that singleton methods were – implicitly – introduced before classes, but when you follow the author’s logic, it all makes a lot of sense. What I really like about this approach is that it exposes the reader to concepts needed for some quite advanced Ruby coding right away, and in a very light-hearted and “natural” manner. Well done!

Part 2 (“Built-in classes and modules”) covers everything from strings, over symbols to regular expressions and file I/O. What I really like about this part is that the author dedicated two whole chapters to collections and iterators/enumerators, which are essential for everyone striving to become fluent in Ruby.

Last but not least Part 3 (“Ruby dynamics”) talks about singleton methods, procs, lambdas, Symbol#to_proc, the various eval methods, bindings and introspection, thus equipping the reader with all the necessary tools for metaprogramming, one of the many things that make Ruby that sexy little beast it is.

Oh yeah, since I didn’t mention this before, “The Well-Grounded” Rubyist” covers Ruby 1.9.1, which means you’ll learn about the coolest Ruby around. Don’t worry though, most of what you read will also apply to 1.8.6, but isn’t it about time that we all slowly moved on?