02
Jun 11

10 Clojure One Liners to Impress Your Friends

I saw an interesting post today aptly titled 10 Scala One Liners to Impress Your Friends and then someone followed up with another blog post titled 10 CoffeeScript One Liners to Impress Your Friends. Those two posts show programming tasks (most of them trivial to solve in modern programming languages) being accomplished in about 1 line of code each.

I thought it’d be quite appropriate if someone ported those examples to my favourite programming language Clojure, so here they are -

1. Multiple Each Item in a List by 2

(map #(* % 2) (range 1 11))

2. Sum a List of Numbers

(reduce + (range 1 1001))

3. Verify if Word Exists in a String

I used a regex here, because I believe that’s the right way to do it.

(def tweet "This is an example tweet talking about clojure and emacs.")
(def regex (re-pattern (apply str (interpose "|" ["clojure" "logic" "compojure" "emacs" "macros"]))))
(re-seq regex tweet) ; Gives me the actual matches instead of just true/false

As suggested by a commentator, this problem can be solved without using regex by leveraging Clojure’s sets.

(def tweet "This is an example tweet talking about clojure and emacs.")
(def is-word? (set ["clojure" "logic" "compojure" "emacs" "macros"]))
(not (nil? (some is-word? (.split tweet " ")))) ; Returns true/false

4. Read a File

(def file-text (slurp "data.txt")) ; Reads the whole file
(def file-lines (clojure.contrib.io/read-lines "data.txt")) ; Reads as a sequence of lines

Since Clojure Contrib has been deprecated for future Clojure releases, clojure.contrib.io/read-lines can be rewritten as (line-seq (clojure.java.io/reader (clojure.java.io/file “data.txt”))) in Clojure 1.3 onwards. Thanks to Aaron for pointing it out.

5. Happy Birthday to You

(doseq [l (map #(str "Happy Birthday " (if (= % 2) "dear Rich" "to You")) (range 4))] (println l))

Alternate version -

(dotimes [n 4] (println "Happy Birthday " (if (= n 2) "dear Rich" "to You")))

6. Filter List of Numbers

(partition-by #(> % 60) [49 58 76 82 88 90])

7. Fetch and Parse XML Web Service

(clojure.xml/parse "http://search.twitter.com/search.atom?&q=clojure")

8. Find Maximum (or Minimum) in a List

(reduce max [14 35 -7 46 98])
(reduce min [14 35 -7 46 98])
;; Now both together
((juxt #(reduce max %) #(reduce min %)) [14 35 -7 46 98]) ; Returns [98 -7]

9. Parallel Processing

;; Assuming process-line to be a CPU intensive function that operates on a line
(pmap process-line lines) ; Note the "p" in front of map

10. Sieve of Eratosthenes

I don’t I have a sufficiently good (in terms of performance & beauty) one line implementation of SoE. I would recommend checking out Christophe Grand’s treatise on the subject titled Everybody loves the Sieve of Eratosthenes for a great discussion on writing real world prime sieves in Clojure.

11. Solve FizzBuzz

(map #(cond (zero? (mod % 15)) "FizzBuzz" (zero? (mod % 3)) "Fizz" (zero? (mod % 5)) "Buzz" :else %) (range 1 101))

Conclusion

Can’t conclude anything from these examples, really. Clojure is an extremely powerful and succinct programming language. Learn it, write some code in it and then decide for yourself.

Have fun!


31
May 10

Clojure Protocols & Datatypes — A sneak peek

Clojure 1.2 introduces two very remarkable features – Protocols and Datatypes. Clojure is defined in terms of abstractions and various implementations of those abstractions. For example, vectors, maps, lists, sets in Clojure implement the sequence abstraction which lets us treat any of those data structures as sequences.
Until recently it was not possible feasible to define and implement such core abstractions in Clojure itself; one had to drop down to Java (or C#) for those tasks, but not anymore :)

Clojure 1.2 now has excellent facilities for defining and implementing similar abstractions in a highly dynamic manner while maintaining fantastic performance characteristics.
In this post, I will give you a brief overview of these new features and will show you how they are useful.

Protocols

Protocols in Clojure are similar to Java Interfaces, though not quite. Basically a protocol is a contract, a set of functionalities without any implementation. Let’s consider a simple protocol –

(defprotocol Fly
  "A simple protocol for flying"
  (fly [this] "Method to fly"))

So here we have a trivial protocol Fly which declares a method fly which takes one argument ‘this’ (which is actually the type implementing the protocol itself). In case of all methods defined via Protocols, the first argument is always the implementing type itself. The name ‘this’ is just a convention; it could be ‘self’, etc. or anything.

When we declared the Fly protocol, two new vars were created. One is ‘Fly’, the protocol itself, and the other is ‘fly’ which is a polymorphic function that will get called when we execute it on an implementation of Fly.

Right now, if you try to execute the method ‘fly’ on any object, you will get an exception because no types are implementing that protocol yet, which brings us to the next topic, DataTypes.

DataTypes

Traditionally in Clojure whenever we wanted to have some kind of record or a property-only Class, we used maps or struct-maps. Those serve the purpose perfectly well in most cases but the problem was that those maps didn’t have any type information attached to them. As a result, we had to put some extra keys in maps to help us determine the type of a record before we could dispatch methods. There were some obvious performance limitations too; being vanilla maps, they were never as fast as Plain Old Java Objects (POJOs). Enter deftype and its cousin defrecord.

In Clojure 1.2 we can define our own types using defrecord like this -

(defrecord Bird [nom species])

Boom! We have a custom type, Bird with two fields, name and species. We can now instantiate a Bird like this -

(def crow (Bird. "Crow" "Corvus corax"))

We can access the fields of the Bird instance by treating it like
a normal map -

user› (:nom crow)
"Crow"
user› (:species crow)
"Corvus corax"

We can also add/remove/modify keys in a record like we would do with a
normal map.

(def sparrow (assoc crow :nom "Sparrow" :species "Passer domesticus"))

This will create a new immutable instance of Bird with different data. Note that since Clojure records are persistent and immutable, the original crow instance is not affected.

Now to make the Bird fly. We already have a protocol called Fly. We need to implement the protocol so that our birds can actually fly. One way to do that is to put the protocol implementation inline with the record definition itself -

(defrecord Bird [nom species]
  Fly
  (fly [this] (str (:nom this) " flies..."))

So easy, right? If we now create another instance of Bird, it will actually be able to fly -

user› (def kiwi (Bird. "Kiwi" "Apteryx australis"))
#'user/kiwi
user› (fly kiwi)
"Kiwi flies..."

Great! But what happens to the Crow, and Sparrow? We created those instances when the Bird record didn’t have any implementation of the Fly protocol. You might face similar issues when you don’t have control over the code which defines the record/class. You will need to extend those types dynamically with implementation of a protocol. Enter extend-type. extend-type (and its cousin extend-protocol) allows us to implement protocols on pre-existing types. Consider the following example -

(defprotocol Walk
  "A simple protocol to make birds walk"
  (walk [this] "Birds want to walk too!"))
 
(extend-type Bird
  Walk
  (walk [this] (str (:nom this) " walks too..."))

We just added an implementation of the Walk protocol to the existing type Bird. All new Bird instances created from now on will be able to Walk and Fly.

user› (def hummingbird (Bird. "Hummingbird" "Selasphorus rufus"))
user› (fly hummingbird)
"Hummingbird flies..."
user› (walk hummingbird)
"Hummingbird walks too..."

Cool, right? At times you might require a anonymous object which implements some protocol or interface. You could utilise those objects in cases where you just need an object which implements a given protocol but you don’t care about its type. Clojure 1.2 gives you reify. reify allows us to create one-off anonymous objects which implement one or more protocols.

user› (fly (reify Fly (fly [_] "Swine flu...")))
"Swine flu..."

Woah! Clojure can make Pigs fly :) Jokes apart, what we just did was very interesting. We just created an anonymous type which implements the Fly protocol and called the fly method on it; and it flu[sic] :)

We could implement multiple protocols in the same reify statement too,
like this -

(def pig (reify
                Fly (fly [_] "Swine flu...")
                Walk (walk [_] "Pig-man walking...")))
 
user› (fly pig)
"Swine flu..."
user› (walk pig)
"Pig-man walking..."

Beautiful. reify is quite similar to proxy and it is now recommended to use reify instead of proxy wherever possible because reify is much faster than proxy.

Before I finish off, let me explain the differences between defrecord and deftype. defrecord creates a new type and implements a few core Clojure interfaces like that of the persistent map, hashcode, keyword accessors, etc. If you are using deftype, Clojure will not implicitly implement any interface not provided by the user. In short, if you are using deftype, you will have to implement your own accessors, hashcode, etc. In most cases defrecord should suffice, but in other cases like when you need mutable fields, use deftype.

There is some in-depth explanation of Protocols and Datatypes on the Clojure website which you should consult if you need more information.

Bonus Material

Making Java Strings fly and walk :)

user› (extend-type java.lang.String
                   Fly (fly [this] "See me fly?")
                   Walk (walk [this] "Yes, that's me walking!"))
 
nil
user› (walk "foo")
"Yes, that's me walking!"
user› (fly "bar")
"See me fly?"

PS – I wrote this today because I was sitting at home, sick. There are possibly some mistakes in this post; in which case, please let me know.


27
May 10

Clojure Course in Pune

There is a possibility of me conducting a Clojure course/workshop in Pune. In the course I will cover Clojure from ground up, teaching how to build real-world applications in Clojure.

If you are (or your friend is) interested, please take part in this short survey which will help me understand what is needed.


19
Feb 10

Slides from my Clojure talk at GNUnify 2010

I gave an intro talk about Clojure at GNUnify 2010, Pune today. It was supposed to be a very basic talk on Clojure aimed at Java programmers. Here are the slides -


06
Oct 09

Downloading a bunch of files in parallel using Clojure Agents

I suddenly needed to download around 3000 files from the Internet. I had the urls in a sequence and I was thinking about a nice way to download the files in parallel.

The idea of using Clojure Agents came naturally to my mind and I was thinking about writing an Agent based HTTP client in Clojure. I asked around on the Clojure IRC channel and the very helpful Stuart Sierra pointed me towards clojure.contrib.http.agent

Indeed, c.c.http.agent seemed to be exactly what I had in my mind :)

The API seemed to be straightforward enough and I got cracking immediately. I came up with something like this –

;;; downloader.clj -- Parallel Downloader -*- Clojure -*-
;;; Time-stamp: "2009-10-06 13:38:57 ghoseb"
;;; Author: Baishampayan Ghose 
 
(ns downloader
  (:require [clojure.contrib.http.agent :as h]
            [clojure.contrib.duck-streams :as d]))
 
A vector of vectors containing the file name and the URL
(def url-data [["file1" "http://some.domain/file1.xml"]
               ["file2" "http://some.domain/file2.xml"]
               ; Many many more :)
               ])
 
(defn download
  "Download the data in the given URL using HTTP Agents
   Args:
     file-name - The file name to save the data in
     url - The URL to fetch
  "
  [file-name url]
  (h/http-agent url
                :handler (fn [agnt]
                           (let [fname file-name]  ; File name in a closure
                             (with-open [w (d/writer fname)]
                               (d/copy (h/stream agnt) w))))))
 
(defn download-all
  "Download all the URLs
   Args:
     url-data - A vector of vectors containing the file name and the url
  "
  [url-data]
  (doseq [[file-name url] url-data]
    (download file-name url)))
 
(download-all url-data)

This looked fine and worked with a small set of urls. But when I ran it on the full-blown set of URLs, the server bailed out because of too many concurrent requests. The reason being the fact that http.agent uses send-off to dispatch action to the agents and send-off can end up using a potentially very large thread-pool.

Surely I needed to somehow make sure that only a limited number of files are downloaded in parallel and start downloading more when those are done.

To achieve that, I did this –

(def partitioned-data (partition 15 url-data)) ;; 15 being the max parallel downloads
 
(defn download-all2
  "Download all the files, step by step
   Args:
     p-url-data - Partitioned url data
  "
  [p-url-data]
  (doseq [url-data p-url-data]
    (let [agnts (map #(download (first %) (second %)) url-data)]
      (apply await agnts)))) ; Wait till the agents finish
 
(download-all2 partitioned-data)

What did I just do? I simply partitioned the data set by the number of parallel downloads I wanted to do, and then modified the download-all function to take the partitioned data, dispatch agents on one partition and wait for them to finish, and then move on to the next partition.

Simple, yet very beautiful.


01
Aug 09

Setting up Emacs & Clojure with Emacs Starter Kit

Note: This blog post is no longer accurate as a lot of things have changed since I wrote this post. Please check this document for working instructions.

Clojure is a very modern functional programming language which runs on the JVM. It’s a very cleanly designed Lisp dialect and has all the features that any useful programming language would require. You can learn more about Clojure on the official website, the wikibook, a comprehensive article by R. Mark Volkmann or by buying the awesome book by Stuart Halloway.

Now to setup Emacs as a Clojure IDE. If you are new to Emacs then don’t worry as we will take the easiest way to set it all up. If you are a Emacs veteran, then keep your own dotemacs and the bunch of customisations that you have done in all these years aside and follow along.

Installing Emacs is easy. On an Ubuntu system, do this -

$ sudo apt-get install emacs-snapshot-gtk

That will install Emacs 23 for you. You will also need a few other things -

$ sudo apt-get install sun-java6-jdk ant git-core

You will need the JDK and Ant to build and run Clojure (it runs on the JVM, remember?) and Git to fetch the Clojure an other related libraries.

Now to setup Emacs, we will use a brilliant set of Emacs Lisp libraries aptly named Emacs Starter Kit (ESK). To get it, do this -

$ git clone git://github.com/technomancy/emacs-starter-kit.git ~/.emacs.d

That will get the ESK and will put it in your ~/.emacs.d (existing Emacs users, please move your ~/.emacs and ~/.emacs.d to some other location before doing this. You can get back your old settings later).

What ESK does is that it provides a bunch of sane defaults for pretty much everything in Emacs. You can safely use it and get a very usable Emacs setup almost immediately.

You can now launch Emacs by going to Applications > Accessories and selecting Emacs Snapshot Gtk. Once Emacs has started up, install the Emacs Clojure libraries by typing M-x package-list-packages. That will start up a buffer with a bunch of Emacs packages. Go to the line that says clojure-mode in it and press i. Then press x to install the package. Once that’s done you are ready to install Clojure. Now type M-x clojure-install and it will prompt you for a directory to install Clojure. Just press Enter for now and let it proceed. You can always change these things later.

That will fetch the Clojure runtime and also a few other useful libraries like clojure-contrib and will install them inside ~/src.

Once that is done, you can type M-x slime to start Clojure inside your Emacs. Play around with it and learn the various key-bindings for Slime and Paredit which is a fantastic system for doing Lisp development. Paredit is so good that it makes all those parentheses just vanish in thin air. But yes, it takes a bit getting used to which you can achieve with some practice. One tip is to type C-h m in Emacs to see the documentation of all the modes that are currently active. This gives a good overview of all the key-bindings and the functions bound to those keys.

To develop slightly more complex Clojure applications, you need to use different dependencies with your own code. Since Java has its own classpath related quirkiness it can become a slightly complicated task. But then, there is cool function in Emacs Starter Kit called clojure-project which will make your life easy.

To use it all you need to do is to follow this simple directory structure and the rest will be taken care of automagically. The directory structure should be like this -

myproject
|-- lib
|-- src
|-- target
| |-- classes
| `-- dependency
`-- test

Copy the Clojure & Clojure Contrib (or any other dependency) JAR files into lib (you also alternately use a build system like Maven to manage dependencies for you in which case you should consider configuring the build system to copy the dependencies into target/dependency). Keep all your code inside src and tests inside test. The src and test directories should contain namespaces like foo.bar & foo.bar.test respectively (you can later use clojure-test-mode to automatically pick up the tests and run them quickly). So when you run M-x clojure-project, it will ask you for a directory name. Give it the name of the project directory and boom! Emacs will launch Slime with the right classpath settings.

If you want to customise any Emacs setting, just create a file with your current system login id called <username.el> inside ~/.emacs.d and it will be automatically loaded when you restart Emacs. If you use additional libraries, create a directory with your username inside the same directory and everything inside will be in the Emacs load-path. Nifty, right?

So that’s all required to setup Emacs for Clojure. Join the Clojure mailing list to communicate with the Clojure community. Do check the Emacs Wiki in case you have any queries about Emacs.

Above all, have fun. That’s what we are here for.

By the way, I am @ghoseb on Twitter and I think you should follow me :)

UPDATE: Fixed a factual inconsistency regarding the paths used for clojure-project. Thanks to Phil (the creator of ESK & clojure-project) for pointing this out!


24
Feb 09

Introducing JSON Stackoverflow

To all stackoverflow.com users, you can now add a nifty widget with your stackoverflow.com reputation and badge count to your website or blog. There is also a JSONP service which will provide you the raw data so that you can customise the display as you like. Head over to json-stackoverflow.appspot.com for all the nitty-gritties.

Feedback is appreciated!

Posted via web from Baishampayan’s Posterous


13
Feb 09

Live cricket scores for your blog made easy

I will keep it short, just add the following Javascript snippet anywhere on your website or blog and you’ll get live cricket scores courtesy  oCricket.com.

The Javascript snippet:

<script src="http://cdn.ocricket.com/assets/score_widget.js" type="text/javascript"></script>

Feedback is appreciated!

Posted via web from Baishampayan’s Posterous


05
Feb 09

Recreating the new GMail buttons

a portion of the background on our buttons here, and discuss some of the iterations we’ve been through so far to get to the current state.

Interesting story of the design process that went behind the new GMail buttons.

Posted via web from Baishampayan’s Posterous


05
Feb 09

Half a second delay caused a 20% drop in traffic

Half a second delay caused a 20% drop in traffic. Half a second delay killed user satisfaction.

Interesting story from Marissa Mayer about an experiment Google conducted.

Posted via web from Baishampayan’s Posterous