Fork me on GitHub
#beginners
<
2022-06-28
>
zakkor06:06:22

I'm trying to translate (or rethink) a snippet from JS to clojure. It's an event handler that is supposed to update some data when the callback is called. Any way to achieve this without mutation?

let tree = …;
el.addEventListener('click', () => { tree = updateTree(tree); });

delaguardo07:06:14

You can store the tree as an atom and reset it to the new state once the callback is fired. This is also kinda mutation but at least you will use mutable data structures designed for that instead of resetting a var

👍 1
zakkor08:06:57

Alrighty, thanks

zakkor08:06:14

I guess these sort of callbacks aren't really a functional concept

aratare10:06:23

Hi there. Is there, currently, any way to write to a file raw string form? I’m working with CSV right now and currently if I do (.write (io/writer "some-file") {"category":[{"id":1,"name":"Just some category","type":"Category"}]}) then it will just write that string as-is to the file, which is

{"category":[{"id":1,"name":"Just some category","type":"Category"}]}
whereas I’m looking for
"{\"category\":[{\"id\":1,\"name\":\"Just some category\",\"type\":\"Category\"}]}"
Thanks in advance 🙂

lsenjov10:06:19

Encode it a second time?

aratare10:06:10

@U24QP2D4J Encode how though? I’m lost here 😅

lsenjov10:06:42

So for edn, I've used (-> obj pr-str pr-str) to make it work

lsenjov10:06:46

Are you using clojure.csv?

aratare10:06:59

Yeah clojure.data.csv

aratare10:06:02

Let me try pr-str

aratare10:06:43

Bloody hell it works!

aratare10:06:47

Cheers 😄

simon lombard13:06:56

greetings. best place to start from absolute scratch?

lsenjov13:06:51

http://braveclojure.com is the (free) book that's generally recommended

Mno14:06:31

After those, maybe the lambdaisland stuff?

dorab15:06:19

Depending on your background, in addition to the resources already mentioned, try https://clojure.org/community/resources#_tutorials_and_learning_materials https://practical.li/

simon lombard15:06:44

Thank you! Will have a look

respatialized16:06:36

https://aphyr.com/posts/301-clojure-from-the-ground-up-welcome I like this series a lot. Detailed enough that I still get bits of insight out of it when I re-read it.

Michaël Salihi17:06:55

If you have front-end (React & co) background or target it, you can also bookmark: • https://www.learnreagent.com/https://www.learnreframe.com/

Michaël Salihi17:06:44

First part of learnreagent course is free.

practicalli-johnny07:06:33

There are 100+ hours of video covering basic Clojure development and solving small challenges on my website https://practical.li/

Mark Wardle17:06:43

I liked the Russ Olsen book "Getting Clojure"

👍 1
sheluchin19:06:54

How do I go about copying a large data structure from my program into an EDN file so the spacing is nice?

hiredman20:06:37

clojure.pprint is a built in pretty printer

sheluchin20:06:54

Is it just a matter of doing something like (spit "output.edn" (pprint ds)) to skip the manual copypasta altogether?

hiredman20:06:05

depends, I might instead rebind *out* to a printer writer that would write to the file

gratitude-thank-you 1
hiredman20:06:33

I can never remember if spit does the right thing with strings

hiredman20:06:47

There are third-party ones as well

Ronny Li23:06:54

Hi everyone, quick Java interop question: How do I represent something like

SubscriptionCreateParams.Item.builder()
in Clojure? If needed, these links can provide context: • https://stripe.com/docs/billing/subscription-resource?ui=API#create-a-subscription-through-the-apihttps://stripe.dev/stripe-java/com/stripe/param/SubscriptionCreateParams.html

Ronny Li23:06:19

I tried importing com.stripe.param.SubscriptionCreateParams.Item but no luck

Jacob O'Bryant23:06:03

Inner classes use a dollar sign, e.g. in this case IIRC you'd import com.stripe.param.SubscriptionCreateParams$Item and then do (SubscriptionCreateParams$Item/builder)

🙏 1
sheluchin23:06:31

I generated some uuids with (random-uuid) and saved them to my database. Now when I get the values from my DB and try to use them, some of them give me an error:

(identity #uuid "0eb537e1-72bc-4d1a-89e0-8e3c2a7f8978")   
; => Invalid number: 0eb537e1-72bc-4d1a-89e0-8e3c2a7f8978 
I don't understand how this could happen. Did I just make a sloppy mistake somewhere in storing the data or is there some other potential cause?

hiredman23:06:52

works fine in a vanilla clojure repl

% clj
Clojure 1.10.3
user=> #uuid "0eb537e1-72bc-4d1a-89e0-8e3c2a7f8978"
#uuid "0eb537e1-72bc-4d1a-89e0-8e3c2a7f8978"
user=>

hiredman23:06:40

the issue is whatever bit of tooling you are using is sitting between you and the repl trying to do stuff with the results from the repl, and the tooling doesn't understand the uuid literal

sheluchin23:06:28

Hmm, what the heck? First time encountering an issue like this working with UUIDs.

sheluchin23:06:03

java.lang.ExceptionInInitializerError
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at clojure.lang.Compiler$ObjExpr.eval(Compiler.java:5008)
	at clojure.lang.Compiler.eval(Compiler.java:7193)
	at clojure.lang.Compiler.eval(Compiler.java:7149)
	at clojure.core$eval.invokeStatic(core.clj:3215)
	at clojure.core$eval.invoke(core.clj:3211)
	at nrepl.middleware.interruptible_eval$evaluate$fn__67379$fn__67380.invoke(interruptible_eval.clj:87)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invokeStatic(core.clj:667)
	at clojure.core$apply.invoke(core.clj:662)
	at clojure.core$with_bindings_STAR_.invokeStatic(core.clj:1999)
	at clojure.core$with_bindings_STAR_.doInvoke(core.clj:1990)
	at clojure.lang.RestFn.invoke(RestFn.java:425)
	at nrepl.middleware.interruptible_eval$evaluate$fn__67379.invoke(interruptible_eval.clj:87)
	at clojure.main$repl$read_eval_print__9206$fn__9209.invoke(main.clj:437)
	at clojure.main$repl$read_eval_print__9206.invoke(main.clj:437)
	at clojure.main$repl$fn__9215.invoke(main.clj:458)
	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:84)
	at nrepl.middleware.interruptible_eval$evaluate.invoke(interruptible_eval.clj:56)
	at nrepl.middleware.interruptible_eval$interruptible_eval$fn__67412$fn__67416.invoke(interruptible_eval.clj:152)
	at clojure.lang.AFn.run(AFn.java:22)
	at nrepl.middleware.session$session_exec$main_loop__67672$fn__67676.invoke(session.clj:218)
	at nrepl.middleware.session$session_exec$main_loop__67672.invoke(session.clj:217)
	at clojure.lang.AFn.run(AFn.java:22)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.NumberFormatException: Invalid number: 0eb537e1-72bc-4d1a-89e0-8e3c2a7f8978
	at clojure.lang.LispReader.readNumber(LispReader.java:352)
	at clojure.lang.LispReader.read(LispReader.java:278)
	at clojure.lang.LispReader.read(LispReader.java:216)
	at clojure.lang.LispReader.read(LispReader.java:205)
	at clojure.lang.RT.readString(RT.java:1876)
	at clojure.lang.RT.readString(RT.java:1871)
	at com.example.playgrounds.pathom$eval123662.<clinit>(NO_SOURCE_FILE:122)
	... 32 more
I'm trying to understand what bit of tooling could be at fault here..

hiredman00:06:19

he code being called is annoyingly going to be in the "... 32 more"

hiredman00:06:40

but I would start by looking for any nrepl middleware / plugins / whatever and disabling them

hiredman00:06:58

but it may else be your editor