/// ///Accumulate on input event, outputting the new state each time. /// public Behavior <TS> Accum <TS>(TS initState, Func <TA, TS, TS> f) { return(Transaction.Run(() => { Event <TA> ea = this; var es = new EventLoop <TS>(); Behavior <TS> s = es.Hold(initState); Event <TS> esOut = ea.Snapshot(s, f); es.Loop(esOut); return esOut.Hold(initState); })); }
/// ///Transform an event with a generalized state loop (a mealy machine). The function ///is passed the input and the old state and returns the new state and output value. /// public Event <TB> Collect <TB, TS>(TS initState, Func <TA, TS, Tuple <TB, TS> > f) { return(Transaction.Run(() => { Event <TA> ea = this; var es = new EventLoop <TS>(); Behavior <TS> s = es.Hold(initState); Event <Tuple <TB, TS> > ebs = ea.Snapshot(s, f); Event <TB> eb = ebs.Map(bs => bs.Item1); Event <TS> esOut = ebs.Map(bs => bs.Item2); es.Loop(esOut); return eb; })); }
/// ///Transform a behavior with a generalized state loop (a mealy machine). The function ///is passed the input and the old state and returns the new state and output value. /// public Behavior <TB> Collect <TB, TS>(TS initState, Func <TA, TS, Tuple <TB, TS> > f) { return(Transaction.Run(() => { Event <TA> ea = Updates().Coalesce((fst, snd) => snd); Func <Tuple <TB, TS> > zbs = () => f(SampleNoTrans(), initState); var ebs = new EventLoop <Tuple <TB, TS> >(); Behavior <Tuple <TB, TS> > bbs = ebs.HoldLazy(zbs); Behavior <TS> bs = bbs.Map((x => x.Item2)); Event <Tuple <TB, TS> > ebsOut = ea.Snapshot(bs, f); ebs.Loop(ebsOut); return bbs.Map(x => x.Item1); })); }