Skip to content

Instantly share code, notes, and snippets.

@carlisgg
Last active September 28, 2015 04:27
Show Gist options
  • Save carlisgg/1383963 to your computer and use it in GitHub Desktop.
Save carlisgg/1383963 to your computer and use it in GitHub Desktop.
7L7W - Clojure
(defn big [st n] (> (count st) n))
(println (big "lalala" 5))
(defn collection-type [col] (cond
(vector? col) :vector
(list? col) :list
(map? col) :map))
(println (collection-type [1 2 3]))
(println (collection-type (list 1 2 3)))
(println (collection-type {1 2 3 4}))
()
(defmacro unless [tester body other]
(list 'if (list 'not tester) body other))
(println (macroexpand '(unless condition body other)))
(unless (> 1 2) (println "unless executed") (println "not executed"))
(defprotocol Resource
(update [this content])
(visit [this login]))
(defrecord WebPage [url]
Resource
(update [this content] (str "updated " url " with " content))
(visit [this login] (str url " visited by " login))
)
(def google (WebPage. "www.google.es"))
(visit google :myself)
(def accounts (ref []))
(defn add [target account] (concat target account))
(defn credit [target number amount] (map #(if (= (% :number) number) (assoc % :amount (+ (% :amount) amount)) %) target))
(defn debit [target number amount] (map #(if (= (% :number) number) (assoc % :amount (- (% :amount) amount)) %) target))
(dosync (alter accounts add [{:number 12345, :amount 0}]))
(println "before " @accounts)
(dosync (alter accounts credit 12345 5))
(dosync (alter accounts credit 12345 7))
(dosync (alter accounts debit 12345 3))
(println @accounts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment