Esempio n. 1
0
 ///
 ///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);
     }));
 }
 /// <summary>
 ///     Unwrap a cell inside a behavior to give a time-varying cell implementation.
 /// </summary>
 /// <typeparam name="T">The type of the cell.</typeparam>
 /// <param name="bca">The behavior containing a cell.</param>
 /// <returns>The unwrapped cell.</returns>
 public static Cell <T> SwitchC <T>(this Behavior <Cell <T> > bca) =>
 new Cell <T>(bca.Map(c => c.AsBehavior()).SwitchC());