Last active
July 22, 2017 06:31
-
-
Save gdeer81/02484a9e9f6631cea3a381bce40e1d40 to your computer and use it in GitHub Desktop.
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 inc-coll (partial map inc)) | |
;;#'user/inc-coll | |
(inc-coll [4 5 6]) | |
;;(5 6 7) | |
(def inc-coll-coll (partial map inc-coll)) | |
;;#'user/inc-coll-coll | |
(inc-coll-coll [[1] [2]]) | |
;;((2) (3)) | |
(inc-coll-coll [[1] 3]) | |
;;IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542) | |
(defn inc2 [n] (cond (sequential? n) (inc-coll n) (number? n) (inc n) :else n)) | |
;;#'user/inc2 | |
(def inc-coll-coll (partial map inc2)) | |
;;#'user/inc-coll-coll | |
(inc-coll-coll [[1] [2]]) | |
;;((2) (3)) | |
(inc-coll-coll [[1] [2] 3]) | |
;;((2) (3) 4) | |
;;without conditionals | |
(defmulti inc2 class) | |
;;#'user/inc2 | |
(defmethod inc2 Number [n] (inc n)) | |
;#object[clojure.lang.MultiFn 0x155f5b43 "clojure.lang.MultiFn@155f5b43"] | |
(inc2 10) | |
;11 | |
(defmethod inc2 clojure.lang.Sequential [c] (map inc c)) | |
;;#object[clojure.lang.MultiFn 0x155f5b43 "clojure.lang.MultiFn@155f5b43"] | |
(inc2 [1]) | |
;(2) | |
(inc-coll-coll [[1] [2] 3]) | |
;((2) (3) 4) |
You can also do this with Specter:
user=> (require '[com.rpl.specter :as s])
nil
user=> (def numbers (s/walker number?))
#'user/numbers
user=> (s/transform numbers inc [1 2 3])
[2 3 4]
user=> (s/transform numbers inc [[1 2] [3 4 5]])
[[2 3] [4 5 6]]
user=> (s/transform numbers inc [[1 2] [3 4 5] 6 [7]])
[[2 3] [4 5 6] 7 [8]]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i just found out you can easily generalise it to nthly-nested seqs by using a recursive multimethod:
pretty awesome if you ask me!