Hareactive.Combinators
- Package
- purescript-hareactive
- Repository
- funkia/purescript-hareactive
#applyS Source
applyS :: forall b a. Behavior (a -> b) -> Stream a -> Stream bThis function is similar to apply for behaviors except the last argument
is a stream instead of a behaviors. Whenever the stream has an occurrence
the function at the behavior is applied to the value of the occurrence.
This function has an operator alias <~>. The operator is intended to work
in tandem with <$> and <*>. As an example, assume that f3 is a
function of three arguments, that b1 and b2 are two behaviors, and that
s is a stream.` Then the function can be applied to the two behaviors and
the stream in the following way.
f3 <$> b1 <*> b2 <~> s
With the above code, whenever s has an occurrence the value of b1,
b2, and the value of the occurrence will be applied to f3 and its
return value will be the value of the occurrence in the resulting stream.
Semantically.
applyS b s = map (\{time, a} -> {time, a: (b time) a}) s
#filterApply Source
filterApply :: forall a. Behavior (a -> Boolean) -> Stream a -> Stream aA combination of filter and apply
#filterJust Source
filterJust :: forall a. Stream (Maybe a) -> Stream aRemoves all Nothing values from the stream and extracts the
values from the remaining Justs.
#split Source
split :: forall a. (a -> Boolean) -> Stream a -> Tuple (Stream a) (Stream a)Takes a predicate and a stream. A pair of streams is returned. The first stream includes all occurrences from the original stream for which the predicate is satisfied and the seconds stream all occurrences for which the predicate is false.
Example.
Tuple smallNumbers largeNumbers = split (_ < 100) streamOfNumbers
#snapshotWith Source
snapshotWith :: forall c b a. (a -> b -> c) -> Behavior b -> Stream a -> Stream cReturns a stream that occurs whenever the given stream occurs. At each occurrence the value and the value from the behavior is passed to the function and the return value is the value of the returned streams occurrence.
#switchStream Source
switchStream :: forall a. Behavior (Stream a) -> Stream aTakes a stream valued behavior and returns a stream that emits values from the current stream at the behavior. I.e. the returned stream always "switches" to the current stream at the behavior.
#toggle Source
toggle :: forall b a. Boolean -> Stream a -> Stream b -> Behavior (Behavior Boolean)Creates a behavior that switches between true and false. Initally it
takes the value of its first argument. Each occurrence of the first stream
will make the behavior true and each occurrence of the second stream
makes the behavior false.
The example below demonstrates one use case for toggle. A stream
doorOpen signifies that a door has been opened and similairly a stream
doorClose signifies that the door has closed. toggle is then used to
construct a behavior that at any time represents the state of the door.
isDoorOpen <- sample $ toggle false doorOpen doorClose