This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-24
Channels
- # announcements (26)
- # babashka (9)
- # beginners (63)
- # calva (2)
- # chlorine-clover (22)
- # cider (2)
- # cljsrn (8)
- # clojure (36)
- # clojure-europe (36)
- # clojure-italy (5)
- # clojure-nl (76)
- # clojure-spec (9)
- # clojure-uk (8)
- # clojurescript (39)
- # conjure (24)
- # cursive (19)
- # data-science (1)
- # datascript (10)
- # datomic (1)
- # emacs (2)
- # events (5)
- # figwheel-main (9)
- # fulcro (21)
- # graalvm (1)
- # helix (5)
- # jobs (1)
- # jobs-discuss (1)
- # kaocha (1)
- # leiningen (4)
- # meander (2)
- # off-topic (22)
- # re-frame (16)
- # reitit (3)
- # rewrite-clj (75)
- # rum (1)
- # sci (51)
- # shadow-cljs (110)
- # tools-deps (16)
- # vrac (9)
- # xtdb (23)
Good morning!
Thanks, link fixed (again) 🙂
I always thought python was a dictionary based language. But this mean string keys, I guess
doesn’t **
expand to method arguments, and using anything else than a string as a named parameter would fail anyway in python?
I mean, in clojure we do (merge foo bar)
without givin a single thought.
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
(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"}}
or what do you think the trade offs are and under what circumstances would you prefer one over another
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
Both are equally opaque, but by-key has a docstring, tests and reuse, so I'd use that.
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.
^ 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 😄
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
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