Older blog entries for rkrishnan (starting at number 251)

Embracing maven

After several failed attempt to understand maven and going back and forth between lein and maven, I have finally decided to settle on maven. There are several reasons for this. One of the most important one is that Debian has a very well packaged maven and a local repository system. i.e. Debian java packages installed on a machine is available as a local repo. With that, offline builds become quite easy (provided all the dependencies are available as Debian packages). Maven is also well supported by the Java ecosystem. Though XML turns me off as well, maven takes care of generating much of the boilerplate for us.

I will walk through creating a trivial clojure project using maven. I assume that maven is already installed on your machine.

First, run the following command:

$ mvn archetype:create -DgroupId=in.clj.hello -DartifactId=hello

This gives the following output on my machine: [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:create] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:create {execution: default-cli}] [WARNING] This goal is deprecated. Please use mvn archetype:generate instead [INFO] Defaulting package to group ID: in.clj.hello [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: in.clj.hello [INFO] Parameter: packageName, Value: in.clj.hello [INFO] Parameter: package, Value: in.clj.hello [INFO] Parameter: artifactId, Value: hello [INFO] Parameter: basedir, Value: /tmp/hello-mvn [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: /tmp/hello-mvn/hello [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 second [INFO] Finished at: Wed Apr 21 22:13:22 IST 2010 [INFO] Final Memory: 15M/150M [INFO] ------------------------------------------------------------------------

A directory called hello is created. Let us look at the parameters to archetype:create. groupId is a unique way to identify the module. Think of it as the namespace. artifactId is the name of the project. groupId:artifactId:version uniquely identify an ‘artifact’. By default, maven will create a version number 1.0-SNAPSHOT for the project, if it is omitted from commandline.

The directory tree at this point looks like this:

$ tree
.
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- in
    |           `-- clj
    |               `-- hello
    |                   `-- App.java
    `-- test
        `-- java
            `-- in
                `-- clj
                    `-- hello
                        `-- AppTest.java
Let us get rid of the java files first. $ rm -rf src/main/java $ rm -rf src/test Create the following directory structure: $ mkdir -p src/main/clojure/in/clj Note that we have created the structure according to our groupId specification. Create a file called hello.clj at the leaf of this directory and put the following contents inside it:
REXML could not parse this XML/HTML: 
<pre>
(ns in.clj.hello)

(defn greet & args (apply str args)) </pre>

Now, open the pom.xml file. Some changes needs to be done here so that maven knows that we are building a clojure project and the dependencies. The following things are done:

  1. The dependency on junit is removed.
  2. A dependency on clojure is added.
  3. A new plugin clojure-maven-plugin is added and configured.
  4. Repositories from where, dependencies are fetched, are added.
At the end of this process, the pom.xml looks like this:
REXML could not parse this XML/HTML: 
<pre>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">project</span> <span class="nxml-namespace-attribute-xmlns">xmlns</span>=<span class="nxml-namespace-attribute-value-delimiter">"</span><span class="nxml-namespace-attribute-value">http://maven.apache.org/POM/4.0.0</span><span class="nxml-namespace-attribute-value-delimiter">"</span> <span class="nxml-namespace-attribute-xmlns">xmlns</span><span class="nxml-namespace-attribute-colon">:</span><span class="nxml-namespace-attribute-prefix">xsi</span>=<span class="nxml-namespace-attribute-value-delimiter">"</span><span class="nxml-namespace-attribute-value">http://www.w3.org/2001/XMLSchema-instance</span><span class="nxml-namespace-attribute-value-delimiter">"</span>
  <span class="nxml-attribute-prefix">xsi</span><span class="nxml-attribute-colon">:</span><span class="nxml-attribute-local-name">schemaLocation</span>=<span class="nxml-attribute-value-delimiter">"</span><span class="nxml-attribute-value">http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd</span><span class="nxml-attribute-value-delimiter">"</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">modelVersion</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">4.0.0</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">modelVersion</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">in.clj.hello</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">hello</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">packaging</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">jar</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">packaging</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">1.0-SNAPSHOT</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">name</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">hello</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">name</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">http://maven.apache.org</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">description</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">project to demonstrate maven</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">description</span><span class="nxml-tag-delimiter">&gt;</span>
<
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">plugins</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">plugin</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">com.theoryinpractise</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">clojure-maven-plugin</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">1.3.2</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">executions</span><span class="nxml-tag-delimiter">&gt;</span>
      <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">execution</span><span class="nxml-tag-delimiter">&gt;</span>
        <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">compile-clojure</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span>
        <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">phase</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">compile</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">phase</span><span class="nxml-tag-delimiter">&gt;</span>
        <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">goals</span><span class="nxml-tag-delimiter">&gt;</span>
          <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">goal</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">compile</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">goal</span><span class="nxml-tag-delimiter">&gt;</span>
        <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">goals</span><span class="nxml-tag-delimiter">&gt;</span>
      <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">execution</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">executions</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">configuration</span><span class="nxml-tag-delimiter">&gt;</span>
      <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">sourceDirectories</span><span class="nxml-tag-delimiter">&gt;</span>
        <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">sourceDirectory</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">src/main/clojure</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">sourceDirectory</span><span class="nxml-tag-delimiter">&gt;</span>
      <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">sourceDirectories</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">configuration</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">plugin</span><span class="nxml-tag-delimiter">&gt;</span>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">plugins</span><span class="nxml-tag-delimiter">&gt;</span>
<<
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">dependency</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">org.clojure</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">groupId</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">clojure</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">artifactId</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">1.1.0</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">version</span><span class="nxml-tag-delimiter">&gt;</span>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">dependency</span><span class="nxml-tag-delimiter">&gt;</span>
<<
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">repository</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">clojure-snapshots</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">http://build.clojure.org/snapshots</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">releases</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">false</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">releases</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">snapshots</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">true</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">snapshots</span><span class="nxml-tag-delimiter">&gt;</span>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">repository</span><span class="nxml-tag-delimiter">&gt;</span>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">repository</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">clojure-releases</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">id</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">http://build.clojure.org/releases</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">url</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">releases</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">true</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">releases</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">snapshots</span><span class="nxml-tag-delimiter">&gt;</span>
    <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span><span class="nxml-text">false</span><span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">enabled</span><span class="nxml-tag-delimiter">&gt;</span>
  <span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">snapshots</span><span class="nxml-tag-delimiter">&gt;</span>
<span class="nxml-tag-delimiter">&lt;</span><span class="nxml-tag-slash">/</span><span class="nxml-element-local-name">repository</span><span class="nxml-tag-delimiter">&gt;</span>
<<
REXML could not parse this XML/HTML: 
</pre>
REXML could not parse this XML/HTML: 
</pre>

Now do: $ mvn package This produces the following output on my machine:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building hello
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/hello-mvn/hello/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [clojure:compile {execution: compile-clojure}]
Compiling in.clj.hello to /tmp/hello-mvn/hello/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/hello-mvn/hello/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: /tmp/hello-mvn/hello/target/hello-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Apr 21 22:57:11 IST 2010
[INFO] Final Memory: 23M/121M
[INFO] ------------------------------------------------------------------------
A jar file is created under the target directory. Now, we add the jar into our classpath and invoke the clojure repl: $ java -cp /usr/share/java/clojure.jar:target/hello-1.0-SNAPSHOT.jar clojure.main Clojure 1.1.0 user=> (use 'in.clj.hello) nil user=> (greet "Hello World") "Hello World" user=> You can also invoke the repl from maven: $ mvn clojure:repl That wasn’t really too hard. With emerging tools around maven like the Polyglot Maven which supports Clojure and a S-expression syntax for writing the POM, we don’t even have to do as much as we did now. I have put this example in this repository for anyone to play with.

Syndicated 2010-04-21 07:00:00 from Ramakrishnan Muthukrishnan

The passionate programmer by Chad Fowler

I just finished reading ”The Passionate Programmer: Creating a Remarkable Career in Software Development” by Chad Fowler. The book is a rehash of the author’s earlier book with a mysterious title.

This book is not strictly in the category of “self help” books meant to give a temporal high. It is a set of advice (54 of them) for any programmer to improve himself. Each of the chapter is only 2 or 3 pages long, so it is quite easy to read and digest. I have put some of my notes on the book in this wiki page. The author had earlier lived in India, apparently managing an outsourced team here and makes some good and correct observations on the general attitudes of Indian software programmers.

The book is worth a read. If you have no time to read the entire book and just need a summary, read my notes.

Syndicated 2010-04-19 07:00:00 from Ramakrishnan Muthukrishnan

Clojure's function composition goodness

Sean Devlin, in the Episode 16 of the Full Disclojure posted some really awesome ways to play with tables. One of the nice things he did is worth writing. Due credits go to Sean for posting the code.

Suppose you have a map of people with their name and ages:

(def from-xml [{:name "Sean" :age 27} {:name "Ross" :age 27} {:name "Brian" :age 22}])

Now, you want to have the name and age of people in this list of those with the age 27. Clojure’s comp function comes to the rescue. comp takes some functions and applies them to the data from right to left. i.e., It takes the right most function, applies it to the input parameters, the result of which goes as input to the next right most function and so on. Let us take one single map and pass it to the comp:

user> ((comp #{33} :age) {:name "Ram" :age 33}) 33

What this does is, it takes the input map, applies the :age function on the map (Clojure’s hashmap keys are functions themselves). The result of this is used to see if it falls in the given set (which just has 33).  Now, sets are functions of their members. Here is an example:

user> (#{33 34} 34) 34 user> (#{33 34} 33) 33 user> (#{33 34} 32) nil user> (#{33} 33) 33

We use this composition as a predicate to the filter function which results in the elegant solution that Sean posted.

user> (filter (comp #{27} :age) from-xml) ({:name "Sean", :age 27} {:name "Ross", :age 27})

Note that we get back maps in the same format, which is really nice in certain situations. Sean’s Episode 16 also has quite a lot of other goodness as well.

Syndicated 2010-04-14 07:00:00 from Ramakrishnan Muthukrishnan

Pragmatic Thinking and Learning

I finished reading ”Pragmatic thinking and learning” by Andy Hunt (who wrote Pragmatic Programmer and other books). If one is really interested in self-improvement, this is the book to grab. For me, most things described in the book were known things, but it got very reinforced because Andy has some magical power to convey ideas in a very convincing way.I highly recommend this book for anyone interested to know more about learning and apply those things into their own daily lives. The book explains the process of learning using the Dreyfus model and how one advances from a beginner to an expert. Ever wondered why you get new ideas while in the bathroom? Have you noticed why you suddenly perform better when you are in the middle of good performers? I have many times noticed that I speak better english when I am talking to a good english speaker and vice versa. This book explains why, using a simplified model of the human brain.

One noteworthy suggestion in the book for every reader is to keep a personal wiki, which I had experimented with, sometime last year, with great success. I especially liked the meditation technique described in the book and can say with some confidence that it works very well. The chapter on “focus” is also very well researched. The book says that each context switch costs about 20 minutes. Interestingly, the book does not talk about the concept of flow.

I greatly enjoyed reading “Pragmatic Programmer: From Journeyman to Master” earlier (despite the extremely bad packing from Flipkart, which made the paper curly, sort of permanantly).

Syndicated 2010-04-01 07:00:00 from Ramakrishnan Muthukrishnan

Power of find and xargs

Doesn’t, the unix commands find, xargs and grep look like map, apply and filter of the functional world? I was looking at the OCW website and downloaded some content from there as a zip file, which had a complicated directory structure. I wanted to flatten the directory structure, so that all the pdfs are in one directory. With find and xargs it turned out to be extremely easy.

$ find ./ -name *.pdf -print | xargs -I xxx cp xxx /tmp

The key is the -I argument to xargs which replaces every occurance of the pattern in the command with the input from stdin.

Also don’t the pipes look like calling a function composition like in f(g(x) as mentioned by this post?

Syndicated 2010-03-17 07:00:00 from Ramakrishnan Muthukrishnan

SICP Challenge

The SICP study group met today. We agreed to do the following:

  • read a section of the book.
  • solve the exercises in the section.
  • see the videos.
  • come with doubts/insights etc for the next meetup.
We agreed to meetup once in 2 weeks.

So, here I am, (re)starting the SICP from today. Hopefully this time I will complete the book. I am very tempted to do the exercises in clojure, so that I won’t copy the code as it is from the text. I am not sure whether future chapters can become difficult for me  because of this, but I think I will give it a try with clojure. Also I believe I will need only a subset of clojure so that use only the fundamentals and no advanced facilities.

Here are some of the available resources that I plan to use:

My solutions will be available in this github repository.

Of course, I/we are not the first to do this. Here are some of the other people who have attempted this and have documented it (much better than I could ever do) on the web.

For those who are using Scheme, this page will be of great help.

Syndicated 2010-03-14 08:00:00 from Ramakrishnan Muthukrishnan

SICP Study group

Some of us in the bangalore-fp group have decided to form a study group on SICP. The main reason behind study groups is that it is a great way to learn any new subject or a literary work. People share what they read, ask questions, have discussions around these questions they ask etc etc. Also regular meetups provide a time tick to the group and is very motivational. If anyone is interested, please contact me or subscribe to bangalore-fp. We will most likely have a meetup this sunday. The venue and time is not yet decided. I will update this post once it is agreed upon. I am not sure whether this will be a “success”. I don’t even know how to measure success (in general, not just in this context). If one person manage to get inspired to complete the entire book, then I guess it can be considered as a success. Update: The meeting is at this place called Jaaga on sunday, 14th March 2010 from 3pm.

Syndicated 2010-03-10 08:00:00 from Ramakrishnan Muthukrishnan

Laptop Power Supply

Common folklore (confirmed by some Norwegian friends) is that heat is not a friend of laptop batteries. The life of a laptop battery tend to reduce in Indian weather. One of the Dell Laptops I bought in early 2008 can now work barely 30 minutes with a fully charged battery.

Linux kernel provides a wealth of information (through ACPI) about the Battery like the make, type, serial number etc. Look under /sys/class/power_supply/BAT0/. This is what I have: ~$ cat /sys/class/power_supply/BAT0/status Unknown ~$ cat /sys/class/power_supply/BAT0/technology Li-ion ~$ cat /sys/class/power_supply/BAT0/type Battery ~$ cat /sys/class/power_supply/BAT0/serial_number 32511 ~$ cat /sys/class/power_supply/BAT0/manufacturer SANYO ~$ cat /sys/class/power_supply/BAT0/model_name 42T4511 ~$ cat /sys/class/power_supply/BAT0/energy_full_design 84240000 ~$ cat /sys/class/power_supply/BAT0/energy_full 64530000 ~$ cat /sys/class/power_supply/BAT0/energy_now 63160000 It looks like, my battery (this is a work laptop, Lenovo Thinkpad T61 running Debian x86-64 unstable) is functioning at a lesser capacity than it is designed. It will be nice to corelate these readings with the actual performance of the battery. I tend to run this machine mostly with AC, but I will try profiling it for battery performance and see how much time these numbers translate to.

Syndicated 2010-03-06 08:00:00 from Ramakrishnan Muthukrishnan

git - renaming branches

I was working on a project recently, which involved cloning another git repository and adding my own changes into it. After I cloned it, I forgot to create and switch to the new branch and do my changes there. So all my changes were in the ‘master’ branch. I rather wanted to have my changes in my own branch. git makes these things trivial. All you have to do is rename your current master:

$ git branch -m <old-branch-name> <new-branch-name>
$ git branch (should show the new name)
Now, do a git log and find out which is the commit before you started making the change and do:
$ git checkout -b <old-branch-name> <commit-sha1>
That’s it and you have the whole thing as you wanted it to be.

Thanks to my friend Anand for the tip on the -m option to git branch. Most git gems seem to be deep embedded in those switches of the sub commands of git.

Syndicated 2010-01-24 08:00:00 from Ramakrishnan Muthukrishnan

insertion sort in clojure

Here is my first cut, non-lazy implementation of insertion sort.

gist id=282146

Syndicated 2010-01-21 08:00:00 from Ramakrishnan Muthukrishnan

242 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!