Fork me on GitHub
#beginners
<
2022-01-05
>
mister_m04:01:40

Hi sort of a pedestrian question RE leiningen and the environ lib: I am trying to provide environment specific variables through profiles in my lein project. I have a profiles.clj file next to the project.clj file with something like the following:

{:dev {:env {:test "ing"}}}
I read that the dev profile in leiningen is active by default, so my assumption is that this :test key should be available after lein repl ; however I do not see this key in my environ env map (`(require '[environ.core :refer [env]])`) after https://github.com/weavejester/environ#usage. I did not do any composite profiles as are shown further in that example, in fact my project.clj has no profiles defined. Am I missing an additional step in my project.clj that prevents me from seeing (env :test) return non-nil?

andy.fingerhut04:01:59

I do not know the answer, but wanted to mention that there is also a #leiningen channel that may be a more target-rich environment for Leiningen experts than the #beginners channel.

seancorfield05:01:38

@U01188CHUFL Did you add this to your project.clj file?

:plugins [[lein-environ "1.2.0"]]

mister_m05:01:38

oh man I added it as a dependency not a plugin, let me try that

mister_m05:01:34

That was it, thanks Sean

1
seancorfield05:01:14

@U01188CHUFL I don't use Leiningen or environ so it was just a lucky guess based on the environ readme 🙂

1
pinkfrog16:01:22

Hi. I am seeing an empty list of classpath:

