Fork me on GitHub
#beginners
<
2021-10-13
>
Alexey Baryshnikov09:10:59

Hi guys. Did anybody meet a problem like this? I use shadow-cljs with react-native and try to set up a project from scratch as mentioned here https://github.com/thheller/reagent-react-native but stuck with that error.

Adie10:10:12

How do I add multiple maps like this

{:vehicle  "default"
               :service  "default"
               :config   "my-config"
               :country   "default"
               :value     2500}
at once to a list which already has maps of this type

pyry10:10:35

So ultimately the question seems to just be about "how do I add items to a list"?

pyry10:10:19

Many possibilities there: conj, into, concat, ...

pyry10:10:38

The different functions cater to slightly different needs: some allow for appending multiple items in one go versus just adding one item, into preserves the type of the target collection,...

Fredrik11:10:50

Can you provide an example of what you want to achieve?

Fredrik11:10:09

Do you have a list of maps

(A B C)
where the maps A, B and C looks like what you described above? And you want to add more maps D, E and F to this list?

Fredrik11:10:05

Then you can use (into '(A B C) '(D E F)) , which will return the list '(F E D A B C), or replace into with concat, which will return '(A B C D E F). The reason these two give different results is that into takes elements from the second list one at a time and adds it to the first one, each time placing that element at the front of the list, while concat just concatenates them together.

Dmitrii Sherstobitov11:10:42

hi everyone how this code may be rewritten in clojure way correctly?

(if smth
  (do smth
      return smth)
  (if smth2
     (do smth
         return smth)
      (if smth3
           ….)
I’ve tried (some->> args fn1 fn1) but it doesn’t fir here because I don’t understand how to pass few arguments as list, and I need to return smth in if leaf. I understand that there is no returns but still )

Fredrik11:10:03

What are you trying to achieve? If either smth, smth2 or smth3 are true, then evaluate smth?

valerauko11:10:32

can you write what you try to achieve in another language? (js etc)

Dmitrii Sherstobitov11:10:24

sorry about confusing code) is other languages it will be simple if sequence

if a is None and b is None:
   # do somthing and
   return map[]
elif a is not None and b is None:
   # do somthing and
   return map[]
elif a is None and b is not None:
   # do somthing and
   return map[]
else:
   # do somthing and
   return map[]

valerauko11:10:13

you can use a cond for that, just a sec

(cond
  (and (not a) (not b))
  (do
    ;; some processing
    return-value1)
  
  (and a (not b))
  (do
    ;; some processing
    return-value2)
  
  (and (not a) b)
  (do
    ;; some processing
    return-value3)
  
  :else
  (do
    ;; some processing
    return-value4))
cf https://clojuredocs.org/clojure.core/cond

Dmitrii Sherstobitov11:10:28

got it, thanks a lot!

✌️ 1
🙂 1
Eric Boatman18:10:55

When dealing with a lot of map reuse ... is it more proper to (map function data) or have the map inside the function where the output is just the "transformed" data (function data)? And does this apply to filters too?

Eric Boatman19:10:08

Thanks, thats a very good series ... unfortunately I only understood 50% of it .. I'm going to have to rethink what I'm doing. I'm going with the theory that if it looks messy. I'm doing it wrong.

Pablo19:10:25

Hello everyone ¿Do you know where are in-memory ring sessions stored? ¿Where is the atom that holds all sessions information?

Fredrik19:10:27

The default store is ring.middleware.session.memory/memory-store. Are you looking for a way to inspect it?

Pablo19:10:00

Yes, I want to write-session to specific ring session.

Fredrik19:10:22

Then you can pass it in the options map to the session middleware

Pablo19:10:27

But isn’t it a default instance of MemoryStore class?

Fredrik19:10:00

You are correct. But that instance has the method session-atom , which returns the atom for you

Fredrik19:10:17

My suggestion is that you explicitly create a MemoryStore , then pass it in the options map to wrap-session .

Fredrik19:10:02

(def session-store (mem/memory-store))

(def app
  (wrap-session handler {:store session-store})

Fredrik19:10:27

Now you can play with the atom through (mem/session-atom session-store)

Pablo19:10:38

Ok, I thought the same idea, but I wanted Ring to continue to be in control of everything. And then I wonder how does ring use the same atom in subsequent requests?

Fredrik19:10:56

Ring uses whatever session store you give to wrap-session

Fredrik19:10:20

And you don't give it anything, it reverts to mem/memory-store as a default

Pablo19:10:33

Does it creates a new atom on each request?

Fredrik19:10:13

No. The middleware wrap-session will use the same session store for each request it looks at

Pablo19:10:24

Okok. Thanks a lot 🙌

Pablo20:10:03

I think I got it. In this HTTP server declaration:

(mount/defstate http-server
  :start (http/start
          {:handler (wrap-session handler/app {:cookie-attrs {:http-only true}})
           :port    3000})
  :stop (http/stop http-server))
The atom is (indirectly) created when wrap-session is evaluated (via mem/memory-store) and is bound to the anonymous function (the middleware), so there is no way to access it.

Fredrik20:10:05

Yes, you are bang on. Very hard to access it without creating it first yourself and passing it in!

👍 1