This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-07
Channels
- # announcements (12)
- # autochrome-github (4)
- # babashka (27)
- # babashka-sci-dev (2)
- # beginners (80)
- # calva (34)
- # cider (8)
- # clj-kondo (77)
- # clojars (34)
- # clojure (50)
- # clojure-europe (35)
- # clojure-nl (4)
- # clojure-uk (5)
- # clojured (2)
- # clojurescript (26)
- # core-async (4)
- # cursive (4)
- # datahike (4)
- # datomic (40)
- # emacs (7)
- # etaoin (1)
- # fulcro (12)
- # google-cloud (143)
- # hyperfiddle (1)
- # integrant (2)
- # jobs (1)
- # malli (15)
- # membrane (17)
- # off-topic (38)
- # pathom (1)
- # reagent (4)
- # releases (2)
- # remote-jobs (2)
- # sci (1)
- # shadow-cljs (10)
- # spacemacs (7)
- # tools-deps (5)
- # vim (6)
- # xtdb (37)
Hi everyone. I have a map with namespaced keys (generated by next.jdbc), how can I remove the namespace from all keys?
For example, I have a map like
{:myapp/a 1 :myapp/b 2}
and I want to get
{:a 1 :b 2}
(-> {:myapp/a 1 :myapp/b 2}
(update-keys #(keyword (name %))))
update-keys
is from Clojure 1.11
@U01RL1YV4P7 Okay, thanks.👍
Please, just try to get used to namespaced keys -- they are idiomatic in Clojure.
You do not want to "remove the namespace".
@U031NJ4ERUP what should happen to {:foo/a 1, :bar/a 2, :a 3}
?
@U04V70XH6 Thanks, I got it. But I want to return the database query results to the front end (javascript) as json strings, is it ok the keep the namespaced keys?
@UEQPKG7HQ In my situation, The query results are from a single database table and I can make sure that the keys are in the same namespace.
@U031NJ4ERUP The various JSON-producing libraries in Clojure either strip the ns by default or have an option to do so:
dev=> (require '[clojure.data.json :as json])
nil
dev=> (json/write-str {:myapp/a 1 :myapp/b 2})
"{\"a\":1,\"b\":2}"
@U04V70XH6 Thanks Sean, the middleware I used to produce JSON keeps the ns by default, I need to explore the docs.🙂
That's probably based on Cheshire. Depending on the middleware, you should be able to provide some JSON options, that get passed to Cheshire, to tell it to drop namespaces.
Or are you using the Metosin stuff? If so, that'll be jsonista I expect. No idea about that one.
Yes, I’m using the Metosin/reitit, I’ll explore the docs for reitit and jsonista. Thanks again for your help.👍
Hi everyone,
Can I conditionally apply macros? For example, I have 2 macros: macro-1
and macro-2
, and I want to do something like this:
((if condition macro-1 macro-2) args)
Hm, just tried it and apparently you can't use apply
with macros: Can't take value of a macro: #'user/macro-1
I have a map function like below
(map (fn [data]// some logic) input-data)
How I do two assoc function like below in clojure, (since clojure is immutable) Note that I cannot use one (`assoc data :a "A" : b "B")`@U01RL1YV4P7 Thanks for your response , Can I use merge ?
A. Assoc takes a variadic number of arguments, you can just "assoc all" B. Merge is slow
To make it more clear, this
(-> data
(assoc :a "A")
(assoc :b "B"))
is equivalent to
(assoc
(assoc data :a "A") ;; this returns a new map with A assoc'd
:b "B"))
If I have a collection of maps and want :x
to appear in each map, using a default value if it doesn't already exist in the map, is there any function that helps with that? I can reduce + check + assoc to get the desired result, but I wonder if there's anything easier.
I’d map update
.
@U1Z392WMQ I like that one
what’s your favorite way of going from a unix timestamp string "1646570536"
to a human readable date 2022-03-06
?
I'd convert to a long, multiply by 1000, and feed it into the Timestamp constructor
https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html#Timestamp-long-
and then just do normal formatting for dates
but maybe it's not the best route ¯\(ツ)/¯
the proper way would be to make like a LocalDate and then use java.time.format'ers
you'll need to pick a zone/offset to interpret that point in time in of course (but that's how human time works)
Instant really needs to be converted to a date/time in the context of a zone/offset before it can be interpreted
oh absolutely. and it has atZone and atOffset to make that easier
thanks!
schweeet thank you! I need to explore those docs @corasaurus-hex, my java is still pretty weak 😅
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html
the time stuff is very rich
Hi guys, a pretty basic question please. After downloading and installing the clj CLI (Windows), I'm trying to spin up a Reveal REPL in order to beautify the outputs especially maps and Datomic records. Since I'm new to Clojure and google doesn't exactly help, I wanted to show you guys what I did: Executing the following command within the Clojure root folder (as per the github repo - https://vlaaad.github.io/reveal/):
clj -Sdeps '{:deps {vlaaad/reveal {:mvn/version "1.3.272"}}}' -X vlaaad.reveal/repl
Which should start a Reveal REPL. But instead I get the following error:
Error building classpath. Don't know how to create ISeq from: clojure.lang.Symbol
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
// REST OF STACK TRACE
What am I missing here? And thanks in advance!can you share some of that stack trace? if you hit the + icon in the message editor and "create a text snippet" that's the best way to add a big chunk of text
one thing you might be hitting is that quoting on Windows is extra awful on the command line (see https://clojure.org/reference/deps_and_cli#quoting for details)
you might actually want clj -Sdeps '{:deps {vlaaad/reveal {:mvn/version """1.3.272"""}}}'
instead
@U064X3EF3 This is the stack and the last command you sent results in the same stack. Do you think it's another Windows weird behavior? I'm used to work on Ubuntu but this time Windows is the only option. Once again thanks for ur help!
Error building classpath. Don't know how to create ISeq from: clojure.lang.Symbol
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol
at clojure.lang.RT.seqFrom(RT.java:557)
at clojure.lang.RT.seq(RT.java:537)
at clojure.core$seq__5402.invokeStatic(core.clj:137)
at clojure.core$merge_with$merge2__5964.invoke(core.clj:3066)
at clojure.lang.ArrayChunk.reduce(ArrayChunk.java:58)
at clojure.core$reduce1.invokeStatic(core.clj:942)
at clojure.core$reduce1.invokeStatic(core.clj:934)
at clojure.core$merge_with.invokeStatic(core.clj:3059)
at clojure.core$merge_with.doInvoke(core.clj:3051)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invokeStatic(core.clj:667)
at clojure.core$apply.invoke(core.clj:660)
at clojure.tools.deps.alpha.reader$merge_deps.invokeStatic(reader.clj:87)
at clojure.tools.deps.alpha.reader$merge_deps.invoke(reader.clj:84)
at clojure.tools.deps.alpha.script.make_classpath2$run_core.invokeStatic(make_classpath2.clj:91)
at clojure.tools.deps.alpha.script.make_classpath2$run_core.invoke(make_classpath2.clj:74)
at clojure.tools.deps.alpha.script.make_classpath2$run.invokeStatic(make_classpath2.clj:117)
at clojure.tools.deps.alpha.script.make_classpath2$run.invoke(make_classpath2.clj:111)
at clojure.tools.deps.alpha.script.make_classpath2$_main.invokeStatic(make_classpath2.clj:162)
at clojure.tools.deps.alpha.script.make_classpath2$_main.doInvoke(make_classpath2.clj:134)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:705)
at clojure.core$apply.invokeStatic(core.clj:665)
at clojure.main$main_opt.invokeStatic(main.clj:514)
at clojure.main$main_opt.invoke(main.clj:510)
at clojure.main$main.invokeStatic(main.clj:664)
at clojure.main$main.doInvoke(main.clj:616)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:705)
at clojure.main.main(main.java:40)
2022/03/07 18:16:03 open C:\Users\badeer/.clojure/.cpcache/5688acfd.cp: The system cannot find the file specified.
seems like you are using a very old version of the Clojure CLI based on that stack - can you install the latest version?
latest version is 1.10.3.1087
@U064X3EF3 Oh i'm using:
clj --version
Clojure 1.10.1
I'll try to use 1.10.3 but 1.10.1 isn't that old is it?Couple years old
The stack has namespaces that haven't existed in a couple years, I'd much rather debug the current version
Oh wow! That makes sense. Actually @U064X3EF3 I tried it on Ubuntu and it works like a charm there. I'll stick with Ubuntu as it's far more natural to me than windows. Thanks A LOT for your help!
Also switched to the newest version
cool, enjoy! if you have questions on the CLI in the future, best place to put those is in #tools-deps
Ohh nice! So this channel is strictly for clojure beginner matters. Thanks a lot Alex!
all questions welcome here, but some people only attend to specific channels so sometimes the topics are better
REPL get stuck
Hey guys, when I start a REPL and type something like (range 10)
it prints the result. But if for some reason I type something like (range)
it starts only to print the commands but not evaluating them. I'm just able to restore the REPL restarting. Why this happens and is restart the only solution?
I thought that as a lazy sequence it would not be evaluated if nothing was calling it for the next value.
if you did (def foo (range))
it would not try to print out (range), but only the var that was just defined
that makes sense
I'm assuming you're using CIDER or some other nREPL based thing, because if you use the plain clj
REPL, you'll see it print out incrementally
I'm using IntelliJ with Cursive plugin.
After a while the socket disconnect with an error 😅
Exception in thread "nRepl-session-814d6ee7-c483-4e3a-b6c2-3943cbe2a7b6" java.net.SocketException: Socket closed
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at nrepl.transport$bencode$fn__450.invoke(transport.clj:116)
at nrepl.transport.FnTransport.send(transport.clj:41)
at nrepl.middleware.print$send_nonstreamed.invokeStatic(print.clj:159)
at nrepl.middleware.print$send_nonstreamed.invoke(print.clj:138)
at nrepl.middleware.print$printing_transport$reify__843.send(print.clj:174)
at nrepl.middleware.caught$caught_transport$reify__878.send(caught.clj:58)
at nrepl.middleware.interruptible_eval$evaluate$fn__934.invoke(interruptible_eval.clj:123)
at clojure.main$repl$fn__9119.invoke(main.clj:460)
at clojure.main$repl.invokeStatic(main.clj:458)
at clojure.main$repl.doInvoke(main.clj:368)
at clojure.lang.RestFn.invoke(RestFn.java:1523)
at nrepl.middleware.interruptible_eval$evaluate.invokeStatic(interruptible_eval.clj:79)
at nrepl.middleware.interruptible_eval$evaluate.invoke(interruptible_eval.clj:55)
at nrepl.middleware.interruptible_eval$interruptible_eval$fn__941$fn__945.invoke(interruptible_eval.clj:142)
at clojure.lang.AFn.run(AFn.java:22)
at nrepl.middleware.session$session_exec$main_loop__1043$fn__1047.invoke(session.clj:171)
at nrepl.middleware.session$session_exec$main_loop__1043.invoke(session.clj:170)
at clojure.lang.AFn.run(AFn.java:22)
at java.base/java.lang.Thread.run(Thread.java:829)
(range 10)
Error writing to client: Broken pipe (Write failed)
cider has cider-print-quota
which lets you limit the size of values it will try to print. if you're not using cider, but you're still using nrepl, you can use something like https://github.com/phronmophobic/nrepl-set-print-quota-middleware
Hello I am befuddled by the use of Coercion in Reitit. For example, the JSON corecion on https://github.com/metosin/reitit/blob/master/test/clj/reitit/http_coercion_test.clj
(testing "json coercion"
(let [e2e #(-> (request "application/json" (ByteArrayInputStream. (j/write-value-as-bytes %)))
(app) :body (slurp) (j/read-value (j/object-mapper {:decode-key-fn true})))]
(is (= data-json (e2e data-edn)))
(is (= data-json (e2e data-json)))))
What is the purpose of encoding it as a ByteArrayInputStream? And is this local on the server only?
I'm really confused as to what benefits this provides and could use some clarityit looks like the ring handler expects body to be a stream. so if you're not sending an actual http request that's how you can simulate an http request
Interesting