this page gives an overview of common closure scripting calls.

The following steps help you write your first clojure script:

1. Get Counterclockwise, the Eclipse plug-in at http://updatesite.counterclockwise.googlecode.com/hg (for more information, see Clojure's getting started page).
2. Then create a new Clojure project and a clojure file.
3. Add the following: (println "Hello, world").
4. Run as > clojure

More detailed info on Clojure syntax, see:

Clojure Philosophy

Clojure is a functional programming language. It provides the tools to avoid mutable state, provides functions as first-class objects, and emphasizes recursive iteration instead of side-effect based looping. Clojure is impure, in that it doesn't force your program to be referentially transparent, and doesn't strive for 'provable' programs. The philosophy behind Clojure is that most parts of most programs should be functional, and that programs that are more functional are more robust.

The naming convention in Clojure is to use all lowercase with hyphens separating words in multi-word names, unlike the Java convention of using camelcase.

Clojure makes heavy use of lazy evaluation. This allows functions to be invoked only when their result is needed. "Lazy sequences" are collections of results that are not computed until needed. This supports the efficient creation of infinite collections.

Common Syntax

Alternatively, see the Clojure Sample Code.



Built in forms







common arithmetic operations







comparisons







comment







functions







Lists

Lists can have zero or more items, enclosed in parentheses. The list's elements are delimited with a space.

vector

immutable, between []

list

immutable, between ()

map

immutable, between {}

sets

immutable, between #{}

hash-map

immutable, between ()

set







get







def 

assoc 

dissoc 

merge- with 

doseq







looping

In the absence of mutable local variables, looping and iteration must take a different form than in languages with built-in for or while constructs that are controlled by changing state. In functional languages looping and iteration are replaced/implemented via recursive function calls. Many such languages guarantee that function calls made in tail position do not consume stack space, and thus recursive loops utilize constant space.







sequence

Sequence is not a data structure but an interface, or view, into a data structure. A sequence can be derived from a collection. The relation between collection and sequence is similar to the relation between database table and database view.


sequence from a map:







 







Java integration







Automated tests








More clojure test examples. More info on clojure tests.

if                 : (                 if                 (=                 2                 2                 ) (println                  "true"                 ) (println                  "false"                 ))                

                                  

                 str: (str                  "Value: "                  5                 )                

                                  

                 println and def:                 

                 (def msg (str                  "this is four: "                  4                 ))                

                                  (println msg) ; output:                  this                 is four:                  4                

                                  

                 do                 : (                 do                 (println                 "Hello."                 ) (println (+                  4                 1                 )))                

                                  

                 when: (when (>                  50                 (inc                 4                 ))                

                                  (println                 "Yes"                 ))                

                                  

                 let: (let [color                  "Red"                 ] (println (str                  "Color is: "                  color)) )



Java integration



General







Static methods







Class and fields







Import







Create instance







Call instance methods







Call Java static methods







Chaining







Implementing interfaces and extending classes (use the reify macro):








(. ( new java.util.Date) (toString))




MySQL

(Alternatively, see the entire clojure MySQL demo code.)



(ns mySQLTutorial           


                        (:require [clojure.contrib.sql :as sql]))           


                        


            (def db {:classname             "com.mysql.jdbc.Driver"           


                        :subprotocol            "mysql"           


                        :subname            "//localhost:3306/dummy"           


                        :user            "duser"           


                        :password            "dpass"            })           


                        


            (defn create-users []           


                        (sql/create-table           


                        :people           


                        [:id :integer             "PRIMARY KEY"             "AUTO_INCREMENT"            ]           


                        [:fname            "varchar(25)"            ]           


                        [:lname            "varchar(25)"            ]           


                        [:town            "varchar(25)"            ]           


                        )           


            )           


                        


            (defn drop-users []           


                        (sql/drop-table :users))           


                        


            ;;;;;;;;;inserting data is accomplished via insert-values function;;;;;;;;           


            (defn insert-user [fname lname town]           


                        (sql/insert-values :people [:fname :lname :town] [fname lname town]))           


                        


            ;;;;;;;;;;;;to update a record           


            (defn update-person [id attribute-map]           


                        (sql/update-values :people [            "id=?"            id] attribute-map))           


            ;;;;;;;;;;delete a record           


            (defn delete-person [id]           


                        (sql/with-connection db            


                        (sql/delete-rows :people [            "id=?"            id])))



Commands



With clojure you can use all the familiar MySQL commands. In the following examples, the commands are wrapped into a def which binds the MySQL command to            'statement'            . Then the command is  processed with:  (sql/with-query-results rs [statement]           


            (defn printAll [table]           


                        (sql/with-connection db            


                        (def statement (str             "select * from "             table ))           


                        (sql/with-query-results rs [statement]            


                        (dorun (map #(println %) rs))))           


            )           


            ;;;;;;;;;;search            for            all who match a last name           


            (defn selectLastname [lastName]           


                        (println            "******************"            )           


                        (sql/with-connection db            


                        (def statement (str             "select * from people where lname=\""             lastName             "\""            ))           


                        (sql/with-query-results rs [statement]            


                        (dorun (map #(println %) rs))))            


            )           


            ;;;;;;;;;;;;;call a function           


            (sql/with-connection db           


            (insert-user            "40"            "Denver"            "Abby"             "Smith"            )



Join



Do a join between two tables (people and alumni) joining on last name             for            a specific last name where the tables are:           


            mysql> select * from people;           


            +---+--------+-------+-----------+           


            | id  | fname  | lname  | town        |           


            +---+--------+-------+-----------+           


            |             1            | Abby    | Smith  | Allenwood |           


            |             2            | Ben      | Brown | Boston      |           


            |             3            | Sandy   | Brown | Chester    |           


            |             4            | Charlie  | Brown | Dover       |           


            +----+-------+-------+-----------+           


            second table:           


            mysql> select * from alumni;           


            +---+-----+--------+-------+-------+           


            | id  | age | college  | fname | lname |           


            +---+-----+--------+-------+-------+           


            |             1            |            30              | Bacon   | Sandy | Brown |           


            |             3            |            21              | Cherry  | Scott  | Brown |           


            |             4            |            40              | Denver  | Abby  | Smith  |           


            +---+-----+--------+-------+-------+           


            (defn selectLastNameTwoTables [table1 table2 lastName]           


                        (sql/with-connection db            


                        (def statement (str             "select "             table1             ".lname, "             table1             ".fname, "             table1             ".town, "             table2             ".college, "             table2             ".age from "             table1            ", "            table2            " where "            table1            ".lname = "            table2            ".lname and "            table1            ".fname = "            table2            ".fname and "            table1            ".lname = '"            lastName            "'"            ))           


            (sql/with-query-results rs [statement]            


            (dorun (map #(println %) rs))))            


            )           


            (selectLastNameTwoTables            "people"            "alumni"            "Brown"            )           


            output:           


            {:lname Brown, :fname Sandy, :town Chester, :college Bacon, :age             30            }



References

http://clojure.blip.tv/?sort=custom;date=;view=archive;user=clojure;nsfw=dc;s=posts;page=1