Skip to content

Instantly share code, notes, and snippets.

@nwallace
Last active April 10, 2018 12:34
Show Gist options
  • Save nwallace/d303c2e62d589c563805b7dac3215493 to your computer and use it in GitHub Desktop.
Save nwallace/d303c2e62d589c563805b7dac3215493 to your computer and use it in GitHub Desktop.
;; This is a demonstration of a question I have about Clojure
;; I want a function like `map-kv`, but I'm not aware of one that behaves this way
;; My hypothetical `map-kv` works like Underscore.js/Lodash.js `map` works
(defn classify-nums [hash-or-vector]
(map-kv (fn [key-or-index value]
(if (odd? value)
"is odd"
"is even"))))
(classify-nums {:a 1 :b 3 :c 4}) ; => {:a "is odd" :b "is odd" :c "is even}
(classify-nums [1 3 4]) ; => {0 "is odd" 1 "is odd" 2 "is even"}
;; I got the answer! Thanks to luma on Freenode #clojure -- `reduce-kv` https://clojuredocs.org/clojure.core/reduce-kv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment