Fork me on GitHub
#clojurescript
<
2022-07-12
>
Abhinav07:07:13

Is there any guide to compiling a cljc library for clj and cljs consumption? I don’t have any npm deps, and I couldn’t find a straightforward guide. any leads would be appreciated

thheller08:07:24

they don't need to be compiled at all. just publish like any clj library, ie. just the files

1
Abhinav08:07:00

I did that, but shadow-cljs was throwing an error when I tried to import it.

The required namespace "my-lib.core" is not available, it was required by "my-current-ns.cljs".
I packaged it using`lein uberjar` since it wasn’t working, I was wondering if I did something wrong. I can import it in a clojure namespace though.

thheller08:07:51

uberjar is definitely incorrect

thheller08:07:58

how are you consuming things?

thheller08:07:26

I mean did you actually publish the library to clojars or something?

thheller08:07:00

or where are you putting the files?

Abhinav08:07:29

I did publish it to clojars. I figured out the issue, I forgot to add it to the shadow-cljs.edn file. it worked after adding it there. my bad

👍 1
popeye09:07:43

I am trying to do assoc-in in reframe db and I am getting , What does it mean

router.cljc?rel=1657557358379:204 Uncaught Error: No protocol method IAssociative.-assoc defined for type cljs.core/List:

thheller09:07:51

that you are trying to assoc something into a list. so maybe you don't have the actual db? or maybe missed to destructure something?

popeye09:07:50

Thanks for your response @U05224H0W, I am trying something like this https://clojurians.slack.com/archives/C053AK3F9/p1657564702187899 but it is list instead of vector

thheller09:07:00

you got the answer in that thread?

popeye09:07:34

yeah, I tried the same thing in my code now,, but it is list instead of vector

thheller09:07:44

ah, so an actual list

thheller09:07:54

ok then the answer becomes a bit trickier. do you actually need a list? I mean the easiest would be a vector

popeye09:07:26

Agree it is better, But is there way to do in list? else I would need some more time to change the code

thheller09:07:43

yes, of course it is possible. it just is very ugly and inefficient code

thheller09:07:42

basically you want (update-in db [:a :b :c] update-first-list-element assoc :d "updated")

thheller09:07:08

the update-first-list-element fn you of course need to write 😉

thheller09:07:47

of course that different if the index may actually change and so on

thheller09:07:27

there are also many libs that tackle exactly these kinds of deeply nested datastructure manipulation problems

popeye09:07:59

what should be update-first-list-element if index is 5 ?

thheller10:07:25

well a update-nth-list-element function you also write

thheller10:07:36

manipulating lists is a bit annoying thats why I'm trying to avoid just giving you the code

thheller10:07:57

unless you have an actual very good reason you need a list you should be using a vector

popeye10:07:05

I see, Thanks for helping me to understand this 🙂

thheller10:07:45

since I feel bad for just leaving you hanging here would be an example fn impl

thheller10:07:50

(defn update-nth-list-element [the-list target-idx update-fn & args]
  (->> the-list
       (map-indexed
         (fn [idx item]
           (if (not= idx target-idx)
             item
             (apply update-fn item args))))
       (doall)))


(update-nth-list-element (list 1 2 3) 1 inc)

thheller10:07:20

so (update-in db [:a :b :c] update-nth-list-element 4 assoc :d "updated")

thheller10:07:55

far from ideal though

popeye10:07:46

ahhh , I see, this looks more lengthy though

reefersleep10:07:15

I would add; try to avoid deeply nested structures, if you can. As the above discussion shows, updating them tends to be ugly, and can be error fraught. Perhaps it’s both easy and simple if you use the right library, I don’t know.

thheller10:07:31

yes, absolutely that too 🙂

popeye10:07:47

how to update values list type to vector here? {:data ({:a "A" :b "B")} to

{:data [{:a "A" :b "B")}]}

thheller10:07:30

(update thing :data vec)

thheller10:07:57

or better have the thing that created that list create a vector instead (likely a lazy-seq?)

popeye10:07:04

hmmm... I need to have (update thing :data vec) for my scenario

popeye10:07:24

But I got the expected output now after changing 🙂 Thanks @U05224H0W for you r help