Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Created June 1, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clinuxrulz/506b62b45ede646252f6784b09e7bfe3 to your computer and use it in GitHub Desktop.
Save clinuxrulz/506b62b45ede646252f6784b09e7bfe3 to your computer and use it in GitHub Desktop.
Incremental Cell
public class DuelCell<A,DA> {
private final A initA;
private final Stream<DA> sda;
private final BiFunction<A,DA,A> patchA;
private final Cell<A> ca;
public DuelCell(
A initA,
Stream<DA> sda,
BiFunction<A,DA,A> patchA
) {
this.initA = initA;
this.sda = sda;
this.patchA = patchA;
this.ca = sda.accum(initA, (DA da, A a) -> patchA.apply(a, da));
}
public <B,DB> DuelCell<B,DB> incMap(BiFunction<B,DB,B> patchB, Function<A,B> nonIncrementalFn, BiFunction<Tuple2<A,DA>,B,DB> incrementalFn) {
B initB = nonIncrementalFn.apply(ca.sample());
StreamLoop<Tuple2<B,DB>> slDuelB = new StreamLoop<>();
Cell<B> cb = slDuelB.map((Tuple2<B,DB> duelB) -> patchB.apply(duelB.a, duelB.b)).hold(initB);
DuelCell<B,DB> dcb = new DuelCell<>(
initB,
sda.snapshot(ca, (DA da, A a) -> new Tuple2<>(a, da)).snapshot(cb, incrementalFn::apply),
patchB
);
slDuelB.loop(dcb.sda.snapshot(dcb.ca, (DB db, B b) -> new Tuple2<>(b, db)));
return dcb;
}
public Cell<A> cell() {
return ca;
}
public Stream<DA> deltaStream() {
return sda;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment