Created
          December 13, 2013 07:02 
        
      - 
      
 - 
        
Save bendisposto/7940775 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 | |
| ;; --- Wiederholung | |
| ;; Vars & Concurrency | |
| (defn printv [n] (println n *clojure-version*)) | |
| ;; emacs bug -> nrepl-server-repl öffnen | |
| (do | |
| (binding [*clojure-version* 42] | |
| (new-thread #(printv "t3")) | |
| (printv "t4")) | |
| (printv "t5")) | |
| ;; Evaluation von Symbolen | |
| ;; 1) qualified symbols | |
| (def java.io.Writer 101) | |
| repl.core/java.io.Writer | |
| java.io.Writer | |
| ;; 2) Java-Class | |
| (let [java.io.Writer 12] java.io.Writer) ;; error | |
| ;; 3) locals | |
| (def foo 10) | |
| (let [foo 11] foo) | |
| (let [foo 11] repl.core/foo) | |
| ;; 4) Symbols | |
| foo | |
| ;; 5) Error | |
| explode! | |
| ;; 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* ) | |
| ;; Interop | |
| (import java.util.Random) | |
| (def r (Random.)) | |
| Math/PI | |
| (.. r nextInt toString hashCode) | |
| (-> [1 2 3 4] (conj ,,, 5) reverse) | |
| (->> [1 2 3 4] (map inc ,,,) (filter even? ,,,) (reduce + ,,,)) | |
| (defrecord Foo [x y z]) | |
| (Foo. 1 2 3) | |
| (def x (->Foo 2 3 4)) ;; besser ! | |
| (type x) | |
| (defn kehrwert [x] | |
| (try (/ 1 x) | |
| (catch Exception e :inf) | |
| (finally (println "done.")))) | |
| ;; 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]) | |
| (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! | |
| ;; ----------------------------------------------------------------------------------------------- | |
| ;; Nachtrag: Agents & Refs | |
| (def k1 (ref 100000)) | |
| (def k2 (ref 0)) | |
| (defn transfer [f t a] | |
| (dosync | |
| (when (< a @f) | |
| (println "transfering") | |
| (alter f #(- % a)) | |
| (Thread/sleep 5000) | |
| (alter t #(+ % a))))) | |
| (transfer k1 k2 10) | |
| (def se (agent nil)) | |
| (defn transfer [f t a] | |
| (dosync | |
| (when (< a @f) | |
| (send se (fn [_] (println "transfering"))) | |
| (alter f #(- % a)) | |
| (Thread/sleep 5000) | |
| (alter t #(+ % a))))) | |
| (transfer k1 k2 10) | |
| ;; ----------------------------------------------------------------------------------------------- | |
| ;; Slides | |
| ;; ----------------------------------------------------------------------------------------------- | |
| (defrecord KlausurAufgabe [aufgabe punkte]) | |
| (def a (->KlausurAufgabe 1 10)) | |
| (class a) | |
| ;; multimethod = multi + method | |
| (defmulti get-columns class) ;; zeile | |
| (defmethod get-columns KlausurAufgabe [_] ["Aufgabe","Punkte"]) ;; feld | |
| (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) | |
| (class nil) | |
| (defmethod get-values nil [_] ["empty"]) | |
| (get-values nil) | |
| (get-values -3) | |
| (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 5 6]) | |
| ;; 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