Last active
July 17, 2018 22:28
-
-
Save dantheobserver/6f4d1cf0b3b890b2bb6f800f03512e83 to your computer and use it in GitHub Desktop.
Data structure Manipulation Examples
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 some-lazy | |
([] (some-lazy 1)) | |
([next] | |
(cons next (lazy-seq | |
(do (println "getting next") | |
(some-lazy (inc next))))))) | |
;; Instead of recur, use the name of the function (e.g. some-lazy) to recur | |
;; lazy-seq gets turned to a function and recur target would be hidden. | |
;; Don't hold on to seq returned by some-lazy, only subsets | |
;; (def sl (take 5 (some-lazy))) ;;Holding on to subset of lazy list ok | |
;; (doall sl) | |
;; getting next | |
;; getting next | |
;; getting next | |
;; getting next | |
;; (1 2 3 4 5) | |
;; | |
;; user=> (doall sl) ;; Second time no re-eval | |
;; (1 2 3 4 5) |
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
;; Sorted Set | |
(def z-sorted-seq (sorted-set-by #(< (:z %1) (:z %2)))) | |
(conj z-sorted-seq {:z 3} {:z 2} {:z -12} {:z 44}) ;> #{{:z -12} {:z 2} {:z 3} {:z 44}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment