Created
          November 21, 2013 13:27 
        
      - 
      
 - 
        
Save bendisposto/7581543 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) | |
| (comment | |
| ;; Atoms | |
| (def foo (atom {})) | |
| foo | |
| (defn store [k v] (swap! foo assoc ,,,, k v)) | |
| ;; (def foo (assoc foo k v)) | |
| (store :x 7) | |
| foo | |
| (:x foo) | |
| (deref foo) | |
| @foo | |
| (:x @foo) | |
| (def counter (atom 0)) | |
| (defn incer [] (swap! counter inc)) | |
| (do | |
| (future (last (repeatedly 10000000 incer))) | |
| @counter) | |
| counter | |
| (defn sleepy-inc [n] | |
| (println "Zzz") | |
| (Thread/sleep 4000) | |
| (println "Whut?") | |
| (inc n)) | |
| (sleepy-inc 6) | |
| (defn v-incer [] (swap! counter sleepy-inc)) | |
| (defn tease [] (let [c (rand-int 1000)] (reset! counter c))) | |
| (defn myreset! [a v] (swap! a (fn [_] v))) | |
| (future (v-incer)) ; repl | |
| (tease) ;repl | |
| (def agent-x | |
| (add-watch counter | |
| :my-awesome-watcher | |
| (fn [k r old new] | |
| (println (str k ": " old " -> " new))))) | |
| (tease) | |
| ;; Refs | |
| (def a (ref 0)) | |
| (def b (ref 0)) | |
| (def agent-y | |
| (add-watch a | |
| :my-a-watcher | |
| (fn [k r old new] | |
| (dosync (println "a is" @a "And b is" @b))))) | |
| (dosync (alter a inc) | |
| (alter b inc)) | |
| (def c (ref 0)) | |
| (def d (ref 0)) | |
| (defn modify-cd [n m] | |
| (alter c (constantly n)) | |
| (alter d (constantly m))) | |
| (modify-cd 1 3) | |
| (defn modify-cd [n m] | |
| (dosync | |
| (alter c (constantly n)) | |
| (alter d (constantly m)))) | |
| (modify-cd 1 3) | |
| (defn expensive [] | |
| (dosync | |
| (alter c sleepy-inc) | |
| (commute d inc))) | |
| (defn teasec [] (let [r (rand-int 1000)] (dosync (alter c (constantly r))))) | |
| (defn teased [] (let [r (rand-int 1000)] (dosync (alter d (constantly r))))) | |
| ;; repl | |
| (future (expensive)) | |
| (teasec) | |
| (teased) | |
| (def konto (ref 100)) | |
| (def creditcard (ref 0)) | |
| (defn resetk [] (dosync (alter konto (constantly 100)) (alter creditcard (constantly 0)))) | |
| (defn withdraw [balance n] (Thread/sleep 4000) (- balance n)) | |
| (def agent-z1 | |
| (add-watch konto | |
| :my-a-watcher | |
| (fn [k r old new] | |
| (dosync (println "Konto:" old "->" new ))))) | |
| (def agent-cc | |
| (add-watch creditcard | |
| :my-a-watcher | |
| (fn [k r old new] | |
| (dosync (println "CC:" old "->" new ))))) | |
| (defn bezahlen [n] | |
| (dosync | |
| (alter creditcard | |
| (fn [cc] | |
| (if (pos? (- (+ @konto cc) n)) | |
| (withdraw cc n) | |
| cc))))) | |
| (defn teasek [n] | |
| (dosync | |
| (ensure creditcard) | |
| (alter konto | |
| (fn [k] | |
| (if (pos? (- (+ k @creditcard) n)) | |
| n | |
| k))))) | |
| ;; repl | |
| (future (bezahlen 50)) | |
| (teask 10) | |
| ;; Beide Transaktionen schreiben unterschiedliche Werte | |
| ;; => beide Transaktionen werden gleichzeitig ausgeführt | |
| ;; => Invariante wird verletzt | |
| (defn bezahlen [n] | |
| (dosync | |
| (ensure konto) | |
| (alter creditcard | |
| (fn [cc] | |
| (if (pos? (- (+ @konto cc) n)) | |
| (withdraw cc n) | |
| cc))))) | |
| (future (bezahlen 50)) | |
| (teask 10) | |
| ;; Beide Transaktionen schreiben unterschiedliche Variablen, | |
| ;; benutzen aber ensure wechselseitig => | |
| ;; Die Transaktionen werden nacheinander ausgeführt => | |
| ;; Keine Invariantenverletzung | |
| ) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment