Last active
August 29, 2015 14:18
-
-
Save 7even/79609a787a7781209ffc to your computer and use it in GitHub Desktop.
Exercises from "7 languages in 7 weeks".
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
;; Macros | |
(defmacro unless-else | |
[cond if-true if-false] | |
(list 'if cond if-false if-true)) | |
(macroexpand '(unless-else false (println "false") (println "true"))) | |
;;=> (if false (println "true") (println "false")) | |
(unless-else false (println "false") (println "true")) | |
;; false | |
;; Protocols | |
(defprotocol Enumerable | |
(each [e f])) | |
(defrecord Array [elements] | |
Enumerable | |
(each [_ f] | |
(map #(do (f %) %) elements))) | |
(def array (Array. [1 2 3])) | |
(each array #(println % "+ 1 =" (inc %))) | |
;; 1 + 1 = 2 | |
;; 2 + 1 = 3 | |
;; 3 + 1 = 4 | |
;;=> (1 2 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment