Skip to content

Commit

Permalink
add instant, a wrapper for stateful functions
Browse files Browse the repository at this point in the history
  • Loading branch information
limemloh committed Aug 22, 2019
1 parent cc17692 commit 3d4f746
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { Now, MapNowTuple } from "./now";
import { Behavior, SinkBehavior, MapBehaviorTuple } from "./behavior";
import { Stream, SinkStream } from "./stream";
import { Now, MapNowTuple, FlashRun, flash, sample } from "./now";
import {
Behavior,
SinkBehavior,
MapBehaviorTuple,
AccumPair,
accum,
accumCombine,
stepper,
when,
toggle,
switcher,
freezeAt
} from "./behavior";
import { Stream, SinkStream, scan, shift } from "./stream";
import { Future, MapFutureTuple } from "./future";
import { integrate } from "./time";

export * from "./common";
export * from "./now";
Expand Down Expand Up @@ -60,3 +73,44 @@ export function combine<A>(
// FIXME: More performant implementation with benchmark
return (values as any).reduce((a: any, b: any) => a.combine(b));
}

export type InstantStart = {
run: FlashRun;
sample: <A>(b: Behavior<A>) => A;
scan: <A, B>(f: (a: A, b: B) => B, initial: B, s: Stream<A>) => Stream<B>;
accum: <A, B>(f: (a: A, b: B) => B, initial: B, s: Stream<A>) => Behavior<B>;
accumCombine: <B>(f: AccumPair<B>[], initial: B) => Behavior<B>;
stepper: <B>(initial: B, steps: Stream<B>) => Behavior<B>;
toggle: (
initial: boolean,
turnOn: Stream<any>,
turnOff: Stream<any>
) => Behavior<boolean>;
when: (b: Behavior<boolean>) => Future<{}>;
switcher: <A>(init: Behavior<A>, stream: Stream<Behavior<A>>) => Behavior<A>;
freezeAt: <A>(
behavior: Behavior<A>,
shouldFreeze: Future<any>
) => Behavior<A>;
shift: <A>(s: Stream<Stream<A>>) => Stream<A>;
integrate: (curve: Behavior<number>) => Behavior<number>;
};

export function instant<A>(fn: (start: InstantStart) => A): Now<A> {
return flash((run) =>
fn({
run,
sample: (b) => run(sample(b)),
accum: (f, i, s) => run(accum(f, i, s)),
accumCombine: (f, i) => run(accumCombine(f, i)),
stepper: (i, s) => run(stepper(i, s)),
toggle: (i, on, off) => run(toggle(i, on, off)),
when: (b) => run(when(b)),
switcher: (i, s) => run(switcher(i, s)),
freezeAt: (b, s) => run(freezeAt(b, s)),
scan: (f, i, s) => run(scan(f, i, s)),
shift: (s) => run(shift(s)),
integrate: (b) => run(integrate(b))
})
);
}
15 changes: 15 additions & 0 deletions test/now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,19 @@ describe("Now", () => {
runNow(now);
});
});
describe("instant", () => {
it("simpel", () => {
const s = sinkStream<number>();
s.push(1);
s.push(2);
const now = H.instant((start) => {
const sum = start.accum((a, b) => a + b, 0, s);
return sum;
});
const res = runNow(now);
s.push(4);
s.push(5);
assert.strictEqual(res.at(), 9);
});
});
});

0 comments on commit 3d4f746

Please sign in to comment.