Fork me on GitHub
#beginners
<
2021-06-09
>
Simon12:06:34

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?

tschady12:06:00

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.

🙌 2
X12:06:03

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

🙌 2
Simon12:06:08

Thanks for both of your answers. Aah Yes @U0241S5522K 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

nod12:06:46

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!

Alex Miller (Clojure team)12:06:52

Loop and update with assoc on the index

Alex Miller (Clojure team)12:06:53

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

Alex Miller (Clojure team)12:06:32

Increments every 10th element

nod12:06:02

Thank you, Alex!

thomas17:06:25

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 🙂

thomas17:06:59

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. 😕

Stuart19:06:01

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?

dpsutton19:06:51

(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

dpsutton19:06:08

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.

Stuart19:06:21

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.

sova-soars-the-sora00:06:44

as @U11BV7MTK said you can turn that vector into an (ordered) map by making the key the index [77 76 75 74 73] becomes

{0 77 
 1 76
 2 75
 3 74
 4 73}
So you can access the first element doing (get m 0) because 0 is effectively the key name Might be better to use something like this for long-term sanity

popeye20:06:13

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?

R.A. Porter20:06:28

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

popeye20:06:34

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

R.A. Porter20:06:09

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

popeye20:06:36

Any link you suggest?

popeye20:06:39

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

popeye20:06:14

clientid=2 and region=can in another query

R.A. Porter20:06:15

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. Porter20:06:48

That's really a SQL question, though. Not so much a Clojure one. 🙂

practicalli-johnny10:06:29

@U01J3DB39R6 there is an #sql channel

👍 3