Module

Data.Hareactive

Package
purescript-hareactive
Repository
funkia/purescript-hareactive

#Now Source

data Now :: Type -> Type

A Now represents a computation that occurs at a specific moment in time. That moment is always "now". This means that computations inside the now can depend on time—but only the current point in time. The requirement that Time is always the current moment in time is enforced and guaranteed by the API for working with time.

In addition to containing time-dependent computations a Now can also contain computations with side-effects. An approximate model of a Now a is that it is equivalent to Time -> IO a.

Now servers two purposes in Hareactive: It makes it possible to create stateful behaviors that depends on the past without introducing space-leaks (a notorious problem in FRP). Additionally, it is the glue between FRP primitives and effectful computations.

Instances

#Behavior Source

data Behavior :: Type -> Type

A behavior represents a value that changes over time. I.e. a value that depends on time. Semantically a Behavior a can be understood as being equivalent to a function Time -> a. A behavior isn't implemented as a function. But the semantics serve as a mental model in terms of which all other operations can be understood.

Instances

#Stream Source

data Stream :: Type -> Type

A stream represents events that occur at specific moments in time. It is a list of future values. Semantically a Stream a can be understood as List (Time, a). That is, a list of values where each value is associated with a specific moment in time. The time values have to be increasing. But, they do not have to be strictly increasing. This means that a stream can have several occurrences at the same moment in time. This can be very useful in certain circumstances.

Instances

  • Semigroup (Stream a)

    The Semigroup instance merges to streams by combining their occurrences while keeping them ordered with respect to time.

    One detail to be aware of is what happens in case both the left and the right stream contains occurences that occur simultaneously. The sortWith in the semantics below is stable. This implies that simultaneous ocurrences in the left stream will occurr before ones in the righ stream.

    Semantically.

    append s1 s2 = sortWith (\(time, a) -> time) (s1 <> s2)
    
  • Monoid (Stream a)

    The Monoid instance lets mempty be a stream without any occurrences.

  • Functor Stream

#Future Source

data Future :: Type -> Type

A future is a value associated with some point in time. Semantically a Future a is equivalent to (Time, a). That is, a value tagged with time. In practice, Stream is used more often than Future. Future is useful in circumstances where some event occurs exactly once in the future.

Instances

#apply Source

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

Whenever the stream has an occurence the function at the behavior is applied to the value of the occurrence.

Semantically.

apply 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

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

#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)

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