Sunday, November 13, 2011

Clojure Lists, Maps and Sets

Define the unless function on the first line of 'unless.clj' and then, use it in a few expressions:

[clojure] java -cp .:$CLOJURE_HOME/clojure-1.3.0.jar:$CLOJURE_HOME/jline-1.0.jar jline.ConsoleRunner clojure.main
Clojure 1.3.0
user=> (load-file "unless.clj")
nil
"it's good to be 12"
"6 it's good to be 6"
"5 "
nil
user=>

I found out about transactional Clojure in the book Practical Clojure by Luke VanderHart:

user=> (load-file "dosync.clj")
b
c
d
nil
user=> 

Often, single data items don't require names because they are stored in a list, map or set and can be worked on in sequence. In this example, three accounts are stored in the account-info datastructure.

Another test of a loop-thru function coded in loop-thru-maps.clj:

user=> (load-file "loop-thru-maps.clj")
{:balance 11.59, :member-since "2009-22-02", :username "jak"}
{:balance 13.59, :member-since "2009-02-01", :username "zak"}
{:balance 12.59, :member-since "2011-02-02", :username "mak"}
clojure.lang.PersistentHashMap
{:balance 11.59, :member-since "2009-22-02", :username "jak"}
{:balance 13.59, :member-since "2009-02-01", :username "zak"}
{:balance 12.59, :member-since "2011-02-02", :username "mak"}
nil
user=>