Esempio n. 1
0
 ///
 ///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);
     }));
 }
Esempio n. 2
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);
     }));
 }
Esempio n. 3
0
 ///
 ///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;
     }));
 }
Esempio n. 4
0
        public void Loop(Event <TA> eventOut)
        {
            if (this.eaOut != null)
            {
                throw new Exception("EventLoop looped more than once");
            }
            this.eaOut = eventOut;
            EventLoop <TA> me = this;

            AddCleanup(
                eventOut.Listen_(
                    Node,
                    new TransactionHandler <TA>
            {
                Run = (trans, a) => me.Send(trans, a)
            }));
        }