This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-07
Channels
- # adventofcode (3)
- # announcements (6)
- # babashka (20)
- # beginners (53)
- # calva (11)
- # clj-kondo (11)
- # clojure (50)
- # clojure-argentina (4)
- # clojure-dev (1)
- # clojure-europe (14)
- # clojure-houston (1)
- # clojure-italy (2)
- # clojure-nl (4)
- # clojure-norway (3)
- # clojure-seattle (3)
- # clojure-uk (13)
- # clojurescript (2)
- # cloverage (1)
- # code-reviews (4)
- # conjure (2)
- # cursive (5)
- # datalevin (4)
- # datascript (33)
- # datomic (16)
- # events (1)
- # graphql (10)
- # gratitude (1)
- # honeysql (6)
- # introduce-yourself (2)
- # jobs (1)
- # lsp (88)
- # malli (8)
- # off-topic (3)
- # other-languages (4)
- # polylith (3)
- # re-frame (16)
- # reagent (17)
- # reitit (3)
- # releases (2)
- # remote-jobs (1)
- # rewrite-clj (3)
- # shadow-cljs (3)
- # slack-help (2)
- # sql (36)
- # testing (31)
- # tools-deps (41)
- # xtdb (23)
what do people think about having the capability to create reactions with history? e.g.:
(def counter (r/atom 0))
;; a "reactive reduce"
(def counts (r/reduce conj [] counter))
(swap! counter inc) ; 1
(swap! counter inc) ; 2
(swap! counter inc) ; 3
@counts
;; => [0 1 2 3]
FWIW I can remember only two places where that might be useful - undo/redo mechanism in re-frame and a similar thing but in re-frame-10x. But implementing something like that using your mechanism would also require an ability to circumvent the mechanism - i.e. to rollback from 3 to 1 above, you want to both reset counter
to 1 and to reset counts
to [0 1]
.
i think it could be useful for relatively static higher order operations though, like filtering values
normally you would have to maintain a list of all the counts in the source, filter the list then take the first. in this case the source can be a single value and we filter each individual value as it's propagated
How is what you propose different from just using add-watch
and swapping an extra ratom in there?
(re-frame/reg-sub ::counter #(:counter db))
(defn only-evens
[prev v]
(if (even? v) v prev))
(def evens (r/reduce only-evens 0 (re-frame/subscribe ::counter))
(defn counter-view
[]
[:div
[:div "Count: " @(re-frame/subscribe ::counter)]
[:div "Even count: " @evens]])
Funny you should mention re-frame - in that setting, I would definitely try to avoid r/reduce
. :)
And if I think about it outside of those preferences, I'm still not entirely convinced such a [relatively] complicated thing justifies the value it brings.
But maybe someone else might chime in and provide more useful commentary.
If I needed some sort of a reduced accumulator, I'd definitely store both the accumulator and the last value fed into the reducing function in app-db
itself.