Context: I'm connected to a regular nREPL in Cursive, then in a cljs REPL (figwheel.main.api/start "dev") through piggieback nREPL middleware. Then I'm doing the following to get a sorted map:
(clojure.walk/postwalk
(fn [x]
(if (map? x)
(into (sorted-map) x)
x))
{:x 999
:a 444
:o 333
:p 222
:d 999
:y 333
:q 999
:b 999
:c 199
:z 555})
=> {:y 333, :q 999, :o 333, :z 555, :c 199, :b 999, :d 999, :x 999, :p 222, :a 444}
As you can see the sorted map does not appear sorted. Is this Cursive? Is it piggieback? Is it piggieback using bencode via nREPL send? Do anybody know how to solve this?
I'd be happy to install a global pretty print hook of sorts that always sorts maps, regardless if they are initially sorted or not.
Edit: in a regular remote nREPL session this works just fine, so I suppose that excludes nREPL/bencodet. And that narrows it down to piggieback..?That does seem very suspicious. Can you try it via piggieback without Cursive in the loop, perhaps via lein REPL? I'm a bit out of touch with piggieback, I'm not sure how people used to run it...
I added the following in user.clj:
(ns user
(:require [clojure.walk :as walk]
[figwheel.main.api :as fig]))
(defn to-sorted-map [v]
(walk/postwalk
(fn [x]
(if (map? x)
(into (sorted-map) x)
x))
v))
(do
(require 'nrepl.middleware.print)
(let [default-fn @#'clojure.core/pr-on]
(alter-var-root #'nrepl.middleware.print/*print-fn*
(constantly
(fn [x printer]
(default-fn (try
(to-sorted-map x)
(catch Exception _
x))
printer))))))
... and it just worked! I suppose pr-on just uses the default and sorted iteration order. This removes the need for calling to-sorted-map yourselfleining (`lein repl :connect localhost:5600`) also does not do it correctly without the middleware.print hack
I'll stick to my user.clj hack
the user.clj hack also works for lein repl :connect
Oh great, glad it's working!