This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-02-13
Channels
- # announcements (1)
- # babashka (28)
- # beginners (72)
- # biff (6)
- # calva (15)
- # clerk (14)
- # clj-otel (4)
- # cljdoc (4)
- # clojure (121)
- # clojure-europe (61)
- # clojure-nl (2)
- # clojure-norway (63)
- # clojure-uk (5)
- # datahike (35)
- # datalevin (37)
- # datomic (7)
- # emacs (2)
- # fulcro (6)
- # gratitude (1)
- # honeysql (2)
- # hyperfiddle (38)
- # malli (9)
- # matrix (24)
- # meander (4)
- # off-topic (10)
- # polylith (8)
- # reagent (2)
- # releases (1)
- # shadow-cljs (8)
- # spacemacs (4)
- # specter (1)
- # squint (5)
- # tools-deps (3)
Is it possible to compose replace-in
with multi-transform
? I couldn’t find anything in the docs about it, or through a Google search.
I came up with this hack, based on the replace-in impl. But I’m pretty sure mutable-cell
is not part of the public API, so this seems risky. I tested this and it seems to work, at least with small, simple data structure. But I’m not sure what mutable-cell
does-- why not just use an Atom? Or is there another approach someone can suggest?
(defn mk-transformer-with-state
[state transform-fn & {:keys [merge-fn] :or {merge-fn concat}}]
(fn [& args]
(let [res (apply transform-fn args)]
(if res
(let [[ret user-ret] res]
(->> user-ret
(merge-fn (spimpl/get-cell state))
(spimpl/set-cell! state))
ret)
(last args)))))
(defn replace-in-with-multi-transform
"How do we combine replace-in with a multi-transform?"
[]
(let [state (spimpl/mutable-cell nil)]
[(sp/multi-transform [sp/ALL :a sp/ALL :b
(sp/multi-path
[#(= % 5)
(sp/terminal
(mk-transformer-with-state state (fn [x] [(dec x) [x]])))]
[#(= % 3)
(sp/terminal
(mk-transformer-with-state state (fn [x] [(inc x) [x]])))]
)]
sample1)
(spimpl/get-cell state)]))