Last active
September 28, 2015 04:27
-
-
Save carlisgg/1383963 to your computer and use it in GitHub Desktop.
7L7W - Clojure
This file contains 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
(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})) | |
() |
This file contains 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
(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) |
This file contains 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
(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