Skip to content

Instantly share code, notes, and snippets.

@bendisposto
Created November 29, 2013 11:20
Show Gist options
  • Save bendisposto/7704418 to your computer and use it in GitHub Desktop.
Save bendisposto/7704418 to your computer and use it in GitHub Desktop.
(ns repl.core)
(comment
;; atom
(def foo (atom 2))
foo
(deref foo)
@foo
(def foo 7)
(defn observer [watcher-key watched-reference old-value new-value]
(println "Watcher fired" watched-reference "changed from"
old-value "to" new-value))
(add-watch foo :foo-watch observer)
(swap! foo (fn [x] (* x x)))
(deref foo)
(reset! foo 2)
(deref foo)
;; ref
(def konto1 (atom 100))
(def konto2 (atom 0))
(defn transfer-money [amount]
(when (<= amount (deref konto1))
(do (swap! konto2 #(+ % amount))
(Thread/sleep 5000)
(swap! konto1 #(- % amount)))))
(defn pay-bill [amount]
(when (<= amount (deref konto1))
(println "Payed" amount)
(swap! konto1 #(- % amount))))
(future (transfer-money 10))
[@konto1 @konto2]
(pay-bill 10)
(future (transfer-money 60))
(pay-bill 60)
[@konto1 @konto2]
(def konto1 (ref 100))
(def konto2 (ref 0))
(defn transfer-money [amount]
(dosync
(when (<= amount (deref konto1))
(do (alter konto2 #(+ % amount))
(Thread/sleep 5000)
(alter konto1 #(- % amount))))))
(defn pay-bill [amount]
(dosync (when (<= amount (deref konto1))
(println "Payed" amount)
(alter konto1 #(- % amount)))))
(future (transfer-money 10))
[@konto1 @konto2]
(pay-bill 10)
(future (transfer-money 60))
(pay-bill 60)
[@konto1 @konto2]
;; commute
(def konto1 (ref 10000))
(def konto2 (ref 10000))
(def x-counter (ref 0))
(defn pay-bill [kto amount]
(dosync (when (<= amount (deref kto))
(println "Payed" amount)
(Thread/sleep 2000)
(alter kto #(- % amount))
(alter x-counter inc))))
(pay-bill konto1 10)
[@konto1 @konto2 @x-counter]
(do (future (pay-bill konto1 10))
(future (pay-bill konto2 10)))
(defn pay-bill [kto amount]
(dosync (when (<= amount (deref kto))
(println "Payed" amount)
(Thread/sleep 2000)
(alter kto #(- % amount))
(commute x-counter inc))))
;; agents
(def log-file (atom []))
(defn debug [& words]
(swap! log-file
(fn [x]
(Thread/sleep 1000)
(conj x (apply str (interpose " " words))))))
(debug "Eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll")
(debug "Noch" "eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll")
(time
(do
(debug "Eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll")
(debug "Noch" "eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll")))
(def log-file (agent []))
(defn debug [& words]
(send log-file
(fn [x]
(Thread/sleep 1000)
(conj x (apply str (interpose " " words))))))
(time
(do
(debug "Eine" "Ausgabe," "die" "an" "einem" "Stück" "erfolgen" "soll")
(debug "Noch" "eine" "Ausgabe")))
(do (await log-file) @log-file)
(def ag
(for [x (range 20)] (agent nil)))
(time
(do
(doseq [a ag] (send a (fn [_] (Thread/sleep 1000))))
(doseq [a ag] (await a))))
(time
(do
(doseq [a ag] (send-off a (fn [_] (Thread/sleep 1000))))
(doseq [a ag] (await a))))
;; future & promise
(defn nag [f]
(if (realized? f)
@f
(do (Thread/sleep 100) (println f) (recur f))))
(let [x (future (do (Thread/sleep 2000) 42))]
(print "nagging ")
(println x)
(nag x))
(let [x (future (do (Thread/sleep 2000) 42))]
(print "nagging ")
(println @x)
(nag x))
(defn stupid-fib [n] (if (< n 2) n (+ (stupid-fib (dec n)) (stupid-fib (- n 2)))))
;; 30 31 32 33 34 35 36 37
;; 200 350 550 900 1500 2300 3700 6000 -> 15500
(time (let [fa (for [x (range 30 38)] (future (stupid-fib x)))]
(doseq [f fa] (println @f))))
(time (let [fa (for [x [36 37]] (future (stupid-fib x)))]
(doseq [f fa] (println @f))))
(def x (promise))
(nag x)
(deliver x 10)
@x
;; destructuring
(let [[x y] [1 2]] (println x y))
(let [[x y] [3 4 6]] (println x y))
(let [[x _ y] [1 2 3]] (println x y))
(let [[x _ x] [1 2 3]] (println x))
(let [[x & y] [7 8 9]] (println x y))
(let [[x & y :as l] [1 2 3]] (println x y l))
(let [point (list 4 8)
[x y] point]
(println x y point))
(let [point {:x 100 :y 200}
[x y] point] (println x y))
(let [point {:x 300 :y 500}
{x :x y :y} point] (println x y))
(let [point {:x 100 :y 200}
{x :x y :y :as m } point] (println x y m))
(let [point {:x 410 :y 600}
{:keys [x y] :as m } point] (println x y m))
(let [point {:x -210 :y 7100}
{:keys [x y z] :or {x 0 y 0 z 0} } point] (println x y z))
(let [db [{:name "Bendisposto" :vorname "Jens"} {:name "Leuschel" :vorname "Michael"}]
[{n1 :name} {n2 :name}] db] (println n1 n2))
;; Namespaces
java.io.File
(import java.io.File)
;; require = load
;; Datei $CLASSPATH/repl/greeter/friendly_hello.clj
(require 'repl.greeter.friendly-hello)
(repl.greeter.friendly-hello/msg! "John" "Michael")
;; Alias
(require '[repl.greeter.friendly-hello :as hello])
(require ['repl.greeter.friendly-hello :as 'hello])
(hello/msg! "John" "Jens")
zipper ;; error
clojure.zip/zipper
(require 'clojure.zip)
zipper
clojure.zip/zipper
(refer 'clojure.zip :except '[next])
zipper
;; use = require + refer
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment