Fork me on GitHub
#clojure-europe
<
2020-09-30
>
otfrom08:09:24

driech here today

slipset08:09:28

GOd morgen

slipset08:09:35

So in some talk or another Rich says that sets are undervalued and underutilized data structures, and I see from our code that in many places where we could have used sets, we end up using lists or vectors.

3
slipset08:09:07

Much because map/filter et al don't preserve the type of the input collections.

slipset08:09:55

But I have a feeling that that adding maps and filters would probably be the wrong approach and that this should rather be handled with transducers in some way? Thoughts?

slipset08:09:09

Btw this one is rather nice:

user=> (disj #{"foo" "bar" "baz" nil} nil)
#{"foo" "bar" "baz"}
user=>

slipset08:09:29

compared to

user=> (filter identity ["foo" "bar" "baz" nil])
("foo" "bar" "baz")
or
user=> (remove nil? ["foo" "bar" "baz" nil])
("foo" "bar" "baz")
user=>

otfrom09:09:51

@slipset interesting. I've noticed lately that I'm only really using filter/map/etc in the context of transducers so I'm usually preserving the type of the collection by doing into or transduce with a collection type (same w/reduce)

otfrom09:09:57

so I've not had that problem

otfrom09:09:11

I love sets and use them for deduplication and as predicates quite a lot

otfrom09:09:57

(into #{} (filter identity) [:foo :bar :baz nil])

otfrom09:09:20

I frequently have things like

(if (#{:foo :bar} (:my-key my-map)) do-this do-that)

slipset09:09:51

Yeah, I love the fact that sets and maps acts as fns.

slipset09:09:48

One could choose to get annoyed by the fact that keys doesn't return a set

otfrom09:09:38

yeah, keys should return a set (as they are unique by contract)

slipset09:09:35

(fnil conj []) seen and used before 🙂

slipset09:09:42

This use of transducers is a bit interesting:

slipset09:09:01

Any reason for that instead of

(->> (get tagged-episode ::edit) (map ::command) (into #{}))

slipset09:09:39

Since you're not composing the transducer?

otfrom09:09:57

@slipset not a good one other than consistency and every time I end up having a single thing in my trasnducer pipeline the universe laughs and says "ha! you need another"

😂 3
otfrom09:09:39

when I write them I tend to do the following:

(into #{}

  my-coll)
and then put the transducing functions in the middle as I work through it

otfrom10:09:01

I used to write my ->> like this

(->> my-coll

  )

otfrom10:09:32

so moving from thread last to into wasn't a big shift for me really

otfrom10:09:00

most of the things about transducers go on about the internals and are a bit light on the using, which wasn't too hard once I grokked how to go from one to another

otfrom13:09:49

@slipset literally comping in another xf into an into as I need to filter some things out before I do a sort. 😉

otfrom14:09:12

good day to you sir!

borkdude15:09:38

What are the most useful Java classes to use reify with in your opinion?

Ben Hammond16:09:58

clojure.lang.IReduceInit? is that cheating?

Ben Hammond16:09:01

java.util.Iterator and java.util.Iterable are also big favourites

dominicm18:09:09

All of them

dominicm18:09:31

I doubt you could get a non biased answer. Maybe try a github search or something.

dominicm20:09:54

Ah, so deref then :)