beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
2021-04-08T00:15:44.242100Z

This, I suppose? https://www.itprotoday.com/compute-engines/why-poet

2021-04-08T00:17:53.242300Z

The article has a lot of nice things to say about it 🙂

seancorfield 2021-04-08T00:33:41.242500Z

“Object-oriented database management systems (also known as OODBMSs) are better suited than relational databases (such as SQL Server) for many of today’s new information types.” — but not for long 🙂

zackteo 2021-04-08T01:31:19.245Z

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 >

zackteo 2021-04-08T01:31:21.245200Z

?

2021-04-08T01:35:07.245300Z

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

2021-04-08T01:36:40.245400Z

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

2021-04-08T01:37:49.245500Z

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)

2021-04-08T01:40:52.245600Z

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

seancorfield 2021-04-08T01:43:39.247200Z

@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).

seancorfield 2021-04-08T01:45:00.248500Z

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).

seancorfield 2021-04-08T01:46:38.250100Z

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.

zackteo 2021-04-08T01:51:24.252300Z

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

Michael Lan 2021-04-08T03:08:29.253800Z

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?

2021-04-08T03:17:21.253900Z

#inst is for writing constant date+time values in Clojure, not run-time variable values: https://clojure.org/reference/reader#_built_in_tagged_literals

2021-04-08T03:18:13.254100Z

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)

2021-04-08T03:19:36.254300Z

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

Michael Lan 2021-04-08T03:20:22.254500Z

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!

pinealan 2021-04-08T08:54:21.255800Z

Recommendations for a lightweight library websocket client on JVM?

flowthing 2021-04-08T08:55:59.256Z

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).

flowthing 2021-04-08T08:57:06.256200Z

Or if you want a wrapper, you could look into e.g. https://github.com/gnarroway/hato#websockets

Joe 2021-04-08T11:14:09.261200Z

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?

Audrius 2021-04-08T12:22:21.262100Z

I wonder what is the difference between clojure.java.jdbc and clojure.jdbc ?

2021-04-08T12:36:42.262600Z

I don’t recall seeing clojure.jdbc before, but this post may be of interest: https://corfield.org/blog/2019/07/04/next-jdbc/

NoahTheDuke 2021-04-08T13:29:53.263200Z

also checkout #sql for discussions of next.jdbc

2021-04-08T14:58:55.264100Z

clojure.jdbc is a defunct project IIRC, and it attempted to be a more clojure flavored alternative to clojure.java.jdbc

jumar 2021-04-08T14:59:08.264300Z

Perhaps it was *in* meaning STDIN?

2021-04-08T14:59:50.264500Z

"The project is tested under JDK7 and JDK8."

ghadi 2021-04-08T15:01:44.264800Z

clojure.jdbc was an unfortunately named library

ghadi 2021-04-08T15:01:51.265Z

namespace squatting

sova-soars-the-sora 2021-04-08T16:35:09.267400Z

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 ?

dpsutton 2021-04-08T16:40:59.268Z

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

dpsutton 2021-04-08T16:41:17.268600Z

that is, if all the data is either disjoint or where it coincides it agrees

sova-soars-the-sora 2021-04-08T16:41:54.269100Z

It oughta be consistent, yeah, I think that is reasonable to expect.

sova-soars-the-sora 2021-04-08T16:42:32.270500Z

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"}}

sova-soars-the-sora 2021-04-08T16:42:49.271Z

so maybe smashing them together on matching index is possible in a simple way?

raspasov 2021-04-08T16:43:04.271200Z

@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) 🙂

sova-soars-the-sora 2021-04-08T16:46:57.272400Z

(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"}}

sova-soars-the-sora 2021-04-08T16:47:17.272800Z

I thought this is what merge was for but I'm not getting the expected result

dpsutton 2021-04-08T16:47:29.273100Z

i think you could (merge-with merge here

sova-soars-the-sora 2021-04-08T16:47:43.273300Z

😮

sova-soars-the-sora 2021-04-08T16:47:53.273500Z

That works 😄

raspasov 2021-04-08T16:53:12.274Z

(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"}}

sova-soars-the-sora 2021-04-08T16:53:46.274300Z

Nice.

sova-soars-the-sora 2021-04-08T16:54:36.275400Z

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

sova-soars-the-sora 2021-04-08T16:55:54.277Z

(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"}}

sova-soars-the-sora 2021-04-08T17:13:59.277800Z

Maybe I'll just start writing nicknames straight into the posts atom, and do find-and-replace if the nicknames ever change.

sova-soars-the-sora 2021-04-08T17:14:21.278400Z

thanks for the help it's much appreciated

sova-soars-the-sora 2021-04-08T17:14:23.278600Z

😃

ndonolli 2021-04-08T17:17:53.279100Z

@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)

1
🥲 1
ndonolli 2021-04-08T17:21:04.280Z

not exactly find-and-replace, but useful if you want to to merge posts into nicks

Michael Lan 2021-04-08T17:34:38.282400Z

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?

borkdude 2021-04-08T17:36:05.282800Z

@michaellan202 This is documented here: https://book.babashka.org/#_interacting_with_an_nrepl_server

👍 1
Michael Lan 2021-04-08T17:37:50.284Z

This library isn’t documented under the “Libraries” section, so I got confused. Thanks borkdude

borkdude 2021-04-08T17:38:29.284400Z

@michaellan202 which library do you mean?

Michael Lan 2021-04-08T17:39:21.285100Z

Oh, nevermind. I was mistakenly Cmd-F’ing for “nrepl” when the library is called “bencode.” Silly me

borkdude 2021-04-08T17:39:51.285700Z

Understandable. Maybe a babashka.nrepl-client library would be nice in time, but this is the low level way how to do it

byrongibby 2021-04-08T21:11:29.290100Z

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 :)

Vachi 2021-04-08T21:25:16.291800Z

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?

agile_geek 2021-04-09T08:45:53.313500Z

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.

👍 1
schmee 2021-04-08T21:36:30.291900Z

you need

(invoke ^double [this ^double arg]
    arg))
see https://clojure.org/reference/java_interop#primitives for more info!

schmee 2021-04-08T21:36:47.292100Z

how do you plan to represent the amounts?

byrongibby 2021-04-08T21:37:08.292300Z

Works perfectly, thanks!

Vachi 2021-04-08T21:39:18.292500Z

with bigdecimal

schmee 2021-04-08T21:39:36.292700Z

nice 👌

byrongibby 2021-04-08T21:39:59.292900Z

I have

(^double invoke [this ^double arg]
    arg))

schmee 2021-04-08T21:40:09.293100Z

if you’re gonna have a ton of these things, I suggest records for performance, otherwise start with maps 🙂

👍 1
schmee 2021-04-08T21:42:55.293400Z

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:

💡 1
alexmiller 2021-04-08T21:53:21.294200Z

Java has some libs for this too like JodaMoney

Vachi 2021-04-08T22:02:09.294400Z

@alexmiller yeah, but it would force us to use interop and bring many types

raspasov 2021-04-08T22:37:00.294800Z

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

raspasov 2021-04-08T22:38:40.295300Z

It does wrap joda-money AFAICT