This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-08
Channels
- # beginners (36)
- # calva (13)
- # cider (3)
- # clerk (7)
- # clj-http (2)
- # clj-kondo (10)
- # clojure (20)
- # clojure-europe (14)
- # clojure-nl (1)
- # clojure-norway (26)
- # clojure-uk (5)
- # clojurescript (143)
- # dev-tooling (7)
- # events (1)
- # exercism (1)
- # honeysql (8)
- # hyperfiddle (4)
- # jobs-discuss (15)
- # leiningen (3)
- # lsp (31)
- # off-topic (17)
- # overtone (3)
- # pathom (11)
- # pedestal (11)
- # re-frame (15)
- # reitit (17)
- # releases (1)
- # scittle (2)
- # shadow-cljs (29)
- # squint (1)
- # yamlscript (3)
Hi! Is it possible to get cheshire.core/generate-string
to pretty-print json with 4 spaces instead of 2?
I’m using cheshire to rewrite some json files in a repo, and I don’t want to change existing formatting.
I’m getting two spaces like this:
user=> (println (json/generate-string {:x 1 :y 2} {:pretty true}))
{
"x" : 1,
"y" : 2
}
In case anyone else is interested, this is what I did:
(def -cheshire-pretty-printer
(let [opts (assoc json/default-pretty-print-options
:indentation " ")]
(json/create-pretty-printer opts)))
(defn json-generate-string [x]
(json/generate-string x {:pretty -cheshire-pretty-printer}))
Worked perfectly. Thanks again!That's true, but didn't solve the issue
I get Unhandled RuntimeException: Invalid token :
generally the fragment of clojure that is exchanged as a data serialization format is called "edn" and I don't believe there is a registered mime type for it, but I suspect application/edn is most used
Show: Project-Only All
Hide: Clojure Java REPL Tooling Duplicates (33 frames hidden)
1. Unhandled java.lang.RuntimeException
Invalid token: :
Util.java: 221 clojure.lang.Util/runtimeException
LispReader.java: 412 clojure.lang.LispReader/interpretToken
LispReader.java: 305 clojure.lang.LispReader/read
LispReader.java: 1398 clojure.lang.LispReader/readDelimitedList
LispReader.java: 1355 clojure.lang.LispReader$MapReader/invoke
LispReader.java: 285 clojure.lang.LispReader/read
LispReader.java: 1398 clojure.lang.LispReader/readDelimitedList
LispReader.java: 1347 clojure.lang.LispReader$VectorReader/invoke
LispReader.java: 285 clojure.lang.LispReader/read
LispReader.java: 216 clojure.lang.LispReader/read
LispReader.java: 205 clojure.lang.LispReader/read
RT.java: 1876 clojure.lang.RT/readString
RT.java: 1871 clojure.lang.RT/readString
core.clj: 3816 clojure.core/read-string
core.clj: 3806 clojure.core/read-string
client.clj: 478 clj-http.client/coerce-clojure-body/fn
client.clj: 477 clj-http.client/coerce-clojure-body
client.clj: 470 clj-http.client/coerce-clojure-body
client.clj: 545 clj-http.client/eval11811/fn
MultiFn.java: 234 clojure.lang.MultiFn/invoke
client.clj: 563 clj-http.client/output-coercion-response
client.clj: 560 clj-http.client/output-coercion-response
client.clj: 574 clj-http.client/wrap-output-coercion/fn
client.clj: 250 clj-http.client/wrap-exceptions/fn
client.clj: 728 clj-http.client/wrap-accept/fn
client.clj: 750 clj-http.client/wrap-accept-encoding/fn
client.clj: 711 clj-http.client/wrap-content-type/fn
client.clj: 959 clj-http.client/wrap-form-params/fn
client.clj: 993 clj-http.client/wrap-nested-params/fn
client.clj: 1017 clj-http.client/wrap-flatten-nested-params/fn
client.clj: 893 clj-http.client/wrap-method/fn
cookies.clj: 128 clj-http.cookies/wrap-cookies/fn
links.clj: 63 clj-http.links/wrap-links/fn
client.clj: 1046 clj-http.client/wrap-unknown-host/fn
client.clj: 1174 clj-http.client/request*
client.clj: 1167 clj-http.client/request*
client.clj: 1180 clj-http.client/get
client.clj: 1176 clj-http.client/get
RestFn.java: 423 clojure.lang.RestFn/invoke
message_store.clj: 49 server.message-store/get-conversations
message_store.clj: 47 server.message-store/get-conversations
REPL: 55 server.message-store/eval14934
REPL: 55 server.message-store/eval14934
Compiler.java: 7194 clojure.lang.Compiler/eval
Compiler.java: 7149 clojure.lang.Compiler/eval
core.clj: 3215 clojure.core/eval
core.clj: 3211 clojure.core/eval
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn/fn
AFn.java: 152 clojure.lang.AFn/applyToHelper
AFn.java: 144 clojure.lang.AFn/applyTo
core.clj: 667 clojure.core/apply
core.clj: 1990 clojure.core/with-bindings*
core.clj: 1990 clojure.core/with-bindings*
RestFn.java: 425 clojure.lang.RestFn/invoke
interruptible_eval.clj: 87 nrepl.middleware.interruptible-eval/evaluate/fn
main.clj: 437 clojure.main/repl/read-eval-print/fn
main.clj: 437 clojure.main/repl/read-eval-print
main.clj: 458 clojure.main/repl/fn
main.clj: 458 clojure.main/repl
main.clj: 368 clojure.main/repl
RestFn.java: 1523 clojure.lang.RestFn/invoke
interruptible_eval.clj: 84 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 56 nrepl.middleware.interruptible-eval/evaluate
interruptible_eval.clj: 152 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
AFn.java: 22 clojure.lang.AFn/run
session.clj: 218 nrepl.middleware.session/session-exec/main-loop/fn
session.clj: 217 nrepl.middleware.session/session-exec/main-loop
AFn.java: 22 clojure.lang.AFn/run
Thread.java: 1583 java.lang.Thread/run
Got it. I thought the coercion meant there was some client-side code that literally coerced the data using some defaults into Clojure if possible
How can I return early from an operation in Clojure? For example, this is a typical pattern from imperative style languages:
condition_is_met = False
for i in collection:
if my_function(i) == some_result:
condition_is_met = True
break
else:
continue
I am allowed to exit the loop when I get my result.
What would be the functional equivalent of this in Clojure? I have written a small function to find the greatest common denominator of length of two sequence here:
(defn get-gcd [x y]
(let [shorter (if (<= (count x) (count y)) x y)
longer (if (= (count x) (count shorter)) y x)]
(apply max (for [r (reverse (range 1 (+ 1 (count shorter))))]
(if (and (= (mod (count longer) r) 0) (= (mod (count shorter) r) 0)) r 0)))))
I get back a whole sequence and then I am applying the max function on the sequence to get a result. In Python, I would just loop and exit with break statement on finding my result. The get-gcd function above does not feel like idiomatic Clojure to me nor does it feel functional. Also, if my collection is really large, then the for comprehension above would feel wasteful since I am getting back an entire sequence. Any ways I can return early from operations in a functional manner?for returns a lazy sequence, so you can write (first (for [... :when foo?] ...))
to stop after the first :when foo?
other common patterns for 'early return' include not recurring and using the reduced
function (in the context of a reduce)
there are several methods to return early in function languages... most fundamentally using recursion
example of short-circuiting for
https://raw.githubusercontent.com/samcf/advent-of-code/main/2020-01-report-repair.clj
it doesn't matter if for is lazy, or whatever, max has to traverse the whole thing, and you would have to traverse the whole thing to find max in python as well
finding the max for a sequence of numbers is not a good example for early returns, because it cannot return early
in this case, since the sequence starts in descending order, rather than building a sequence of zeroes, it would be possible to just filter that sequence to the elements where the divisor divides both subjects, and then the first thing in that list will be the max, so then the use of max becomes unnecessary, and the operation can be lazy-ish
yes, usage of the max function will become un-necessary if for is lazy and I can get some guarantee of the order of the sequence that is being generated by the for.
thanks for the pointers @U02FVPF04A1 @U013JFLRFS8 @U0NCTKEV8. This is good information for me as a beginner to Clojure.
For the original use case, (some my-function collection)
will "short circuit". Recursive functions can stop when they like by not recurring. Beyond that, back in the good old days of Cobol I spent a couple of hours helping someone on an inexplicable bug. Finally in a daze I scrolled up a few pages. An early exit (above the fold so we did not see it) made the behavior explicable. :face_palm: