Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. (def inc-coll (partial map inc))
  2. ;;#'user/inc-coll
  3. (inc-coll [4 5 6])
  4. ;;(5 6 7)
  5. (def inc-coll-coll (partial map inc-coll))
  6. ;;#'user/inc-coll-coll
  7. (inc-coll-coll [[1] [2]])
  8. ;;((2) (3))
  9. (inc-coll-coll [[1] 3])
  10. ;;IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542)
  11.  
  12. (defn inc2 [n] (cond (sequential? n) (inc-coll n) (number? n) (inc n) :else n))
  13. ;;#'user/inc2
  14.  
  15. (def inc-coll-coll (partial map inc2))
  16. ;;#'user/inc-coll-coll
  17. (inc-coll-coll [[1] [2]])
  18. ;;((2) (3))
  19. (inc-coll-coll [[1] [2] 3])
  20. ;;((2) (3) 4)
  21.  
  22.  
  23. ;;without conditionals
  24. (defmulti inc2 class)
  25. ;;#'user/inc2
  26. (defmethod inc2 Number [n] (inc n))
  27. ;#object[clojure.lang.MultiFn 0x155f5b43 "clojure.lang.MultiFn@155f5b43"]
  28. (inc2 10)
  29. ;11
  30.  
  31. (defmethod inc2 clojure.lang.Sequential [c] (map inc c))
  32. ;;#object[clojure.lang.MultiFn 0x155f5b43 "clojure.lang.MultiFn@155f5b43"]
  33. (inc2 [1])
  34. ;(2)
  35. (inc-coll-coll [[1] [2] 3])
  36. ;((2) (3) 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement