leonoel / missionary

A functional effect and streaming system for Clojure/Script
Eclipse Public License 2.0
620 stars 25 forks source link

generalize `signal` to any semigroup #92

Closed leonoel closed 10 months ago

leonoel commented 10 months ago

Observation : the set of all possible values forms a semigroup with {}.

Idea : similar to relieve working on arbitrary semigroups, signal can also work on arbitrary semigroups (not only {}). This is useful if a continuous time variable is defined not in term of successive states, but in term of successive diffs. If the diffs form a semigroup, we get the publisher for free.

Proposition :

It makes sense for {} to be the default semigroup, because it's always defined and idiomatic for continuous time.

Example for integers with + :

(def click-count (m/signal + (m/observe (fn [!] (! 0) (def click! !) #()))))
(def ps1 (click-count #(prn :step1) #(prn :done1)))       ;; :step1
@ps1                                                      ;; 0
(click! 2)                                                ;; :step1
@ps1                                                      ;; 2
(click! 3)                                                ;; :step1
(def ps2 (click-count #(prn :step2) #(prn :done2)))       ;; :step2
@ps2                                                      ;; 5
@ps1                                                      ;; 3
leonoel commented 10 months ago

Released in b.32