Wednesday, September 22, 2010

Presented Clojure to Montreal.rb

Yesterday I had the opportunity to do a smallish (15 minutes) presentation of Clojure to Montreal.rb called Clojure for Rubyists.

I think it went neither terribly bad nor terribly good. I included a section about software transactional memory at the end of it, but in retrospect it was too hard (for me) to explain it well and quickly. I should have gone with Java interop instead.

I had the chance of having a mix crowd of Rubyists, Pythonistas and Clojurians. Everybody was friendly and I had some excellent questions. A huge thanks goes out to my Bonjure buddy Hugo Duncan who took care of the toughest questions.

I was followed-up by Cyril Robert who did a live coding presentation of a basic blog application using Django in only half an hour! Now that's extreme pair programming.

Here is the official presentation wrap up.

Wednesday, August 25, 2010

From Clojure to Ruby

Ever since I've received Stuart Halloway's Programming Clojure, I've been reading, watching and listening about Clojure. And dabbling with it a little.

A few months ago, I started learning Ruby. My main learning experience has been The Ultimate Book to Ruby Programming by Satish Talim and his free online class Core Ruby.

I just finished the Core Ruby Class 18th batch and I must say it's excellent. The class material not only makes a good introduction, it's also my first reference for looking up core concepts. The class ran for 8 weeks. At first, I thought that was too long to cover the basics, but if you do have a day job, the class will be taking plenty of evening and weekend study time.

Each week has assigned reading materials, exercises and a quiz. To really get more from the class than you'd get from just reading a book, you have to do the exercises, post your solutions, discuss them and read solutions from others. The class have competent and friendly mentors that will really help you get from a "Classic OO language in Ruby" solution to an idiomatic Ruby solution. I've also had the chance to have good classmates.

I must stress the point that you can only get back from the class as much as you invest yourself in it. Workload would vary from week to week, but I consider 5 hours a week an absolute minimum and 10 hours is better. I've once spent 8 hours just on a bonus exercise (The Playfair Cypher, if you must know.) YMMV of course!

The bottom line is if you're serious about learning Ruby and you're ready to put in the hours and your passion, the Core Ruby Class is an excellent way to start your journey.

Going back to Clojure, I got some advantages from learning it that translate to Ruby since they have some things in common.

The first thing I noticed is both languages use keywords and with the same syntax too! Hello, I'm a :keyword. They are called symbols in Ruby.

Keywords can be seen as a way to use strings as constants. The advantage over using normal strings is two symbols with the same name are guaranteed to be the same object and thus, you can compare using reference equality instead of value equality. It's typical to use keywords for hash keys. It took me a while to wrap my head around the keyword concept in Clojure, so I was glad to be able recycle it.

Another common thing is the use of separators instead of camel cases for variable and function names: encrypt_message in Ruby or encrypt-message in Clojure. Also, putting ! or ? at the end of the function to express mutation or a predicate. Example: "Hello".empty? in Ruby or (empty? "Hello") in Clojure. Funnily enough, I'm testing these examples with Redcar, a Ruby editor running on JRuby featuring both a Ruby and a Clojure REPL.

As for conditional testing, Ruby and Clojure considers false and nil to be false, and everything else to be true.

Well, there are plenty of differences too of course and I got bitten more than once. They say when you learn your third spoken languages that you'll mix it up with your second spoken language at first. Likewise, even though one is a Lisp dialect and the other one isn't, more often than not, I'll declare a Ruby function like this:
def repeat_function some_function time_interval total_interval do...

I forget the commas between the arguments!
I'll have the same problem calling a function too.

Strange, but true.

Sunday, August 8, 2010

Sherlock

There's a new Sherlock Holmes series on BBC One. It's simply named Sherlock.

I'm a huge fan of the Sherlock Holmes series played by Jeremy Brett as Sherlock and Watson being played first by David Burke an then by Edward Hardwicke. All excellent actors and to me, the character as played by Jeremy Brett became THE Sherlock Holmes.

When doing a new adaptation of a classic, there's always a fine line to walk. You want to respect the original and at the same time, you want to do variations to make it fresh and interesting.

I found the 2009 Sherlock Holmes movie starring Robert Downey Jr. quite excellent, although I had accepted beforehand that it would be different.

Now I'm finally getting back to the new BBC One Sherlock. The story takes place in present-day Lodon, which is really interesting. Sherlock and Watson are wonderfully played by Benedict Cumberbatch and Martin Freeman. This character version of Sherlock Holmes has definitively some variations from the character played by Jeremy. Without giving spoilers, Sherlock performs a cruel action in the final confrontation that would not be compatible with Jeremy Brett character version. On the other hand, he seems less like a robot. He makes jokes!

The episodes are movie-length: 90 minutes! I watched the first episode twice and enjoyed it a lot. I liked the second episode a bit less, although I can't say why.

All in all I think this new series is a tremendous achievement with the visuals, sounds, scenario and acting. I'm really looking forward to the third episode. I guess the only down-side is it seems we can't expect other episodes for a while after that.

Monday, July 5, 2010

Something Old, Something Nu, Something Interoperable

I am amazed at diversity in the vast field of computer science. There are so many needs and some many solutions, and new needs and new solutions arising from the desire or sometimes necessity to communicate between different software over different architecture, networks, etc.

Interoperability influences even choosing a language or designing one!

Here are a few languages targeting a varying spectrum of platforms.
  • Nu, an object-oriented Lisp I heard about on Disclojure, targets Objective-C.
  • Clojure, a functional programming Lisp, targets the JVM.
  • Fantom, a multi-faceted language, targets both the JVM and .NET (and Javascript on the browser)
I'd like to talk a bit about the first two. They both seem well though-out and elegant languages with clear design goals.

What I'd like to highlight though, is that the respective language designers of both previously worked on bridging two languages. In one case, between Common Lisp and Java, in the other case, between Ruby and Objective-C.

Both designers ended up designing a new language for marrying with their platform of choice with maximum interoperability.

I just found it an interesting parallel between two elegant and pragmatic languages.

Sunday, January 24, 2010

A Little Bit of Clojure and Scheme

Following my desire to read as much SICP as I can without going nuts, I did at least one SICP exercise (Exercise 1.8) with both Scheme and Clojure.

I'm putting them here for a lightweight comparison. They're not supposed to be efficient or clever solutions, just the first ones I came up with. Since there are really few differences between the two languages at this level, I took the liberty of using Clojure thread-first macro to be able to contrast something. Thanks to Sean Devlin screencast on the subject.

Scheme
(define (square x)
     (* x x))
(define cube
     (lambda (x)
     (* x x x)))
(define (cubert x)
   (cubert-iter 1 x))
(define (cubert-iter guess x)
   (if (good-enough? guess x)
       guess
       (cubert-iter (improve guess x)
                    x)))
(define (good-enough? guess x)
   (< (abs (- (cube guess) x))
          .0001))
(define (improve y x)
   (/ (+ (/ x
            (square y))
         (* 2.0 y))
       3.0))

Clojure
(defn square [x]
     (* x x)) ;I prefer fn than using the #() macro
 (def cube
   (fn [x]
    (* x x x)))
  (defn cubert [x]
   (cubert-iter 1 x)) ;Need to reorder before usage or "declare" beforehand
  (def good-enough?)
  (def improve)
  (defn cubert-iter [guess x]
   (if (good-enough? guess x)
       guess
       (cubert-iter (improve guess x)
                  x)))
  (defn good-enough? [guess x]
   (< (Math/abs (- (cube guess) x))
          0.0001)) ;Uses the thread-first macro to make it a bit different
 (defn improve [y x]
   (-> x
     (/ (square y))
     (+ (* 2.0 y))
     (/ 3.0)))

Saturday, January 9, 2010

Clojure, Cliché and Nerdiness

More than a year ago, I posted about my dabbling in Scala, and wanting to check out Haskell and Clojure.

Contrary to my expectations, I ended up being attracted to Clojure the most. I do read up a lot about it. Beside the excellent Programming Clojure, I read the Clojure Google group and various blogs. I bought the screencast Functional Programming with Clojure, watched the various screencasts by Rich Hickey and listened several times to the NYC Lisp 2007 presentation. For those interested, here is where to start with Clojure.

My problem though, as always when I try to learn a language, is too much reading and not enough coding!

In what seems to have become a cliché, I'm now watching the famous SICP lectures and going through part of the book. I'm hoping I will do some of the exercices 8) I'm using PLT Scheme with the SICP support.

Anyway, watching the lectures, I was suddenly hit by something: "I'm such a nerd to be enjoying this so much!"

I'm a professional programmer, but I'm beginning to think I'm really a hobbyist programmer at heart. I learned to program reading a introductory column in a newspaper using pen and paper. Later my parents got me a Commodore 64 and I was hooked. I predictably graduated in computer science. I had a course given with the first edition of the SICP book. Unfortunately back then, I really did not appreciate it or Lisp.

So here I am, 15 years down the road, and I'm revisiting my computer roots to see what I missed the first time around.