-
-
Save sundarj/188e3e566f6872b1fd3bfe4db4fcce29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) |
Author
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!