background preloader

Blogs

Facebook Twitter

Best In Class - The Blog. For reasons that I must have mentioned either in tweets or in some old blogposts, I love Clojure. But I’m also very fond of both Haskell and J so Im going to go through some solutions for trivial problems in all three languages simultaneously. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.

The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000. Clojure In Clojure this problem can be expressed very much like you how you would mentally approach the task: (->> (range 1000) (filter #(or (zero? (mod % 3)) (zero? Haskell Haskell has great syntax for building lists with filters: Prelude> [x | x <- [1..5]] -- No filter [1,2,3,4,5] Prelude> [x | x <- [1..5], x >= 2] -- Single filter [2,3,4,5] So bearing that in mind, the Haskell solution also reads like plain english: sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0] X is populated with a list of numbers in the range 1…999, these are filtered using two predicates. An explorer's log. Briancarper.net (λ) Briancarper.net (λ) Clojure.rb. Clojure, from a Ruby perspective. Fogus' recent article "clojure.rb" speculates about why there seem to be so many Ruby users adopting Clojure.

As a Ruby user who adopted Clojure, I figured I'd write about my experiences. What do Ruby and Clojure have in common, that would attract a Rubyist to Clojure? A lot. Obviously, this is somewhat subjective and I don't expect anyone else to agree, but this is what did it for me. In Ruby, everything is an object. In Clojure, everything is not an object, specifically because it inherits primitives from Java land (though this doesn't hurt much in the kind of everyday use I put Clojure to).

Clojure does have abstractions though. In Ruby, a lot of things are Enumerable, which means you can do foo.each{} and other similar things for a lot of different types of foo. Ruby-style OOP brings a lot of complexity and baggage which Clojure avoids by not being OOP. Another example of consistency: Expressions. Like Ruby, Clojure code tends to be terse and expressive. What I see: Fogus suspects: