Skip to content

Instantly share code, notes, and snippets.

@7even
Last active August 29, 2015 14:18
Show Gist options
  • Save 7even/79609a787a7781209ffc to your computer and use it in GitHub Desktop.
Save 7even/79609a787a7781209ffc to your computer and use it in GitHub Desktop.
Exercises from "7 languages in 7 weeks".
;; 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