Avi Drucker / May 04 2021
Remix of Clojure Koans 1 Equality Notebook by ADAvi Drucker
Sorted Map Stops Working After X Size [partially resolved]
For this project, I'm attempting to create a sorted map of maps where the keys are numbers.
{:deps {org.clojure/clojure {:mvn/version "1.10.1"} ;; complient is used for autocompletion ;; add your libs here (and restart the runtime to pick up changes) compliment/compliment {:mvn/version "0.3.9"}}}deps.edn
Extensible Data Notation
{:hello (clojure-version)}0.0s
Clojure
This first example works as expected - I put 8 items inside, then sort
;; start of example 1(def maps1 (atom {}))0.0s
Clojure
(swap! maps1 assoc 100 {:banana :tree} 400 {:frog :tree} 300 {:banana :frog} 200 {:banana :bunny} 105 {:bunny :tree} 410 {:banana :butterfly} 360 {:butterfly :tree} 225 {:banana :banana} )0.0s
Clojure
(into (sorted-map) maps1);; end of example 10.0s
Clojure
This 2nd example doesn't work as desired - I put 12 items inside, and then it seems I cannot sort as before
;; start of example 2(def maps2 (atom {}))0.0s
Clojure
(swap! maps2 assoc 100 {:banana :tree} 400 {:frog :tree} 300 {:banana :frog} 200 {:banana :bunny})0.0s
Clojure
(swap! maps2 assoc 105 {:bunny :tree} 410 {:banana :butterfly} 360 {:butterfly :tree} 225 {:banana :banana} )0.0s
Clojure
(swap! maps2 assoc 505 {:bunny :tree} 110 {:banana :butterfly} 660 {:butterfly :tree} 525 {:banana :banana} )0.0s
Clojure
(into (sorted-map) maps2);; end of example 20.0s
Clojure
Please let me know if you have any advice or know-how on how to resolve this, thank you!
Link back to Clojureverse request for help: https://clojureverse.org/t/has-demo-cant-seem-to-make-a-sorted-map-of-12-items-can-you-help/7632
...
I'm going to try next to create an initial map of size 12 with one swap! + assoc, and see if this changes anything...
;; start of example 3(def maps3 (atom {}))0.0s
Clojure
(swap! maps3 assoc 100 {:banana :tree} 400 {:frog :tree} 300 {:banana :frog} 200 {:banana :bunny} 105 {:bunny :tree} 410 {:banana :butterfly} 360 {:butterfly :tree} 225 {:banana :banana} 505 {:bunny :tree} 110 {:banana :butterfly} 660 {:butterfly :tree} 525 {:banana :banana})0.0s
Clojure
(into (sorted-map) maps3);; end of example 30.0s
Clojure
... This also appears to be not sorted.
Recap: Issue resolved... for now
I can't say exactly what is causing this behavior, but thanks to Andy, I now have a work-around to access the correctly sorted map in string form.
Clojureverse Help Thread: https://clojureverse.org/t/has-demo-cant-seem-to-make-a-sorted-map-of-12-items-can-you-help/7632/2?u=avidrucker
maps10.0s
Clojure
maps20.0s
Clojure
maps30.0s
Clojure
;; converting to a string representation shows that the map is indeed sorted correctly here(pr-str (into (sorted-map) maps2))0.0s
Clojure
;; converting to a string representation shows that the map is indeed sorted correctly here as well(pr-str (into (sorted-map) maps3))0.0s
Clojure
;; ... however, reading from a string back into (map? sorted-map? something else?) seems to revert back to the prior (default?) sorting order that is not numerically sorted(read-string (pr-str (into (sorted-map) maps3)))0.0s
Clojure