Fork me on GitHub
#beginners
<
2022-05-30
>
popeye13:05:41

I have a map

(def  data {:a "A" {:b "B" :c "C" :d "D"}})
how can I use update-in to update both keys :c and :d so that the resulting map is `
{:a "A" {:b "B" :c "R" :d "T"}}
`

Alex Miller (Clojure team)13:05:07

update-in with assoc

πŸ™Œ 1
Baye14:05:19

Hi. What’s your favorite book or course you took to learn Data Structure and Algorithm? Is there a good one done in Clojure?

manas_marthi15:05:53

Better look for books in Java, python. No algorithm books in clojure. You can try SICP, how to design programs written using lisp/racket lang

πŸ‘ 1
Baye15:05:21

Not necessarily looking for one written in clojure (i thought there might be). Was more interested in your favorite one:the one that taught you well.

V19:05:10

Has good online resources also

jumar14:05:57

The "classic" books on Algorithms are usually big and and require a lot of work. For a less formal and lightweight intro, I think, Grokking algorithms is nice.

Benjamin17:05:47

(filter #{"foo"} ["foo" "bar"])
(filter #(= "foo" %) ["foo" "bar"])
which of these is better and is there a blog post about it?

solf17:05:12

First one is idiomatic

solf17:05:23

If you were filtering for more than one value it’s also trivial to do it with sets, not so much with the 2nd method

Benjamin18:05:10

ok nice because I wanted the first

Ben Sless19:05:45

Better for what?

john01:05:28

Right, rarely would you want to filter on the identity of a whole thing in some collection. More often on some characteristic of it. Makes sense though if you want to count how many of a thing exists in a collection, for instance.

2FO20:05:53

Good day, Does Clojure have anything similar to Elixir's IO.inspect ? Which allows you to output the value at any point in a pipline without interrupting its execution. Example:

(-> args
(fun1)
(fun2)
(IO.inspect)
(fun3))
 

V20:05:28

Can just make a function:

(defn thread-print
  [x]
  (pp/pprint x)  
  x)

πŸ‘ 1
V20:05:31

(-> args
(fun1)
(fun2)
(thread-print)
(fun3))

πŸ‘ 1
seancorfield20:05:21

A simpler solution is to use doto:

(-> args
    (fun1)
    (fun2)
    (doto pp/pprint)
    (fun3))
(doto x f) calls (f x) and then returns x

πŸ‘ 1
βž• 1
seancorfield20:05:31

I use it with tap> all the time to send values to Portal (or Reveal or REBL or any other tap>-listener you have registered), since tap> rather annoyingly returns true or false rather than the value you tap!

πŸ‘ 1
2FO22:05:20

I'm running into this error when I try these:

Execution error (ArityException) at user/count-nums (REPL:13).
Wrong number of args (3) passed to: clojure.core/remove

2FO22:05:55

Here's my code:

(defn count-nums [nums]
    (->> nums
          (remove #(< % 5))
         (doto pprint))
    count)

seancorfield22:05:27

You can't use doto with ->> because the argument will be in the wrong place.

πŸ‘ 1
seancorfield22:05:11

You could do this:

(defn count-nums [nums]
  (->> nums
       (remove #(< % 5))
       (#(do (pprint %) %))
       count))
Ugly, but it will work.

πŸ‘ 1
seancorfield22:05:35

Sometimes it's just easier to define a debug function for stuff like this:

(defn debug [x] (pprint x) x)

(-> args
    (fun1)
    (fun2)
    (debug)
    (func3))

(defn count-nums [nums]
  (->> nums
       (remove #(< % 5))
       (debug)
       count))
Since debug takes only one argument and returns it (after printing it) you can use it with both -> and ->>

πŸ‘ 3
didibus05:05:43

Many people have their own little spy function like that. You can also get it from libraries like https://github.com/cloojure/tupelo and some offer more advanced forms of spying like https://github.com/philoskim/debux