Fork me on GitHub
#clojure-nl
<
2022-02-10
>
Mno08:02:33

👋

Thierry20:02:42

I want to use filter on a persistent list to filter out a certain element and assign that to the binding that runs the filter, this works just fine. But what I would really like to do is update that same element from within the the filter. Is that possible? I've been grinding my head for the last cpl of hours but am not succeeding. Here's an example, lets say I have a binding called cases. In the example I predefine them but in my app they come from another function. I want to get out 1 case called mycase and for example print that. This works just fine. (let [cases '({:somename ({:id "2212020"}), :admin 12, :id 123, :defid "9985", :somevalue "2022-02-10T18:00:08.000Z"} {:somename ({:id "1231"}), :admin 13, :id 456, :defid "9985", :somevalue "2022-02-10T18:00:08.000Z"} {:somename ({:id "345345"}), :admin 14, :id 789, :defid "9985", :somevalue "2022-02-10T18:00:08.000Z"})       mycase (remove (fn [case] (if (= (:admin case) 13) (println "got it!") true)) cases)]   (println mycase)) Output: (got it! {:somename ({:id 1231}), :admin 13, :id 456, :defid 9985, :somevalue 2022-02-10T18:00:08.000Z}) Would it be possible to update the contents of mycase that I collect using remove while removing it? I have tried but failed.

borkdude20:02:38

so you would like to update mycase - you can do this either with a new map over that seq, of use a transducer while removing

borkdude20:02:49

(map update-fn mycase)

borkdude20:02:29

or:

(into [] (comp (remove ...) (map update-fn)) cases)

Thierry20:02:35

update-fn? that would be the function that I define updates it right?

borkdude20:02:58

yes, update-fn is just a placeholder that you write

borkdude20:02:12

user=> (remove odd? [1 2 3 4 5])
(2 4)
user=> (map inc (remove odd? [1 2 3 4 5]))
(3 5)
user=> (into [] (comp (remove odd?) (map inc)) [1 2 3 4 5])
[3 5]

borkdude21:02:32

you can also use keep:

user=> (keep (fn [n] (when (not (odd? n)) (inc n))) [1 2 3 4 5])
(3 5)

Thierry21:02:32

cool thanks will try