clj꞉app.main꞉> 
(require '[clojure.java.classpath :as cp])
(cp/classpath)

(#object[java.io.File 0x61c21b0a "/Users/matthew/.asdf/installs/java/openjdk-17/lib/src.zip"])

pinkfrog16:01:38

I am on openjdk17, arm (mac m1)

ghadi16:01:48

(System/getProperty "java.class.path")

ghadi16:01:52

what is the output for this ^?

pinkfrog16:01:50

Lots of jars as specified in the deps.edn

pinkfrog16:01:09

This is actually an X,Y problem. I am using clojure.tools.namespace.repl/refresh to auto reload namespaces.

pinkfrog16:01:37

However, I found I have to explicitly set set-refresh-dirs

pinkfrog16:01:09

Though the docs says,

The directories to be scanned are controlled by 'set-refresh-dirs';
  defaults to all directories on the Java classpath.

pinkfrog16:01:48

I’d expect the refresh-dirs have been automatically populated.

pinkfrog16:01:35

I switch to jdk8 zuul and everything works fine.

pinkfrog16:01:44

It seems there are some issues with jdk17 (openjdk).

ghadi16:01:43

when you say "openjdk, ARM" that could be a bunch of different things

ghadi16:01:51

can you get the property "java.vendor" or "java.vm.vendor"

ghadi16:01:24

or "java.vendor.version"

pinkfrog00:01:04

Two java versions used: 1. (Work): Zuul Java 8, compiled as arm package 2. (Does not work): OpenJdk java 17, compiled as x64 pacakge

pinkfrog00:01:33

But the docs officially claims only 8 and 11 are supported, I shall not go beyond that boundary: https://clojure.org/guides/getting_started#_dependencies

ghadi00:01:23

OpenJDK Java 17 is not specific enough

ghadi00:01:47

using that JDK, please provide the output of the properties I mentioned

PB18:01:03

I have seen that some people have cljr-magic-require-namespaces as shown here: https://github.com/clojure-emacs/clj-refactor.el/blob/9e1f92033449a4abc6218ce31670d89e3e6a4dc5/clj-refactor.el#L67-L91 I have cljr installed but I don't see this function. Does anyone have any idea?

PB18:01:55

I want to be able to add namespaces for experimenting without having to add them to my requires

PB18:01:02

I know, but I'd like to be able to just type pp/pprint

PB18:01:07

I understand. But I'd like to be able to use clj-magic-require-namespaces

PB18:01:42

I thinkj that's a terrible attitude to have. "oh it doesn't work, no use in trying any further, you'll just have to accept that it doesn't work

varun18:01:48

I've been doing the clojure koans and i'm on the lazy sequences part now and having a bit of trouble trying to understand why this is true:

"Iteration provides an infinite lazy sequence"
  (= '(1 2 4 8 16 32 64 128) (take 8 (iterate (fn [x] (* x 2)) 1)))
Looking at the https://clojuredocs.org/clojure.core/iterate`iterate`, does the return value from each iterate get used as x in each subsequent iteration?

varun18:01:52

nevermind i guess my question was kind of dumb. I just read the description of the function for the nteenth time and realize that's exactly what this means: Returns a lazy sequence of x, (f x), (f (f x))

lightsaber 1
duckie 1
Muhammad Hamza Chippa19:01:12

{:subscribed-events [{:event_id 4525869}] I have this type of data structure how can I update another map {:event_id 15} in the data structure using update-in

Cora (she/her)19:01:53

(update-in {:subscribed-events [{:event_id 4525869}]} [:subscribed-events 0 :event_id] inc)
;; => {:subscribed-events [{:event_id 4525870}]}

Cora (she/her)19:01:15

you can look at the examples people have graciously provided on https://clojuredocs.org/clojure.core/update-in for more examples, too

Muhammad Hamza Chippa19:01:59

@corasaurus-hex I look at that I wanted to add another map something like this {:subscribed-events [{:event_id 4525869}{:event_id 14}]} I was trying something like this but it is not working

(update-in {:subscribed-events [{:event_id 4525869}]} [:subscribed-events] (conj {:event_id 15}))

seancorfield19:01:28

(update-in {:subscribed-events [{:event_id 4525869}]} [:subscribed-events] conj {:event_id 15})
Remove those parens.

🙌 1
seancorfield19:01:03

update-in expects a function followed by arguments. You had (conj {:event_id 15}) instead which isn't a function.

Muhammad Hamza Chippa19:01:33

Thanks @seancorfield that solved the problem

seancorfield19:01:08

Well, I mean it sort of behaves as a function but isn't what you want:

dev=> (conj {:event_id 15})
{:event_id 15}
so then that hash map is treated as a function and it is "called" on [{:event_id 452869}] so it's going to return nil:
dev=> ({:event_id 15} [{:event_id 452869}])
nil

🙌 1
Muhammad Hamza Chippa19:01:47

Got it Sir, thanks for Guiding

Muhammad Hamza Chippa20:01:13

@seancorfield to remove the map from the data structure dissoc is not working for it ?

(update-in {:subscribed-events [{:event_id 4525869}{:event_id 15]} [:subscribed-events] conj {:event_id 15})

seancorfield20:01:53

@mhamzachippa Can you explain what code you tried, what result you got, and what result you are expecting/trying to get?

Muhammad Hamza Chippa20:01:28

I have map like this {:subscribed-events [{:event_id 4525869} {:event_id 15}]} and I want to remove {:event_id 15} from it for this I am using this code

(update-in {:subscribed-events [{:event_id 4525869}{:event_id 15}]} [:subscribed-events] dissoc {:event_id 15})

seancorfield20:01:26

The value of :subscribed-events is a vector. dissoc only works on maps.

seancorfield20:01:25

If you find yourself wanting to add and remove arbitrary items from a vector, perhaps you want a different data structure?

seancorfield20:01:12

Given what you've shown so far, it seems like the important aspect of :subscribed-events is just the event ID itself -- if you used a set of IDs for :subscribed-events, this gets easier:

dev=> (update {:subscribed-events #{}} :subscribed-events conj 4525869)
{:subscribed-events #{4525869}}
dev=> (update *1 :subscribed-events conj 15)
{:subscribed-events #{15 4525869}}
dev=> (update *1 :subscribed-events conj 1234)
{:subscribed-events #{1234 15 4525869}}
dev=> (update *1 :subscribed-events disj 15)
{:subscribed-events #{1234 4525869}}
dev=> 

✅ 1
seancorfield20:01:41

Do you need additional data in :subscribed-events beyond just the IDs?

Muhammad Hamza Chippa20:01:29

your suggestion will work for me this sounds too good

Muhammad Hamza Chippa20:01:31

btw which data structure #{} is ?

seancorfield20:01:17

https://clojure.org/reference/data_structures has a lot of good information on this topic.

solf21:01:18

Another one, very useful for people new to clojure: https://clojure.org/guides/weird_characters

Muhammad Hamza Chippa21:01:47

@seancorfield I am basically using your approach to manipulate database of re-frame I have these events in the database, adding the event is working fine whereas deleting is not working can you detect the error in that

; re-frame event to add subscribed events in the database
(re-frame/reg-event-fx
 :add-subscribed-event
 (fn-traced [{:keys [db]} [_ event_id]]
   {:db (update-in db [:user :subscribed-events] conj 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] disj event_id)}      
  )
)

seancorfield21:01:52

Did you make sure to initialize the :subscribed-events value in the db to #{}?

seancorfield21:01:16

Otherwise it'll be nil and conj will turn that into a list and disj will not work.

seancorfield21:01:49

you could use (fnil conj #{}) instead of just conj and (fnil disj #{}) instead of just disj to have that happen automatically

seancorfield21:01:45

fnil wraps a function to provide a default value to use in place of a nil argument.

Muhammad Hamza Chippa21:01:00

I can add event-id successfully but deleting it is not working at all

seancorfield21:01:39

So your event handlers would look like:

; re-frame event to add subscribed events in the database
(re-frame/reg-event-fx
 :add-subscribed-event
 (fn-traced [{:keys [db]} [_ event_id]]
   {:db (update-in db [:user :subscribed-events] (fnil conj #{}) 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] (fnil disj #{}) event_id)}))

seancorfield21:01:14

As I said, if you don't start with a set, you'll get a sequence from conj and then you cannot use disj on it.

seancorfield21:01:59

dev=> (conj nil 13)
(13)
dev=> (conj *1 42)
(42 13)
dev=> (disj *1 13)
Execution error (ClassCastException) at dev/eval100264 (dev.clj:1).
class clojure.lang.PersistentList cannot be cast to class clojure.lang.IPersistentSet (clojure.lang.PersistentList and clojure.lang.IPersistentSet are in unnamed module of loader 'app')
dev=> 

seancorfield21:01:42

(and it would be the same with [] instead of nil -- you need #{} to start with or use the fnil versions above)

Muhammad Hamza Chippa21:01:21

Got it basically I did not initialize :subscribe-events with map so it is a list and disj don't work with the list ?

Muhammad Hamza Chippa21:01:06

(fnil disj #{}) is not working as well

seancorfield22:01:35

Show your code -- because it works for me in the REPL:

dev=> (update nil :subscribed-events (fnil conj #{}) 13)
{:subscribed-events #{13}}
dev=> (update *1 :subscribed-events (fnil conj #{}) 42)
{:subscribed-events #{13 42}}
dev=> (update *1 :subscribed-events (fnil conj #{}) 100)
{:subscribed-events #{13 100 42}}
dev=> (update *1 :subscribed-events (fnil disj #{}) 42)
{:subscribed-events #{13 100}}
dev=> (update *1 :subscribed-events (fnil disj #{}) 13)
{:subscribed-events #{100}}
dev=> 

Muhammad Hamza Chippa22:01:14

there is one problem I am getting subscribed-events it from the api, and it is vector probably, so can you recommend me how can I do this with vector or map because I guess I am not getting map from the api although I converted vector into the set using (into #{} subscribed-events)

Muhammad Hamza Chippa22:01:03

basically I am getting this type of response from the api

Muhammad Hamza Chippa22:01:31

I am using this code :add-subscribe-event is working fine where as :remove-subscribed-event is not working

(re-frame/reg-event-fx
 :add-subscribed-event
 (fn-traced [{:keys [db]} [_ event_id]]
   {:db (update-in db [:user :subscribed-events] (fnil conj #{}) 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] (fnil disj #{}) event_id)}))