Fork me on GitHub
#clojure-europe
<
2020-08-24
>
synthomat06:08:14

good morning!

synthomat07:08:33

(link leads to 404, but the preview works)

ordnungswidrig07:08:06

Thanks, link fixed (again) 🙂

synthomat07:08:43

haha she forgot to change the last dict to “dictionary”

ordnungswidrig07:08:08

I always thought python was a dictionary based language. But this mean string keys, I guess

synthomat07:08:05

is it about how dict() works?

synthomat07:08:50

doesn’t ** expand to method arguments, and using anything else than a string as a named parameter would fail anyway in python?

ordnungswidrig07:08:32

I mean, in clojure we do (merge foo bar) without givin a single thought.

synthomat07:08:18

oh yeah it’s weird that python has so many ways of merging to dicts 😄 it makes me even wonder, that this post might get more attention than the manual

synthomat07:08:30

because it’s not documented well enough

otfrom07:08:36

Nice to talk about something other than elisp here 😉

dominicm09:08:46

viml is pretty great

otfrom13:08:20

do you prefer by-key or by-reduce?

otfrom13:08:29

(def setting-groups [{:category 1 :name "foo"}
                       {:category 1 :name "bar"}
                       {:category 2 :name "baz"}
                       {:category 2 :name "quux"}])

  (def by-reduce (reduce
                  (fn [acc new]
                    (update acc (:category new)
                            (fnil conj #{}) (:name new)))
                  {}
                  setting-groups))

  (require '[net.cgrand.xforms :as x])

  (def by-key (into {}
                    (x/by-key :category
                              (comp (map :name)
                                    (x/into #{})))
                    setting-groups))

  (= by-reduce by-key)
  true

  by-key
  {1 #{"foo" "bar"},
   2 #{"baz" "quux"}}

otfrom13:08:34

you being everyone here 😉

otfrom13:08:04

or what do you think the trade offs are and under what circumstances would you prefer one over another

otfrom13:08:37

I do a lot of things where I want to group by something (user id, date, other) and then want to process all the things in that group, so I like x/by-key but it melts my head occasionally so getting used to it with small examples helps me I think

dominicm13:08:23

Both are equally opaque, but by-key has a docstring, tests and reuse, so I'd use that.

genRaiy14:08:47

I like by-reduce cos it's based on core functions so less to learn

genRaiy14:08:26

and that's not just cos I hate to learn ;-)

plexus14:08:32

I think by-key is awesome, but I don't trust myself to remember how it works for more than a few weeks. It's a real mind-bender. So unless it's code that already heavily uses transducers and xforms I would probably prefer the reduce.

👍 3
dominicm15:08:13

^ I like that caveat too. I'm assuming that you're using transducers and such elsewhere in your program and therefore further use of them is reduced confusion compared to custom solutions. It'd be like requiring ring and then re-implementing all the header parsing yourself 😄

otfrom15:08:40

Sorry, a bit unclear. I wouldn't create a new by-reduce function. I just as l wanted to show that the x/by-key and the reduce versions produce the same result

otfrom15:08:02

I keep forgetting how by-key works, but do find it creates clearer code when I do remember how to use it, so I'm just wondering if I should use it more often to keep it fresh, or just use reduce most of the time

otfrom15:08:40

The one big advantage is being able to put it in a transducer pipeline, and I do those a lot

otfrom15:08:05

And I'd presume it performs reasonably well with larger amounts of data