Module React.S
Signal combinators.
Consult their semantics.
Primitive and basics
type 'a t
= 'a signal
The type for signals of type
'a
.
val const : 'a -> 'a signal
const v
is alwaysv
, [const v
]t= v
.
val create : ?eq:('a -> 'a -> bool) -> 'a -> 'a signal * (?step:step -> 'a -> unit)
create i
is a primitive signals
set toi
and aset
function. The functionset
is such that:set v
sets the signal's value tov
at the time it is called and triggers an update step.set ~step v
sets the signal's value tov
at the time it is called and updates it dependencies whenstep
is executedset ~step v
raisesInvalid_argument
if it was previously called with a step and this step has not executed yet or if the givenstep
was already executed.
Warning.
set
must not be executed inside an update step.
val value : 'a signal -> 'a
value s
iss
's current value.Warning. If executed in an update step may return a non up-to-date value or raise
Failure
if the signal is not yet initialized.
val retain : 'a signal -> (unit -> unit) -> [ `R of unit -> unit ]
retain s c
keeps a reference to the closurec
ins
and returns the previously retained value.c
will never be invoked.Raises.
Invalid_argument
on constant signals.
val stop : ?strong:bool -> 'a signal -> unit
stop s
, stops updatings
. It conceptually becomesconst
with the signal's last value and cannot be restarted. Allows to disable effectful signals.The
strong
argument should only be used on platforms where weak arrays have a strong semantics (i.e. JavaScript). See details.Note. If executed in an update step the signal may still update in the step.
val equal : ?eq:('a -> 'a -> bool) -> 'a signal -> 'a signal -> bool
equal s s'
istrue
iffs
ands'
are equal. If both signals areconst
anteq
is used between their value (defauts to structural equality). If both signals are notconst
ant, physical equality is used.
val trace : ?iff:bool t -> ('a -> unit) -> 'a signal -> 'a signal
trace iff tr s
iss
excepttr
is invoked withs
's current value and ons
changes wheniff
istrue
(defaults toS.const true
). For all t where [s
]t= v
and (t = 0 or ([s
]t-dt= v'
andeq v v' = false
)) and [iff
]t =true
,tr
is invoked withv
.
From events
Transforming and filtering
val app : ?eq:('b -> 'b -> bool) -> ('a -> 'b) signal -> 'a signal -> 'b signal
app sf s
holds the value ofsf
applied to the value ofs
, [app sf s
]t=
[sf
]t [s
]t.
val map : ?eq:('b -> 'b -> bool) -> ('a -> 'b) -> 'a signal -> 'b signal
map f s
iss
transformed byf
, [map f s
]t =f
[s
]t.
val filter : ?eq:('a -> 'a -> bool) -> ('a -> bool) -> 'a -> 'a signal -> 'a signal
filter f i s
iss
's values that satisfyp
. If a value does not satisfyp
it holds the last value that was satisfied ori
if there is none.- [
filter p s
]t=
[s
]t ifp
[s
]t= true
. - [
filter p s
]t=
[s
]t' ifp
[s
]t= false
and t' is the greatest t' < t withp
[s
]t'= true
. - [
filter p e
]t= i
otherwise.
- [
val fmap : ?eq:('b -> 'b -> bool) -> ('a -> 'b option) -> 'b -> 'a signal -> 'b signal
fmap fm i s
iss
filtered and mapped byfm
.- [
fmap fm i s
]t=
v iffm
[s
]t= Some v
. - [
fmap fm i s
]t=
[fmap fm i s
]t' iffm
[s
]t= None
and t' is the greatest t' < t withfm
[s
]t'<> None
. - [
fmap fm i s
]t= i
otherwise.
- [
val diff : ('a -> 'a -> 'b) -> 'a signal -> 'b event
diff f s
is an event with occurrences whenevers
changes fromv'
tov
andeq v v'
isfalse
(eq
is the signal's equality function). The value of the occurrence isf v v'
.- [
diff f s
]t= Some d
if [s
]t= v
and [s
]t-dt= v'
andeq v v' = false
andf v v' = d
. - [
diff f s
]t= None
otherwise.
- [
val sample : ('b -> 'a -> 'c) -> 'b event -> 'a signal -> 'c event
sample f e s
sampless
ate
's occurrences.- [
sample f e s
]t= Some (f ev sv)
if [e
]t= Some ev
and [s
]t= sv
. - [
sample e s
]t= None
otherwise.
- [
val on : ?eq:('a -> 'a -> bool) -> bool signal -> 'a -> 'a signal -> 'a signal
on c i s
is the signals
wheneverc
istrue
. Whenc
isfalse
it holds the last values
had whenc
was the last timetrue
ori
if it never was.- [
on c i s
]t=
[s
]t if [c
]t= true
- [
on c i s
]t=
[s
]t' if [c
]t= false
where t' is the greatest t' < t with [c
]t'= true
. - [
on c i s
]t=
i
otherwise.
- [
val dismiss : ?eq:('a -> 'a -> bool) -> 'b event -> 'a -> 'a signal -> 'a signal
dismiss c i s
is the signals
except changes whenc
occurs are ignored. Ifc
occurs initiallyi
is used.- [
dismiss c i s
]t=
[s
]t' where t' is the greatest t' <= t with [c
]t'= None
and [s
]t'-dt<>
[s
]t' - [
dismiss_ c i s
]0=
v
wherev = i
if [c
]0= Some _
andv =
[s
]0 otherwise.
- [
Accumulating
val accum : ?eq:('a -> 'a -> bool) -> ('a -> 'a) event -> 'a -> 'a signal
accum e i
isS.hold i (
Accumulatinge i)
.
Combining
val merge : ?eq:('a -> 'a -> bool) -> ('a -> 'b -> 'a) -> 'a -> 'b signal list -> 'a signal
merge f a sl
merges the value of every signal insl
usingf
and the accumulatora
.[
merge f a sl
]t= List.fold_left f a (List.map
[]tsl)
.
val switch : ?eq:('a -> 'a -> bool) -> 'a signal signal -> 'a signal
switch ss
is the inner signal ofss
.- [
switch ss
]t=
[[ss
]t]t.
- [
val bind : ?eq:('b -> 'b -> bool) -> 'a signal -> ('a -> 'b signal) -> 'b signal
bind s sf
isswitch (map ~eq:( == ) sf s)
.
val fix : ?eq:('a -> 'a -> bool) -> 'a -> ('a signal -> 'a signal * 'b) -> 'b
fix i sf
allow to refer to the value a signal had an infinitesimal amount of time before.In
fix sf
,sf
is called with a signals
that represents the signal returned bysf
delayed by an infinitesimal amount time. Ifs', r = sf s
thenr
is returned byfix
ands
is such that :- [
s
]t=
i
for t = 0. - [
s
]t=
[s'
]t-dt otherwise.
eq
is the equality used bys
.Raises.
Invalid_argument
ifs'
is directly a delayed signal (i.e. a signal given to a fixing function).Note. Regarding values depending on the result
r
ofs', r = sf s
the following two cases need to be distinguished :After
sf s
is applied,s'
does not depend on a value that is in a step ands
has no dependents in a step (e.g in the simple case wherefix
is applied outside a step).In that case if the initial value of
s'
differs fromi
,s
and its dependents need to be updated and a special update step will be triggered for this. Values depending on the resultr
will be created only after this special update step has finished (e.g. they won't see thei
ofs
ifr = s
).- Otherwise, values depending on
r
will be created in the same step ass
ands'
(e.g. they will see thei
ofs
ifr = s
).
- [
Lifting
Lifting combinators. For a given n
the semantics is :
[ln f a1
... an
]t = f [a1
]t ... [an
]t
val l1 : ?eq:('b -> 'b -> bool) -> ('a -> 'b) -> 'a signal -> 'b signal
val l2 : ?eq:('c -> 'c -> bool) -> ('a -> 'b -> 'c) -> 'a signal -> 'b signal -> 'c signal
val l3 : ?eq:('d -> 'd -> bool) -> ('a -> 'b -> 'c -> 'd) -> 'a signal -> 'b signal -> 'c signal -> 'd signal
val l4 : ?eq:('e -> 'e -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signal
val l5 : ?eq:('f -> 'f -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signal -> 'f signal
val l6 : ?eq:('g -> 'g -> bool) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> 'a signal -> 'b signal -> 'c signal -> 'd signal -> 'e signal -> 'f signal -> 'g signal
module Bool : sig ... end
module Int : sig ... end
module Float : sig ... end
module Pair : sig ... end
module Option : sig ... end
module Compare : sig ... end
Combinator specialization
Given an equality function equal
and a type t
, the functor Make
automatically applies the eq
parameter of the combinators. The outcome is combinators whose results are signals with values in t
.
Basic types are already specialized in the module Special
, open this module to use them.
module Make : functor (Eq : EqType) -> S with type 'a v = 'a Eq.t
Functor specializing the combinators for the given signal value type
module Special : sig ... end
Specialization for booleans, integers and floats.