Wednesday, 9 January 2013

clojure and lein REPL 101

I'm interested in clojure a bit because of its functional approach borrowed from LISP. Its a constant learning obviously. Im writing about basic 101 stuffs here.

 clojure has a build tool leiningen is a that helps bootstrapping.

It installs its dependencies upon the first run on unix, so the first run will take longer.

STEP 1 : download lein and add to path
$ wget https://raw.github.com/technomancy/leiningen/stable/bin/lein > /usr/bin/lein
$ sudo chmod +x /usr/bin/lein

My PATH reads as given below :
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/java/jdk1.6.0_23/bin


STEP 2 : start REPLa simple Interactive prog env
REPL helps while learning a new programming lang, for example the groovy, scala also have implementation of REPL.

using lein repl

$ lein repl
REPL started; server listening on localhost port 63090
user=> (println *clojure-version*)
{:major 1, :minor 2, :incremental 1, :qualifier }
nil
user=> (+ 1 3)
4

user=> (println "Thread: " (.getName (Thread/currentThread)))
Thread:  nREPL-worker-0
nil

user=> (clojure-version)
"1.2.1"
user=> (inc 1)
2
user=>

I can connect to above REPL from another machine as below
$ lein repl :connect 50924
Connecting to nREPL at 127.0.0.1:50924
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0

inc returns a number one greater than num.

STEP 3 clojure collections

clojure.lang.PersistentVector

user=> (class ["order-1", "order-2"])
clojure.lang.PersistentVector

user=> (count ["order-1", "order-2", "order-3"])
3

user=> (reverse ["prayagupd", "ocean", "seattle"])
("seattle" "ocean" "prayagupd")

;; access

user=> (get ["cancer" "diabetes" "anxiety"] 1)
"diabetes"

user=> (assoc ["cancer" "diabetes" "anxiety"] 2 "social anxiety")
["cancer" "diabetes" "social anxiety"]

;; map
user=> (map #(+ % 3) [1, 2, 3]);
(4 5 6)

user=> (mapv #(str "Mental condition - " % "!" ) ["Social anxiety" "dysthymia" "PTSD"])
["Mental condition - Social anxiety!" "Mental condition - dysthymia!" "Mental con

user=> (partition 2 1 [1 2 3 4])
((1 2) (2 3) (3 4))

user=> (interleave [1 2 3] ["Social anxiety" "dysthymia" "PTSD"])
(1 "Social anxiety" 2 "dysthymia" 3 "PTSD")

Seq/ lazy Seq


user=> (map #(str "ordered sku is " %) ["sku-1" "sku-2" "sku-3"]) 
("ordered sku is sku-1" "ordered sku is sku-2" "ordered sku is sku-3")

user=> (class (map #(str "ordered sku is " %) ["sku-1" "sku-2" "sku-3"]))
clojure.lang.LazySeq


user=> (doseq [order ["order-1", "order-2", "order-3"]] 
  #_=> (println (str order " is about to be supplied.")))
order-1 is about to be supplied.
order-2 is about to be supplied.
order-3 is about to be supplied.
nil

hash-set

user=> (hash-set "social anxiety" "PTSD" "dysthymia")
#{"PTSD" "social anxiety" "dysthymia"}

user=> (conj (hash-set "social anxiety" "PTSD" "dysthymia") "depression")
#{"depression" "PTSD" "social anxiety" "dysthymia"}

user=> (disj (hash-set "social anxiety" "PTSD" "dysthymia") "PTSD")
#{"social anxiety" "dysthymia"}

user=> (contains? (hash-set "social anxiety" "PTSD" "dysthymia") "PTSD")
true

hash-map

user=> (def purchaseorder (hash-map :order-number 1234,
  #_=>     :scheduled-date 20171028,
  #_=>     :fulfillment-centre "Iowa Fulfillment Centre ",
  #_=>     :supplier "Native USA",
  #_=>     :order-skus (list "sku1" "sku2" "sku3")))
#'user/purchaseorder

user=> (:order-number purchaseorder)
1234

user=> (some #(= % "sku1") (:order-skus purchaseorder))
true

user=> (type purchaseorder)
clojure.lang.PersistentHashMap

user=> (class purchaseorder)
clojure.lang.PersistentHashMap

fp = Var + fn

user=> (defn receive [item] (println (str item " is received")))
#'user/receive

user=> (defn scan [item] (println (str item " is scanned")))
#'user/scan

user=> (defn update-inventory [item] (println (str "inventory for " item " is updated")))
#'user/update-inventory

user=> (def receiving (comp update-inventory scan receive))
#'user/receiving

user=> (receiving "item1")
item1 is received
 is scanned
inventory for  is updated
nil

References
[1] http://leiningen.org/#install

[2] How to install Clojure on Ubuntu 10.04 from Github repo with no clojure.jar, http://stackoverflow.com/a/5984183/432903

[3] http://clojure.org/getting_started

[4] Using Leiningen to build Clojure code, http://alexott.net/en/clojure/ClojureLein.html#sec2

[5] Striving to Make Things Simple and Fast - Phil Bagwell, http://www.youtube.com/watch?v=K2NYwP90bNs

[6] http://www.se-radio.net/2010/03/episode-158-rich-hickey-on-clojure/

No comments:

Post a Comment