This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-09-08
Channels
- # architecture (8)
- # aws (25)
- # babashka (9)
- # beginners (57)
- # calva (16)
- # cider (16)
- # clj-kondo (3)
- # cljdoc (13)
- # cljsrn (6)
- # clojure (272)
- # clojure-europe (36)
- # clojure-losangeles (1)
- # clojure-nl (8)
- # clojure-poland (3)
- # clojure-spec (4)
- # clojure-uk (8)
- # clojuredesign-podcast (9)
- # clojurescript (92)
- # code-reviews (1)
- # conjure (8)
- # core-async (1)
- # cursive (13)
- # datalog (1)
- # datascript (35)
- # datomic (76)
- # duct (10)
- # emacs (5)
- # events (7)
- # figwheel-main (1)
- # fulcro (35)
- # graalvm (20)
- # graphql (6)
- # jobs (3)
- # klipse (1)
- # london-clojurians (1)
- # malli (3)
- # off-topic (223)
- # pathom (2)
- # pedestal (13)
- # portal (1)
- # reitit (6)
- # remote-jobs (1)
- # shadow-cljs (21)
- # specter (2)
- # sql (63)
- # tools-deps (85)
- # tree-sitter (4)
- # xtdb (6)
@neumann @nate This week I listened to the episode on partial
. I didn’t quite get the part about partial apply
in a thread, but besides that, it occurred to me that you can use partial
in a thread-first to sneak in a function that expects the injected param last by partial applying all the args except the last one, so that in effect it becomes also the first. So something like:
(-> value
(assoc ...)
(partial map some-fn))
Is this something you would expect to see in production code? Is it “cheating” / confusing? Thanks!!!the way I've accomplished that in the past is to just use the thread last macro. I forget who it was that I first heard that from, but it might be a Stuart Sierra tweet. For example:
(-> value
(assoc ...)
(->> (map some-fn)))
but now, I generally don't mix ->
with ->>
, unless it's in exploratory fiddle code, because of the mental leap between "operating on an entity" and "operating on a list"
@UGNFXV1FA Thanks for listening! I think you're missing a set of parens.
user=> (-> {:foo 1} (assoc :bar 2) ((partial map key)))
(:foo :bar)
I also concur with @nate that the ->>
trick is easier for me to understand.@UGNFXV1FA Here's a use of partial apply
too:
user=> (def max-list (partial apply max))
#'user/max-list
user=> (max-list [1 2 3 4 5])
5
user=> (max 1 2 3 4 5)
5
Since max
takes a variable number of parameters, you can use partial apply
to create a function that expects a seq.
And if you want to use max
in threading, that's where the apply
comes in:
(->> [{:name "Foo" :age 32} {:name "Bar" :age 64}]
(map :age)
(apply max))
Ok, got ya on the thread first/last, I understand what you mean and it sounds like a sensible thing not to mix them, so I’ll also go with that 🙂 As for the partial apply
IIRC you explicitly mentioned using that as a last statement in a thread, but as also in your example above you don’t need partial
in that case. Or am I misremembering and/or misunderstanding?