Fork me on GitHub
#clojure-europe
<
2024-07-29
>
ray05:07:48

Good morning

nice 3
schmalz07:07:19

Morning all.

Thomas Moerman07:07:18

It's hard to stay off the after weekend-warrior shenanigans

dharrigan07:07:59

Good Morning!

reefersleep09:07:23

Good morning!

reefersleep09:07:16

I still see my (newer) colleagues occasionally doing

(or (= :a foo)
    (= :b foo))
instead of
(#{:a :b} foo)
Do you think there are stronger arguments for doing it either way? Sets as fns is second nature to me by now, and I love their succinctness. Also for stuff like
(filter (comp #{:a} :some-field) coll)

daveliepmann09:07:20

I'm on team sets-as-fns and comp with the argument being that it most closely reflects my intent

3
Ludger Solbach09:07:18

Me too. It's easier to add to the set than add expressions to an or fn. It is also more mathematical.

ray09:07:09

I’m in the set of folks that prefer sets

thinking-face 5
😄 1
teodorlu10:07:20

I also like sets :) It's more concise, especially compared to (or (= ,,,) (= ,,,)) - Last time I wrote clojure with people who didn't have much clojure experience, I held a bit back on the idioms that I knew wouldn't be familiar. The or looks about how it would be in Javascript. I think it's fine to adapt coding style a bit to the prople one's working with - even if one solution is considered more idiomatic by experienced clojure programmers.

👆 1
simongray12:07:24

I also use sets, but I prefer naming the set and using an explicit get, e.g. (get valid-stuff x)

1
yes 1
simongray12:07:32

I've also done (valid-stuff? x) but stopped doing that after learning that predicates should explicitly return true/false (i.e. can't just be a set if you're a pedant like me).

teodorlu12:07:15

Interesting - I haven't been naming my sets, I think that would be helpful.

ray12:07:32

Two or cases is ok… three or more starts to justify the idiom

seancorfield15:07:25

How do people feel about an explicit contains? call: (contains? #{:a :b} foo)

upvote 4
daveliepmann15:07:33

I can go either way — sometimes it feels like it helps, sometimes it feels superfluous

teodorlu15:07:29

I often add a (contains? ,,,), I find reading it a little easier. Might just be what I'm familiar with. I sometimes add a specific get for map lookup too.

Ben Sless16:07:36

Pointer equality vs. PersistentHashMap lookup It depends 🙃

Ben Sless16:07:03

For statically known sets, if the idiom repeats often enough, I usually write a macro that unrolls to an or of identity checks for up to quite a large number of keys

teodorlu16:07:42

Is the goal of that macro improved performance / short circuiting?

Ben Sless16:07:32

Improved performance. A sequence of pointers comparisons will beat a set lookup probably up until the bytecode gets too large

👍 1
1
reefersleep16:07:34

Explicit 'contains' or '(or (= ...' are an easier way into Clojure for beginners/passing-byers, completely agree.

reefersleep17:07:25

Additionally, I love the set of set operations. Small and effective.

borkdude20:07:49

For statically known setsI have a macro in clj-kondo which creates a case expression out of this:

(one-of :foo [:foo :bar :baz])

teodorlu21:07:24

Is case faster than (or (= ,,,) ,,,)?

borkdude21:07:01

it depends, benchmark and you will know, but usually (or (= ...)) is faster than set membership, which is why I often still use it if that's important

👍 2
Ben Sless02:07:33

Case creates big ugly bytecode If the set is only keywords you can even use identical? Up to a ridiculous number of elements (or (identical? ...) ...) Will be faster and smaller than a case --- I also call that macro one-of 😄

ray07:07:43

It would take a very hot flame graph

🔥 1
otfrom12:07:28

morning

☁️ 2