beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Simon 2021-06-09T12:06:34.472Z

Is there an error in this code from https://www.learn-clojurescript.com/section-3/lesson-20-capstone-3-contact-book/. IMU assoc returns a new map and so the new map is never used an ignored?

tws 2021-06-09T12:11:00.472200Z

the new map is the result of the if statement, so the function returns it. btw, looking at that page, the prior block’s if-let is def the way to go there.

πŸ™Œ 1
X 2021-06-09T12:11:03.472400Z

i think you missed that it’s inside if statement, so it takes 2 forms: β€’ (let [address …. β€’ clean-contact

πŸ™Œ 1
nod 2021-06-09T12:13:46.473800Z

Hey! Quick question: How can I efficiently update every nth element in a vector? As in, I want something faster than iterating over every single element. Thanks!

Simon 2021-06-09T12:20:08.473900Z

Thanks for both of your answers. Aah Yes @nicolas.misiura I didn't realize that clean-contact was the branch taken, when the expression is false. I just have to get used to the lack of else

alexmiller 2021-06-09T12:20:52.475Z

Loop and update with assoc on the index

alexmiller 2021-06-09T12:27:53.479700Z

(loop [coll v, i 0]
  (if (< i (count v))
    (recur (assoc coll i (inc (nth coll i))) (+ i 10))
    coll))

alexmiller 2021-06-09T12:28:32.480200Z

Increments every 10th element

nod 2021-06-09T12:35:02.480700Z

Thank you, Alex!

thomas 2021-06-09T17:23:25.483400Z

if you want to optimize for readability I took a stab at this using partition for fun πŸ™‚

; every third indexed element is now the last element in a nested list
user=> (partition 3 [1 2 3 4 5 6 7])
((1 2 3) (4 5 6))

; increment every 3rd element by 1
user=> (->> (partition 3 [1 2 3 4 5 6 7])
  #_=>      (map (comp inc last)))
(4 7)
but probably not very efficient πŸ™‚

thomas 2021-06-09T17:55:59.484200Z

oh wait this doesn't satisfy the requirement I guess the output is supposed to be

[1 2 4 4 6 7 7]
I gave it another shot. trying to avoid assoc using butlast instead but it became quite ugly. πŸ˜•

2021-06-09T19:52:01.485300Z

I want ot replace each item in a collection if the index is a second collection with a replacement value. Is their a nicer way than this?

(let [maze [:none :none :none :none :none :none :none :none :none :none]
      path #{0 2 4 6 8}
      replacement :path]
  (map-indexed (fn [idx tile] (if (path idx) replacement tile)) maze))
Maybe something that doesn't involve visiting every item?

dpsutton 2021-06-09T19:54:51.485800Z

(let [maze [:none :none :none :none :none :none :none :none :none :none]
      path #{0 2 4 6 8}]
  (reduce #(assoc %1 %2 :path) maze path))
vectors are associative with respect to (existing) index

dpsutton 2021-06-09T19:56:08.486900Z

caveat, use a data structure that provides the access patterns that you need. Perhaps a sparse map {0 :path 2 :path 4 :path ...} style map, or whatever data structure allows for the read and write patterns you will need.

2021-06-09T19:57:21.487700Z

hmmm. I have a vector that represents a 2d maze (I store it as single dimension and at the UI partition it up), I'd lose the order if I use map, I think.

popeye 2021-06-09T20:45:13.491800Z

Hi team, I have a situation in my code that, I need to call SQL queries for each client id and get the results, so I am writing it in reduce function, but this is costly operation, Calling database query every time, is there a way where we can improve this?

practicalli-john 2021-06-12T10:25:29.083700Z

@popeyepwr there is an #sql channel

πŸ‘ 1
R.A. Porter 2021-06-09T20:47:28.491900Z

You could pull out all the client ids and use those in your where clause (or groups of them at a time).

popeye 2021-06-09T20:48:34.492100Z

What if I have multiple keys, like client id and client region, ?

R.A. Porter 2021-06-09T20:50:09.492300Z

You could still extract and build a batch query, it'd just be a bit more massaging.

popeye 2021-06-09T20:52:36.492500Z

Any link you suggest?

popeye 2021-06-09T20:54:39.492700Z

Like I want to send clientid=1 and region=us in query

popeye 2021-06-09T20:55:14.492900Z

clientid=2 and region=can in another query

R.A. Porter 2021-06-09T20:55:15.493100Z

Not really. You'd just be building a where clause like... where (clientid = 1 and region = 'us') or (clientid = 2 and region = 'can') ...

R.A. Porter 2021-06-09T20:55:48.493400Z

That's really a SQL question, though. Not so much a Clojure one. πŸ™‚

popeye 2021-06-09T20:56:05.493600Z

Agreee