Skip to content

Derive methods from general accumulators? #34

@dmitriz

Description

@dmitriz

It seems that many methods can be derived from the following general accumulator combinator:

accum<A>(init: Behavior<A>, fnStream: Stream<Behavior<(a: A) => A>>): Behavior<Behavior<A>>

Here accum(init, fnStream) begins with init and is mapped successively through each function in the fnStream:

const accum = (init, fnStream ) => t1 => t2 =>
    fnStream
        .filter({ time } => (time >= t1) && (time <= t2))
        .map({ value } => value(t))
        .reduce((acc, feed)=>feed(acc), init(t))

Now we can derive:

const stepper = (init, stream) =>
    accum(Behavior.of(init), stream.map(a => Behavior.of(b => a)))

const switcher = (init, streamBeh) =>
    accum(init, streamBeh.map(beh => t => a => beh(t)))

const scan = (fn, init, stream) => 
    accum(Behavior.of(init), stream.map(a => Behavior.of(b => fn(a, b)))

Is this correct?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions