Fork me on GitHub
#beginners
<
2022-01-06
>
seancorfield00:01:10

@mhamzachippa When you get that data back from the API and you convert it from JSON to Clojure, you can do this to it

dev=> (update {:subscribed-events [1 2 3]} :subscribed-events set)
{:subscribed-events #{1 3 2}}

🙌 1
seancorfield00:01:29

Alternatively, you could change your event code to

(re-frame/reg-event-fx
 :add-subscribed-event
 (fn-traced [{:keys [db]} [_ event_id]]
   {:db (update-in db [:user :subscribed-events] (fn [events id] (conj (set events) id)) event_id)}))

; re-frame event to remove subscribed events from the database
(re-frame/reg-event-fx
 :remove-subscribed-event
 (fn-traced [{:keys [db]} [_ event_id]]
   {:db (update-in db [:user :subscribed-events] (fn [events id] (disj (set events) id)) event_id)}))
That forces the subscribed events to be a set before trying to add/remove elements from it.

🙌 1
seancorfield00:01:23

(but fixing the data before you add it to the re-frame db map would be better!)

soxley02:01:11

I’m trying to get started with tools.build and cannot figure out what I’m doing wrong. I’m basically trying to follow the instructions here: https://clojure.org/guides/tools_build, but I can’t seem to get it to work. When I run my uberjar (built with clj -T:build uber) using java -jar obeya-0.1.1-standalone.jar, I get this error:

Error: Could not find or load main class obeya.main
Caused by: java.lang.ClassNotFoundException: obeya.main
This is my directory structure:
.
├── build.clj
├── deps.edn
├── src
│   └── obeya
│       └── main.clj
└── target

5 directories, 9 files
This is src/obeya/main.clj:
(ns obeya.main)

(defn -main [opts]
  (println "running"))
This is build.clj:
(ns build
  (:require [clojure.tools.build.api :as b]))

(def app 'obeya)
(def version (format "0.1.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def uber-file (format "target/%s-%s-standalone.jar" (name app) version))

(defn clean [_]
  (b/delete {:path "target"}))

(defn uber [_]
  (clean nil)
  (b/copy-dir {:src-dirs ["src" "resources"]
               :target-dir class-dir})
  (b/compile-clj {:basis basis
                  :src-dirs ["src"]
                  :class-dir class-dir})
  (b/uber {:class-dir class-dir
           :uber-file uber-file
           :basis basis
           :main 'obeya.main}))
This is my deps.edn:
{:deps {}

 :aliases
 {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.7.5" :git/sha "34727f7"}}
          :ns-default build}}}
I’ve tried a bunch of things, but I have yet to get anything to work (either the build fails or I get the above error.)

seancorfield02:01:32

Do you have (:gen-class) in the ns form in obeya/main.clj?

soxley02:01:55

No, I do not.

seancorfield02:01:29

Without that, you won't get a Java-callable main class.

soxley02:01:11

Indeed, that fixed my issue. I’m going to write something in #improve-getting-started about how I got stuck.

seancorfield02:01:48

If you create an app project via clj-new or deps-new, (:gen-class) should be present -- just like it is with lein new app myapp or similar.

soxley02:01:55

I didn’t know those things existed. I haven’t found mention of them in the documentation, otherwise I would have started with one of those.

seancorfield02:01:39

Community projects are rarely mentioned in the official docs.

Muhammad Hamza Chippa10:01:23

@seancorfield I changed it into maps now working like a charm. Thank you so much sir

1
tabidots14:01:20

Question about removing unneeded dependencies— I’m in the early stages of building an app/prototype and decided to use a different DB library. (No data committed yet; just changed my mind.) I already have the old one in deps.edn. Do I need to do anything else besides remove that line and add the new one? I assume because the app hasn’t been .jarred yet, the old library won’t do any more harm than just taking up a few extra MB on my local hard drive. Is that correct?

kj16:01:39

I'm currently working through this (excellent) https://www.learn-clojurescript.com, and in https://www.learn-clojurescript.com/section-1/lesson-8-capstone-weather-forecasting-app/#calling-an-external-api, the author uses the following code to update an app-state atom with new data returned from an API:

(swap! app-state update-in [:temperatures :today :value] (constantly today))
(swap! app-state update-in [:temperatures :tomorrow :value] (constantly tomorrow))
and I was wondering what the difference was between using update-in with (constantly <value>) and just using assoc-in with <value>. I.e can I just do this instead:
(swap! app-state assoc-in [:temperatures :today :value] today)
This assoc-in version seemed to work when I tested it, but is there some reason why using update-in is preferred?

ghadi16:01:51

in this case they are equivalent, and assoc-in makes more sense

ghadi16:01:08

update* is for transforming values through a function

kj16:01:57

ok great, thank you!

caumond16:01:52

Hi, there, I was using lein deps in the past, to know the conflicting artificats I have in my dependency tree. Now, I'm using deps.edn, but I don't know how to do the same? and don't find any reference on the web

❤️ 1
caumond16:01:06

oh yes, It's a so tiny paragraph that I missed it. But as usual a simple and elegant solution, thx !

gratitude-danke 1
grierson19:01:04

Hey, I'm trying to run clj in a docker container. So my image is FROM clojure CMD ["clj"] but when I run the image I get rlwrap: error: My terminal reports width=0 (is it emacs?)  I can't handle this, sorry!``

ghadi20:01:11

run clojure if you have no terminal, clj if you do (`docker -it` )

ghadi20:01:19

clj == clojure + rlwrap

👍 1
grierson20:01:55

Thanks, didn't know that.