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
(fn [w x y & z] z) | |
(let [empty-k (with-meta identity {:kont true})] | |
(fn foo | |
;; need a compatibility overload for if someone called | |
;; using only required parameters. | |
([w x y] (foo empty-k w x y)) | |
;; acts as its own compatibility layer, since we can't | |
;; have another variadic overload | |
([k w x y & z] |
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
;; the following function works much like clojure.core/update-in, but | |
;; also works in the case that the nested data structure being traversed | |
;; includes a list from which you want to get the first element. | |
;; it's woefully incapable of handling anything more complex, as the | |
;; aforementioned use case was the only one I had when I wrote it. | |
(defn update-in+ | |
"Similar to clojure.core/update-in, except coll may include lists and | |
key* may include references to clojure.core/first so that lists in coll | |
can be traversed." | |
[coll key* f & arg*] |
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
;; It often makes sense to think about macros as taking names as arguments. | |
;; This is in contrast to functions which take values as arguments. | |
;; Consider this macro (by a user in the #clojure IRC) | |
(defmacro getter [obj nm] | |
`((memfn ~(read-string (str "get" nm))) ~obj)) | |
;; It takes two names, obj and nm. | |
;; Nothing really interesting happens to obj, so let's focus on what happens with nm: | |
(read-string (str "get" nm)) |
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
user=> (symbol "1") | |
1 | |
user=> (number? (symbol "1")) | |
false | |
user=> (symbol? (symbol "1")) | |
true | |
user=> (= (symbol "1") 1) | |
false | |
user=> (= (symbol "1") '1) | |
false |
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 update-game! | |
[system-atom] | |
;; NB: write-only code | |
(let [game-env ((comp (partial apply game-env/update-player) | |
(juxt identity | |
(comp move-player | |
(partial apply player/update-cur-speed) | |
(juxt identity | |
(comp accelerate player/get-cur-speed)) | |
game-env/get-player)) |
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 return-multi [a b] (mapv inc [a b])) | |
(defn accept-multi [x] | |
(let [[a b] (return-multi x x)] | |
(+ a b))) | |
(accept-multi 1) | |
;; -> 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
;; Does this bother anybody else? | |
((fn [x] (let [recur (dec x)] (if (zero? x) 42 (recur recur)))) 5) | |
;;=> 42 | |
;; Similarly | |
((fn [x] (let [recur (fn [y] 350)] (if (zero? x) 42 (recur (dec x))))) 5) | |
;;=> 42 | |
;; Apparently 'recur' in function position *always* refers to the 'recur' special form |
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 extend-multi | |
"Like extend, but takes a set of types/classes which will all be extended with | |
the given protocol and method map pairs using the same syntax as defrecord for | |
protocol function implementations." | |
[atype* & proto+mmap*] | |
(loop [proto+mmap* proto+mmap* | |
fn-map* [] | |
ext-body []] | |
(if (nil? (seq proto+mmap*)) | |
`(let ~fn-map* ~@(map (fn [a] `(extend ~a ~@ext-body)) atype*)) |