Module

Hareactive.Combinators

Package
purescript-hareactive
Repository
funkia/purescript-hareactive

#applyS Source

applyS :: forall b a. Behavior (a -> b) -> Stream a -> Stream b

This 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 a

A combination of filter and apply

#filter Source

filter :: forall a. (a -> Boolean) -> Stream a -> Stream a

Filter a stream, keeping the elements which satisfy a predicate function, creating a new stream.

Semantically.

filter p s = filter (\(time, a) -> p x) s

#filterJust Source

filterJust :: forall a. Stream (Maybe a) -> Stream a

Removes 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

#keepWhen Source

keepWhen :: forall a. Stream a -> Behavior Boolean -> Stream a

Filter a stream, keeping the elements which satisfy a predicate function, creating a new stream.

keepWhen s b = filter (\{time, a} -> b time) s

#sample Source

sample :: forall a. Behavior a -> Now a

Returns the current value of the behavior in the Now. This is possible because computations in the Now monad have an associated point in time.

#snapshot Source

snapshot :: forall b a. Behavior a -> Stream b -> Stream a

Creates a stream that occurs exactly when the given stream occurs. Every time the stream s has an occurrence the current value of the behavior is sampled. The value in the occurrence is then replaced with the sampled value.

#snapshotWith Source

snapshotWith :: forall c b a. (a -> b -> c) -> Behavior b -> Stream a -> Stream c

Returns 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.

#logS Source

logS :: forall a. Show a => String -> Stream a -> Stream a

#logB Source

logB :: forall a. Show a => String -> Behavior a -> Behavior a

#scan Source

scan :: forall b a. (a -> b -> b) -> b -> Stream a -> Behavior (Behavior b)

For each occurrence on the stream the function is applied to the value and the accumulator.

scan f a s =
  \from, to -> foldr f a <<< map (_.a) <<< filter ({time} -> from <= time && to <= endT) $ s

#scanS Source

scanS :: forall b a. (a -> b -> b) -> b -> Stream a -> Behavior (Stream b)

Similar to scan but instead of returning a behavior it returns a stream.

#stepper Source

stepper :: forall a. a -> Stream a -> Behavior (Behavior a)

Creates a behavior whose value is the last occurrence in the stream.

#switchTo Source

switchTo :: forall a. Behavior a -> Future (Behavior a) -> Behavior a

Creates a new behavior that acts exactly like the first behavior until the future occurs after which it acts like the behavior from the future.

#switcher Source

switcher :: forall a. Behavior a -> Stream (Behavior a) -> Behavior (Behavior a)

Creates a behavior that initially acts like the first behavior and then switches to each new behavior from the stream.

#switchStream Source

switchStream :: forall a. Behavior (Stream a) -> Stream a

Takes 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.

#time Source

time :: Behavior Number

A behavior whose value is the number of milliseconds elapsed since UNIX epoch.

#timeFrom Source

timeFrom :: Behavior (Behavior Number)

A behavior giving access to continous time. When sampled the outer behavior returns a behavior whose value is the time since the outer behavior was sampled.

Semantically.

timeFrom = \from, to -> to - from

#changes Source

changes :: forall a. Behavior a -> Stream a

Takes a behavior and returns a stream that has an occurrence whenever the behavior changes.

#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

#moment Source

moment :: forall b. ((forall a. Behavior a -> a) -> b) -> Behavior b

#performAff Source

performAff :: forall a. Aff a -> Now (Future (Either Error a))

Takes a stream of Aff and runs each side-effect. The returned stream has an occurrence for the result from each asynchronous computation.

#runStreamAff Source

runStreamAff :: forall a. Stream (Aff a) -> Now (Stream (Either Error a))

#runNow Source

runNow :: forall a. Now a -> Effect a

Returns an Effect that executes the Now computation.