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?
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.
i think you missed that itβs inside if
statement, so it takes 2 forms:
β’ (let [address β¦.
β’ clean-contact
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!
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
Loop and update with assoc on the index
(loop [coll v, i 0]
(if (< i (count v))
(recur (assoc coll i (inc (nth coll i))) (+ i 10))
coll))
Increments every 10th element
Thank you, Alex!
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 π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. π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?(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) indexcaveat, 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.
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.
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?
@popeyepwr there is an #sql channel
You could pull out all the client ids and use those in your where clause (or groups of them at a time).
What if I have multiple keys, like client id and client region, ?
You could still extract and build a batch query, it'd just be a bit more massaging.
Any link you suggest?
Like I want to send clientid=1 and region=us in query
clientid=2 and region=can in another query
Not really. You'd just be building a where clause like...
where (clientid = 1 and region = 'us') or (clientid = 2 and region = 'can') ...
That's really a SQL question, though. Not so much a Clojure one. π
Agreee