Freitag, 17. Oktober 2008

Clojure

Clojure is a dynmanic programming language which run on the JVM.  It is also a functional language which offers nice tools for concurrency handling. Since it doesn't give you the imperative fallback like scala it can be hard certain times to express a problem as your used to it. It is also a LISP which means it is essentially build upon a few primitives.


Counting lines in a file


(ns tokenize
(:import (java.io BufferedReader FileReader)))
;# This is a comment?
(defn process-file [file-name line-function counter]
(println " starting from imperative "
 (with-open [rdr (BufferedReader. (FileReader. file-name))]
  (reduce line-function counter (line-seq rdr) ))))
 (defn process-line [acc line]
  (+ acc 1))
(process-file "../../<filepath>" process-line 0)


Simple sum


(defn mysum [base arg1]
 (+ base arg1))
(println " works "(mysum (mysum 1 2) 4))
(time (dotimes [i 5] (println "woha!")))


line counting shorter


(ns tokenize
(:import (java.io BufferedReader FileReader)))
  (defn process-file [file-name]
   (with-open [rdr (BufferedReader. (FileReader. file-name))]
   (doseq [line (line-seq rdr)] (println line))))
(process-file "<filepath>")



Hello Clojure


(println "Hello, world!" (.. System (getProperties) (get "os.name")))
Since Clojure is a functional language and don't offer imperative constructs like loops it takes a bit to get used to but it is definitely  worth a first look. Evaluation concerning how it works out, building bigger systems and what performance issue possibly arise is of course a matter of further investigaion.