CLOJURE
A PRACTICAL LISP FOR THE JVMLT 54 - Ralph Guderlei
WARUM CLOJURE?LispREPL zur interaktiven Entwicklungsehr gute Java-Interopfunktionaldynamisch typisiert
http://clojure.org/rationale
Eric S. Raymond
Lisp is worth learning for the profoundenlightenment experience you will have whenyou finally get it; that experience will make you abetter programmer for the rest of your days, evenif you never actually use Lisp itself a lot.
LISPLISt Processor
Lots of Irritating Silly Parenthesis
Erste Version: 1958Dialekte: Scheme (1975), Common Lisp (1984), Clojure (2007)Einfluß auf viele Sprachen: Ruby, Javascript, Smalltalk, ...
REPL
SYMBOLIC EXPRESSIONS(operator operand1 operand2 ...)
Operand: Literal (Variable, Datenstruktur, ...) oder S-ExpressionPräfix-NotationCode ist gültige Clojure-Datenstuktur (Homoikonizität) ->Makros
BEISPIEL: IF(if (< i 0) 0 (do-sth-with i))
DATENSTRUKTURENBooleans, Strings, Characters, Zahlen, KeywordsMaps
Listen, Vektoren, Sets
{:foo true "bar" 12/3}
FUNKTIONEN(defn multiply "multiplies two numbers x and y" [x y] (* x y))
implizite returnsDocstring
JAVA INTEROP(def s (.toUpperCase "foo")) ;; => "FOO"
(doto (new MyEntity) (.setA "foo") (.setB 42))
JAVA INTEROP - GOODIESDefinition und Implementierung von Interfaces
Polymorphic dispatch
Definition von Typen
(defprotocol)
(defmulti), (defmethod)
(deftype), (defrecord)
REALES BEISPIEL(defroutes app-routes (context "/items" [] (defroutes items-routes (GET "/" [] (get-all-items)) (POST "/" {body :body} (create-item body)) (context "/:id" [id] (defroutes item-routes (PUT "/" {body :body} (update-item id body)) (DELETE "/" [] (delete-item id)))))))
(def app (-> (handler/api app-routes) (middleware/wrap-json-body) (middleware/wrap-json-response)))
REALES BEISPIEL(defn get-all-items [] (let [conn (mg/connect) db (mg/get-db conn "lt")] {:body (mc/find-maps db "items")}))
(defn create-item [todo-item] (let [conn (mg/connect) db (mg/get-db conn "lt") oid (ObjectId.)] (mc/insert db "items" (merge todo-item {:_id oid})) {:status 201}))
FAZITsehr einfache und konsistente SyntaxREPL: schelles Feedback bei der Entwicklunginteressante Bibliotheken (REST, core.async, core.logic)gute Ergänzung zu Java (funktional, dynamische Typisierung)optionale Features: Typisierung, AOT-Compiler, Java-InteropClojureScriptNachteil: IDE-Support