CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei....

17
CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei

Transcript of CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei....

Page 1: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

CLOJURE

A PRACTICAL LISP FOR THE JVMLT 54 - Ralph Guderlei

Page 2: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional
Page 3: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

WARUM CLOJURE?LispREPL zur interaktiven Entwicklungsehr gute Java-Interopfunktionaldynamisch typisiert

http://clojure.org/rationale

Page 4: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

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.

Page 5: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

LISPLISt Processor

Lots of Irritating Silly Parenthesis

Erste Version: 1958Dialekte: Scheme (1975), Common Lisp (1984), Clojure (2007)Einfluß auf viele Sprachen: Ruby, Javascript, Smalltalk, ...

Page 6: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

REPL

Page 7: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

SYMBOLIC EXPRESSIONS(operator operand1 operand2 ...)

Operand: Literal (Variable, Datenstruktur, ...) oder S-ExpressionPräfix-NotationCode ist gültige Clojure-Datenstuktur (Homoikonizität) ->Makros

Page 8: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

BEISPIEL: IF(if (< i 0) 0 (do-sth-with i))

Page 9: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

DATENSTRUKTURENBooleans, Strings, Characters, Zahlen, KeywordsMaps

Listen, Vektoren, Sets

{:foo true "bar" 12/3}

Page 10: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

FUNKTIONEN(defn multiply "multiplies two numbers x and y" [x y] (* x y))

implizite returnsDocstring

Page 11: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

JAVA INTEROP(def s (.toUpperCase "foo")) ;; => "FOO"

(doto (new MyEntity) (.setA "foo") (.setB 42))

Page 12: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

JAVA INTEROP - GOODIESDefinition und Implementierung von Interfaces

Polymorphic dispatch

Definition von Typen

(defprotocol)

(defmulti), (defmethod)

(deftype), (defrecord)

Page 13: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

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)))

Page 14: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

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}))

Page 15: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

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

Page 16: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional

Online: Clojure for the Brave and True

Page 17: CLOJURE - eXXcellent solutions · CLOJURE A PRACTICAL LISP FOR THE JVM LT 54 - Ralph Guderlei. WARUM CLOJURE? Lisp REPL zur interaktiven Entwicklung sehr gute Java-Interop funktional