Clojure
Published: 13:12, Thursday 14 July 2011
Notes
What's this? See my article about Notes.
basics:
(println "hello world")
extension: .clj
list: (list a b c) or '(a b c)
symbols: 'x x (def x sth)
keywords: :keyword
vectors: [] [1 2 3] or (vector 1 2 3) no operations and different performance
maps:
(def map {"a" 1, "b" 2})
(get map "a") returns 1
(def map {:a 1, :b 2})
(:a map) returns 1
defining variables:
(def x 5)
anonymous functions:
((fn [args] (expr)) args)
functions:
objects that can be called
(defn funcname [args] expressions)
same as (def funcname (fn [args] expressions))
with documentation:
(defn func "doc" [args] expressions)
or (defn func {:doc "doc"} [args] expr)
documentation:
(doc form)
if:
(if true "yes")
(if "" "yes") returns "yes"
when:
no else, many expressions
(when true expr1 expr2 ...)
let:
temporary values to use inside
(let [color "red" phrase (str "color is: " color)] (str "Clojure says: " phrase))
java integration:
instantiate: (new Object arg1 arg2 ...)
call: (. (new Object) (methodname arg1 arg2 ...))
import: (import '(package Class1 Class2))
loops:
(loop [i 0] (when (< i 5) expr1 expr2 (recur (inc i))))
(dorun (for [i (range 0 5)] expr))
(doseq [i (range 0 5)] expr)
functions:
first
rest
concatenate: str
+
=
sequences:
(seq [1 2 3])
(cons 1 [2 3])
Write a Comment
Name:
*
Email:
Website:
If you are human write 't':
*
Title:
*
Your comment:
*
* These fields are mandatory.
© Copyright 2009-2011 Nicola Marcacci Rossi