Fork me on GitHub
#clojure-nl
<
2022-08-04
>
borkdude07:08:20

Morning

👋 3
1
Thierry15:08:21

Is it possible to make this shorter?

(filter #(and (= "F" (:type %)) (= 61170 (:id %))) matrix)

borkdude15:08:21

You could make it shorter, but I wouldn't bother as the code would likely become more obscure

✅ 1
arnout15:08:40

Indeed, looks pretty short and clear to me.

✅ 1
arnout15:08:39

Except, if you can use juxt of course: (filter (comp #{["F" 61170]} (juxt :type :id)) matrix). Just kidding.

2
arnout15:08:57

And throw in a transducer for good measure:

(def my-filter (filter (comp #{["F" 61170]} (juxt :type :id))))

(transduce my-filter conj matrix)

Mno15:08:24

Good ol transducers

Thierry15:08:01

Good to know I made it short already 🙂

Thierry15:08:48

juxt works even better for multiple filters thanks! still need to wrap my head around juxt..

Thierry16:08:56

Can someone explain why

(map #(into ["A" %]) [1234 5678])
is faster then
(for [i [1234 5678]] ["A" i])
When I macroexpand both of them the for loop has about 20+ lines and the map has only 1

borkdude16:08:14

Isn't that the explanation already? ;)

Thierry16:08:44

hehe good point