Created
December 15, 2014 13:01
-
-
Save bendisposto/90b6cb83720deff3ff3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns repl.core) | |
| (defn new-thread [f] (.start (Thread. f))) | |
| (comment | |
| ;; Keywords | |
| :foo | |
| ::foo | |
| ;; Wdh.: Evaluation von Symbolen | |
| ;; 1) qualified symbols | |
| ;; 2) Java-Class | |
| ;; 3) locals | |
| ;; 4) globals | |
| ;; 5) Error | |
| ;; Wdh.: vars | |
| ;; Namespaces assoziieren Symbole mit vars | |
| ;; Vars sind concurrency Konstrukte, die auf einen Wert im Speicher zeigen | |
| (def v 4) | |
| (defn foo [x] x) | |
| (def *foo* 17) | |
| *foo* | |
| (def ^:dynamic *foo* 17) | |
| (def x (with-meta {} {:type String})) | |
| (type x) | |
| (class x) | |
| ;; Apropos vars und concurrency | |
| *clojure-version* | |
| (defn printv [ctx] | |
| (println ctx *clojure-version*)) | |
| (printv "root") | |
| (binding [*clojure-version* :bar] | |
| (println *clojure-version*)) | |
| (let [*clojure-version* :foo] | |
| (println *clojure-version*)) | |
| (let [*clojure-version* :bar] | |
| (printv "let")) | |
| (binding [*clojure-version* :bar] | |
| (printv "binding")) | |
| (binding [foo 10] foo) | |
| (binding [*foo* 10] *foo*) | |
| ;; emacs bug -> nrepl-server-repl öffnen | |
| (defn new-thread [f] (.start (Thread. f))) | |
| (new-thread | |
| #(printv "t1")) | |
| (binding [*clojure-version* :bar] | |
| (new-thread #(printv "t2"))) | |
| ;; Quiz: was gibt der Code aus? | |
| (do | |
| (binding [*clojure-version* 42] | |
| (new-thread #(printv "t3")) | |
| (printv "t4")) | |
| (printv "t5")) | |
| ;; Wdh.: Namespaces = Sammlung von Maps | |
| (type *ns*) | |
| (ns-publics *ns*) ;; add: def, remove: ns-unmap | |
| (ns-interns *ns*) ;; add: def-, remove: ns-unmap | |
| (ns-imports *ns*) ;; add: import, remove: nope | |
| (ns-aliases *ns*) ;; add: require + :as, remove: ns-unalias | |
| (ns-refers *ns*) ;; add: refer, remove: ns-unmap | |
| (ns-map *ns*) | |
| ;; Wdh.: Interop | |
| (import java.util.Random) | |
| (def r (Random.)) | |
| (new Random) | |
| Math/PI | |
| (.. r nextInt toString hashCode) | |
| (-> [1 2 3 4] (conj ,,, 5) reverse) | |
| (->> [1 2 3 4] (map inc ,,,) (filter even? ,,,) (reduce + ,,,)) | |
| ;; Mehr Interop | |
| ;; Exception handling | |
| (defn kehrwert [x] | |
| (try (/ 1 x) | |
| (catch Exception e :inf) | |
| (finally (println "done.")))) | |
| (kehrwert 2) | |
| (kehrwert 0) | |
| (defn rodäng!!! [] (throw (RuntimeException. "RodängDBException"))) | |
| (def e (RuntimeException. "RodängDBException")) | |
| (map #(instance? % e) [String Throwable Exception RuntimeException]) | |
| ;; blank? | |
| (defn blank? [cs] | |
| (every? #(Character/isWhitespace %) cs)) | |
| (defn blank? | |
| "True if s is nil, empty, or contains only whitespace." | |
| {:added "1.2"} | |
| [^CharSequence s] | |
| (if s | |
| (loop [index (int 0)] | |
| (if (= (.length s) index) | |
| true | |
| (if (Character/isWhitespace (.charAt s index)) | |
| (recur (inc index)) | |
| false))) | |
| true)) | |
| ;; ----------------------------------------------------------------------------------------------- | |
| ;; Performance tuning: type hints | |
| (set! *warn-on-reflection* true) | |
| (defn len [x] | |
| (.length x)) | |
| (len "foo") | |
| (len [1 2 3 4]) | |
| (time | |
| (->> (repeat 500000 "foo") (map len) (reduce + ,,,))) | |
| (defn len [^String x] | |
| (.length x)) | |
| (time | |
| (->> (repeat 500000 "foo") (map len ,,,) (reduce +))) | |
| (len "foo") | |
| (len [1 2 3]) ;; achtung! | |
| (set! *warn-on-reflection* false) | |
| ;; Records | |
| ;; Maps mit Konstruktor | |
| (defrecord Foo [x y z]) | |
| (Foo. 1 2 3) | |
| (def x (->Foo 2 3 4)) ;; besser ! | |
| (class x) | |
| (assoc x :c 4) | |
| (.-y x) | |
| ;; ----------------------------------------------------------------------------------------------- | |
| ;; Slides | |
| ;; ----------------------------------------------------------------------------------------------- | |
| (defrecord KlausurAufgabe [aufgabe punkte]) | |
| (def a (->KlausurAufgabe 1 10)) | |
| ;; multimethod = multi + method | |
| (defmulti get-columns class) ;; Abstraction | |
| (get-columns a) ;; error | |
| (defmethod get-columns KlausurAufgabe [_] ["Aufgabe","Punkte"]) | |
| (get-columns a) | |
| (defmulti get-values class) | |
| (defmethod get-values KlausurAufgabe [k] [(:aufgabe k) (:punkte k)]) | |
| (get-values a) | |
| (def b ["1","2"]) | |
| (get-values b) ;; error | |
| (class b) | |
| (defmethod get-columns clojure.lang.PersistentVector [k] k) | |
| (defmethod get-values clojure.lang.PersistentVector [k] k) | |
| (get-values b) | |
| (get-values a) | |
| (class nil) | |
| (defmethod get-values nil [_] ["empty"]) | |
| (get-values nil) | |
| (get-values -3) | |
| (defmethod get-values Long [_] ["Zahl"]) | |
| ;; flexibles Dispatching | |
| (defmulti cred (fn [c] (> 5 (count c)))) | |
| (defmethod cred true [c] (println "Kurze Liste")) | |
| (defmethod cred false [c] (println "Lange Liste!!!")) | |
| (cred [1 2 3 4]) | |
| (cred [1 2 3 4 5]) | |
| (supers (type {})) | |
| (defmulti bar type) | |
| (defmethod bar clojure.lang.IPersistentMap [_] :map) | |
| (defmethod bar :foo [_] :foo) | |
| (bar {:a 1}) | |
| (def x ^{:type :foo} {}) | |
| (def x2 {}) | |
| (bar x2) | |
| (class x) | |
| (type x) | |
| ;; Dispatching on multiple inputs | |
| (defmulti encounter (fn [a b] [(:species a) (:species b)])) | |
| (defmethod encounter [:bunny :lion] [x y] :run-away) | |
| (defmethod encounter [:lion :lion] [x y] :fight ) | |
| (defmethod encounter [:bunny :bunny] [x y] :mate) | |
| (defmethod encounter [:lion :bunny] [x y] :omnomnom) | |
| (def simba {:species :lion}) | |
| (def clarence {:species :lion}) | |
| (def bugs {:species :bunny}) | |
| (def donnie {:species :bunny}) | |
| (encounter simba bugs) | |
| (encounter clarence simba) | |
| (defmulti m1 class) | |
| (defmethod m1 clojure.lang.PersistentVector [k] (count k)) | |
| (defn m2 [k] (count k)) | |
| (time (dotimes [i 5000000] (m1 [1]))) | |
| (time (dotimes [i 5000000] (m2 [1]))) | |
| ;; ----------------------------------------------------------------------------------------------- | |
| ;; Slides | |
| ;; ----------------------------------------------------------------------------------------------- | |
| (defprotocol TableData | |
| (get-columns [this]) | |
| (get-values [this])) | |
| (extend-type KlausurAufgabe | |
| TableData | |
| (get-columns [_] | |
| ["Points","Id"]) | |
| (get-values [this] | |
| [(:punkte this) (:aufgabe this)])) | |
| (get-values (->KlausurAufgabe 1 30)) | |
| (defrecord KlausurErgebnis [matrikel punkte note] | |
| TableData | |
| (get-columns [_] | |
| ["Matrikelnummer", "Note"]) | |
| (get-values [t] | |
| [(:matrikel t) (:note t)])) | |
| (get-values (->KlausurErgebnis "12345" 100 1.0)) | |
| (extend-protocol TableData | |
| clojure.lang.PersistentVector | |
| (get-columns [t] (range 0 (count t))) | |
| (get-values [t] t) | |
| nil | |
| (get-columns [_] :nix) | |
| (get-values [_] :garnix!)) | |
| (get-columns [1 2]) | |
| (get-values [6 19]) | |
| (get-values nil) | |
| (extend-type String | |
| TableData | |
| (get-columns [s] (range (count s))) | |
| (get-values [s] (reverse s))) | |
| (get-values "df") | |
| (defprotocol TheCount | |
| (cnt [v])) | |
| (extend java.util.Collection | |
| TheCount | |
| {:cnt (fn [k] (count k))}) | |
| (time (dotimes [i 5000000] (m2 [1]))) | |
| (time (dotimes [i 5000000] (cnt [1]))) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment