This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-08
Channels
- # announcements (13)
- # asami (9)
- # aws (1)
- # aws-lambda (1)
- # babashka (37)
- # beginners (70)
- # calva (19)
- # cider (23)
- # clara (10)
- # clj-kondo (15)
- # cljdoc (14)
- # clojure (3)
- # clojure-bay-area (1)
- # clojure-europe (40)
- # clojure-nl (3)
- # clojure-serbia (1)
- # clojure-uk (31)
- # clojuredesign-podcast (1)
- # clojurescript (9)
- # community-development (7)
- # data-oriented-programming (1)
- # datomic (44)
- # emacs (13)
- # figwheel-main (14)
- # fulcro (6)
- # jobs (6)
- # malli (15)
- # meander (7)
- # off-topic (74)
- # other-languages (1)
- # pathom (3)
- # portal (3)
- # re-frame (25)
- # reagent (6)
- # reitit (2)
- # reveal (1)
- # rewrite-clj (6)
- # ring (5)
- # shadow-cljs (11)
- # specter (7)
- # xtdb (7)
Just to double check, how do I best in a string of json like a response from a http api call. Do I use data.json
or is there something in built like read-string
>
The two main libraries are going to be data.json or cheshire. data.json is a lightweight from scratch clojure json library, cheshire is built on jackson which weighs in substantially heavier
I am pretty anti-dependency these days, the last personal project I did I hacked together just enough of a json encoder to avoid having any dependency for it
At work we just had a big push to switch to data.json from cheshire everywhere, but we still could get rid of our Jackson dep (some other java libraries bring it in)
https://git.sr.ht/~hiredman/lions/tree/master/item/src/com/manigfeald/json.clj is the hacked together encoder, not correct for all data, but generates correct json for the data I feed it
@zackteo Yup, what @hiredman said. Now that data.json
has had a big performance overhaul courtesy of @slipset I would say try to use that for all situations unless you have something that is so performance-critical you need to look at Jsonista (built around the problematic Jackson libraries).
data.json
2.1.0 just came out with UUID support, so the main difference between data.json
and Cheshire is the latter has built-in date support, but with the former and its :value-fn
option, you can manage dates pretty easily (IMO).
Cheshire has a lot of optional functionality, based on what you have on your classpath, but it's very popular and tends to get dragged in via several other popular community libraries, so you can end up with quite a mess of transitive dependencies... which I'm increasingly frustrated with these days.
Alright! Thanks š Yeah I saw your posts about getting managing dependencies and doing the best to get with of jackson . Performance definitely isn't too much a concern so data.json would work
Iām trying to use the #inst
reader macro to create an instant time for a Datomic database. But I want this time to be programmatic: for instance, here I am trying to make a new inst from the current time:
#inst (current-time)
But i suspect this doesnāt work because the #inst
sees a list, not a string. Is there a way to make this work?#inst
is for writing constant date+time values in Clojure, not run-time variable values: https://clojure.org/reference/reader#_built_in_tagged_literals
If you want to create a Clojure data structure with a run-time variable data+time value inside of it, just call whatever function or method you want that returns the desired value, e.g. (current-time)
Note: I do not know if current-time
in your example is a function in your code base -- I am not aware of any such function built into Clojure, but there are several Java APIs for getting the current data and/or time, e.g. those in the java.time package: https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
Yeaā¦ my current-time
function is just
(defn current-time []
(java.time.LocalDateTime/now))
But it seems that is very near to what #inst
does anyway. I think I can figure this out from here, thanks a lot!If you're on Java 11 or newer, you could just use http://java.net.http.WebSocket (https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/WebSocket.html).
Or if you want a wrapper, you could look into e.g. https://github.com/gnarroway/hato#websockets
Hi, I was watching Stu Halloway's talk Running With Scissors, and he mentioned the 'inverted REPL' he wrote for his personal financial software. It includes a snippet
(let [action (edn/read in)
result (perform-cat-action action conn ftx)],,,)
With the idea being he provides an action by writing edn to indicate action which is then processed.
I'm fiddling with something similiar, and I was wondering what the in
variable would be here. I was thinking stdin, but the way he described it, it's sounds like he's inputting via the REPL itself. Is that possible, and if so, how would in
be defined to read from the REPL?I wonder what is the difference between clojure.java.jdbc
and clojure.jdbc
?
clojure.jdbc is a defunct project IIRC, and it attempted to be a more clojure flavored alternative to clojure.java.jdbc
"The project is tested under JDK7 and JDK8."
I donāt recall seeing clojure.jdbc
before, but this post may be of interest: https://corfield.org/blog/2019/07/04/next-jdbc/
also checkout #sql for discussions of next.jdbc
I have 2 maps,
;author-id and nick
{:author-id "1", :nickname "nic"}
;author-id and post-content
{:author-id "1", :post-content "h4x"}
I want to merge :nickname into the author-id and post-content map. is there a simple way to do that on matching :author-id ?if all the data is consistent, you could group-by author-id and then apply merge to the maps that are grouped by that key
It oughta be consistent, yeah, I think that is reasonable to expect.
I think it makes more sense to have an indexed map, now that I try reasoning about it in the REPL...
{"1" {:author-id "1" :nickname "S"} "2" {:author-id "2" :nickname "R"}}
so maybe smashing them together on matching index is possible in a simple way?
@sova Itās not very clear to me what youāre trying to do precisely; Do you have a sequence of many maps that all have :author-id in them? Because if you only have just 2 maps that look exactly like those, you can just (merge m1 m2) š
(def posts (atom {"1" {:author-id "1" :post-content "SNnax"} "2" {:author-id "2" :post-content "Drolle"}}))
(def nicks (atom {"1" {:author-id "1" :nickname "Vace"} "2" {:author-id "2" :nickname "Luke"}}))
I'd like the result:
{"1" {:author-id "1" :post-content "SNnax" :nickname "Vace"} "2" {:author-id "2" :nickname "luke" :post-content "Drolle"}}
I thought this is what merge
was for but I'm not getting the expected result
That works š
(clojure.set/join
[{:author-id "1" :post-content "SNnax"}
{:author-id "2" :post-content "Drolle"}]
[{:author-id "1" :nickname "Vace"}
{:author-id "2" :nickname "Luke"}])
=>
#{{:author-id "1", :post-content "SNnax", :nickname "Vace"} {:author-id "2", :post-content "Drolle", :nickname "Luke"}}
Okay i'm not indexing by user-id though, I am indexing posts by post-id and users by user-id... so my apologies my needs are a little different. I think it's more like "find and replace" where I want to inject the nickname into the post-map
(def posts (atom {"eee9" {:author-id "1" :post-content "SNnax" :post-id "eee9"} "eeea" {:author-id "2" :post-content "Drolle" :post-id "eeea"}}))
(def nicks (atom {"1" {:author-id "1" :nickname "Vace"} "2" {:author-id "2" :nickname "Luke"}}))
(notice posts indexing changed, post-ids (their index) live inside each map as well)
Desired result: posts map with correct nickname injected
{"eee9" {:author-id "1" :post-content "SNnax" :nickname "Vace" :post-id "eee9"} "eeea" {:author-id "2" :post-content "Drolle" :nickname "Luke" :post-id "eeea"}}
Maybe I'll just start writing nicknames straight into the posts atom, and do find-and-replace if the nicknames ever change.
thanks for the help it's much appreciated
@sova a little late but you could do
(reduce-kv
(fn [m k v]
(assoc m k (merge v (get nicks (:author-id v)))))
{} posts)
To mitigate Clojureās slow startup, I want to have a running nREPL in the background and then periodically run babashka scripts. Is it possible for babashka to interact with the REPL? I know the nREPL exposes a port but how do I interact with it without the nrepl package on the babashka end?
@michaellan202 This is documented here: https://book.babashka.org/#_interacting_with_an_nrepl_server
This library isnāt documented under the āLibrariesā section, so I got confused. Thanks borkdude
@michaellan202 which library do you mean?
Oh, nevermind. I was mistakenly Cmd-Fāing for ānreplā when the library is called ābencode.ā Silly me
Understandable. Maybe a babashka.nrepl-client
library would be nice in time, but this is the low level way how to do it
Hi. I have a question about reify
, I'm not sure where to post it, so I'll try here. I don't understand why my code is returning java.lang.Object
and not java.lang.Double
? I am trying to reify a functional interface from http://generated.ojalgo.org/org/ojalgo/function/PrimitiveFunction.Unary.html.
(reify
org.ojalgo.function.PrimitiveFunction$Unary
(invoke [this ^Double arg]
(double arg)))
Returns
; eval (current-form): (reify org.ojalgo.function.PrimitiveFunction$Un...
; (err) Syntax error (IllegalArgumentException) compiling reify*
; (err) Mismatched return type: invoke, expected: java.lang.Double, had: java.lang.Object
Any help would be greatly appreciated :)you need
(invoke ^double [this ^double arg]
arg))
see https://clojure.org/reference/java_interop#primitives for more info!Works perfectly, thanks!
I have
(^double invoke [this ^double arg]
arg))
type hints can go before the name or the args list, before args list is preferred since before name doesnāt work for functions with multiple arities :thumbsup:
hi everyone, what is the recommendation for implementing money with currencies in Clojure, should it be done with Records to enforce some rigidity or just use maps with namespaced keys?
if youāre gonna have a ton of these things, I suggest records for performance, otherwise start with maps š
Java has some libs for this too like JodaMoney
@U064X3EF3 yeah, but it would force us to use interop and bring many types
Thereās this, it seems like it hasnāt been updated in a while, but Iāve used in the past (at a basic level); it worked fine https://github.com/clojurewerkz/money
Just a thought but a lot of financial institutions represent currency values using long values by removing the decimal place and using the smallest currency unit i.e. cents instead of dollars or pence instead of pounds.