Beispiel #1
0
        public Preset(DiscreteCell <int> presetDollars, Fill fi, DiscreteCell <Maybe <Fuel> > fuelFlowing)
        {
            DiscreteCell <Speed> speed = presetDollars.Lift(
                fi.Price, fi.DollarsDelivered, fi.LitersDelivered,
                (presetDollarsLocal, price, dollarsDelivered, litersDelivered) =>
            {
                if (presetDollarsLocal == 0)
                {
                    return(Speed.Fast);
                }

                if (dollarsDelivered >= presetDollarsLocal)
                {
                    return(Speed.Stopped);
                }

                double slowLiters = presetDollarsLocal / price - 0.10;
                return(litersDelivered >= slowLiters ? Speed.Slow : Speed.Fast);
            });

            this.Delivery = fuelFlowing.Lift(speed,
                                             (m, speedLocal) =>
                                             speedLocal == Speed.Fast ? (
                                                 m.Equals(Maybe.Some(Fuel.One)) ? PetrolPump.Delivery.Fast1 :
                                                 m.Equals(Maybe.Some(Fuel.Two)) ? PetrolPump.Delivery.Fast2 :
                                                 m.Equals(Maybe.Some(Fuel.Three)) ? PetrolPump.Delivery.Fast3 : PetrolPump.Delivery.Off
                                                 ) :
                                             speedLocal == Speed.Slow ? (
                                                 m.Equals(Maybe.Some(Fuel.One)) ? PetrolPump.Delivery.Slow1 :
                                                 m.Equals(Maybe.Some(Fuel.Two)) ? PetrolPump.Delivery.Slow2 :
                                                 m.Equals(Maybe.Some(Fuel.Three)) ? PetrolPump.Delivery.Slow3 : PetrolPump.Delivery.Off
                                                 ) : PetrolPump.Delivery.Off);
            this.KeypadActive = fuelFlowing.Lift(speed, (m, speedLocal) => m.Match(_ => speedLocal == Speed.Fast, () => true));
        }
Beispiel #2
0
        public static DiscreteCell <string> PriceLcd(
            DiscreteCell <IMaybe <Fuel> > fillActive,
            DiscreteCell <double> fillPrice,
            Fuel fuel,
            Inputs inputs)
        {
            DiscreteCell <double> idlePrice;

            switch (fuel)
            {
            case Fuel.One:
                idlePrice = inputs.Price1;
                break;

            case Fuel.Two:
                idlePrice = inputs.Price2;
                break;

            case Fuel.Three:
                idlePrice = inputs.Price3;
                break;

            default:
                idlePrice = null;
                break;
            }
            return(fillActive.Lift(fillPrice, idlePrice,
                                   (fuelSelectedMaybe, fillPriceLocal, idlePriceLocal) =>
                                   fuelSelectedMaybe.Match(f => f == fuel ? Formatters.FormatPrice(fillPriceLocal, 4) : string.Empty, () => Formatters.FormatPrice(idlePriceLocal, 4))));
        }
Beispiel #3
0
        public MainWindow()
        {
            this.InitializeComponent();

            Transaction.RunVoid(() =>
            {
                STextBox word = new STextBox(string.Empty);
                DiscreteCellLoop <bool> enabled = new DiscreteCellLoop <bool>();
                SButton button = new SButton(enabled)
                {
                    Content = "look up"
                };
                Stream <string> sWord = button.SClicked.Snapshot(word.Text);
                IsBusy <string, Maybe <string> > ib = new IsBusy <string, Maybe <string> >(Lookup, sWord);
                Stream <string> sDefinition         = ib.SOut.Map(o => o.Match(v => v, () => "ERROR!"));
                DiscreteCell <string> definition    = sDefinition.Hold(string.Empty);
                DiscreteCell <string> output        = definition.Lift(ib.Busy, (def, bsy) => bsy ? "Looking up..." : def);
                enabled.Loop(ib.Busy.Map(b => !b));
                STextBox outputArea = new STextBox(output.Values, string.Empty, enabled)
                {
                    TextWrapping = TextWrapping.Wrap, AcceptsReturn = true
                };
                this.TextBoxPlaceholder.Child = word;
                this.ButtonPlaceholder.Child  = button;
                this.OutputPlaceholder.Child  = outputArea;
            });
        }
Beispiel #4
0
 public FrFlow(Orientation dir, IEnumerable <Fridget> fridgets)
     : base((size, sMouse, sKey, focus, idSupply) =>
 {
     DiscreteCell <Size> desiredSize          = DiscreteCell.Constant(new Size(0, 0));
     DiscreteCell <DrawableDelegate> drawable = DiscreteCell.Constant(new DrawableDelegate(d => { }));
     Stream <long> sChangeFocus = Stream.Never <long>();
     foreach (Fridget fridget in fridgets)
     {
         DiscreteCellLoop <Maybe <Size> > childSz = new DiscreteCellLoop <Maybe <Size> >();
         Output fo = new FrTranslate(fridget,
                                     dir == Orientation.Horizontal
                     ? desiredSize.Map(dsz => new Point(dsz.Width, 0))
                     : desiredSize.Map(dsz => new Point(0, dsz.Height)))
                     .Reify(childSz, sMouse, sKey, focus,
                            idSupply.Child1());
         idSupply = idSupply.Child2();
         childSz.Loop(
             size.Lift(fo.DesiredSize, (mSize, foDsz) =>
                       mSize.Map(s => dir == Orientation.Horizontal
                         ? new Size(foDsz.Width, s.Height)
                         : new Size(s.Width, foDsz.Height))));
         Size LiftDesiredSizeHorizontal(Size dsz, Size foDsz) => new Size(dsz.Width + foDsz.Width, dsz.Height > foDsz.Height ? dsz.Height : foDsz.Height);
         Size LiftDesiredSizeVertical(Size dsz, Size foDsz) => new Size(dsz.Width > foDsz.Width ? dsz.Width : foDsz.Width, dsz.Height + foDsz.Height);
         desiredSize  = desiredSize.Lift(fo.DesiredSize, dir == Orientation.Horizontal ? LiftDesiredSizeHorizontal : (Func <Size, Size, Size>)LiftDesiredSizeVertical);
         drawable     = drawable.Lift(fo.Drawable, (drA, drB) => drA.Append(drB));
         sChangeFocus = sChangeFocus.OrElse(fo.SChangeFocus);
     }
     return(new Output(drawable, desiredSize, sChangeFocus));
 })
 {
 }
Beispiel #5
0
                public TestObject()
                {
                    this.Input1 = new DiscreteCellStreamSink <int>();
                    DiscreteCell <int> input1Cell = this.Input1.Hold(3);

                    this.Input2 = new DiscreteCellStreamSink <int>();
                    DiscreteCell <int> input2Cell = this.Input2.Hold(2);

                    this.Output = input1Cell.Lift(input2Cell, (i1, i2) => i1 + i2);
                }
Beispiel #6
0
            private static DiscreteCell <double> PausableClock(Stream <Unit> sPause, Stream <Unit> sResume, DiscreteCell <double> clock)
            {
                DiscreteCell <IMaybe <double> > pauseTime = sPause.Snapshot(clock, (_, t) => Maybe.Just(t)).OrElse(sResume.Map(_ => Maybe.Nothing <double>())).Hold(Maybe.Nothing <double>());
                DiscreteCell <double>           lostTime  = sResume.Accum(0.0, (_, total) =>
                {
                    double tPause = pauseTime.Cell.Sample().Match(v => v, () => 0);
                    double now    = clock.Cell.Sample();
                    return(total + (now - tPause));
                });

                return(pauseTime.Lift(clock, lostTime, (otPause, tClk, tLost) => otPause.Match(v => v, () => tClk) - tLost));
            }
Beispiel #7
0
        public void TestLiftGlitch()
        {
            DiscreteCellSink <int> c1   = DiscreteCell.CreateSink(1);
            DiscreteCell <int>     c3   = c1.Map(x => x * 3);
            DiscreteCell <int>     c5   = c1.Map(x => x * 5);
            DiscreteCell <string>  c    = c3.Lift(c5, (x, y) => x + " " + y);
            List <string>          @out = new List <string>();
            IListener l = c.Listen(@out.Add);

            c1.Send(2);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out);
        }
Beispiel #8
0
 private FrButton(DiscreteCell <string> label, StreamLoop <Unit> sClicked)
     : base((size, sMouse, sKey, focus, idSupply) =>
 {
     Stream <Unit> sPressed = sMouse.Snapshot(size,
                                              (e, mSize) => mSize.Match(
                                                  s =>
     {
         MouseButtonEventArgs b = e.Args as MouseButtonEventArgs;
         Point p = e.GetPosition();
         return(b != null && b.ChangedButton == MouseButton.Left && b.ButtonState == MouseButtonState.Pressed &&
                p.X >= 2 && p.X < s.Width - 2 && p.Y >= 2 && p.Y < s.Height - 2
                         ? Maybe.Just(Unit.Value)
                         : Maybe.Nothing <Unit>());
     },
                                                  Maybe.Nothing <Unit>)).FilterMaybe();
     Stream <Unit> sReleased = sMouse.Snapshot(size,
                                               (e, mSize) => mSize.Match(
                                                   s =>
     {
         MouseButtonEventArgs b = e.Args as MouseButtonEventArgs;
         return(b != null && b.ChangedButton == MouseButton.Left && b.ButtonState == MouseButtonState.Released
                         ? Maybe.Just(Unit.Value)
                         : Maybe.Nothing <Unit>());
     },
                                                   Maybe.Nothing <Unit>)).FilterMaybe();
     DiscreteCell <bool> pressed = sPressed.MapTo(true).OrElse(sReleased.MapTo(false)).Hold(false);
     sClicked.Loop(sReleased.Gate(pressed));
     Typeface typeface = new Typeface(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
     DiscreteCell <Size> desiredSize = label.Map(l =>
     {
         Size labelSize = FontUtilities.MeasureString(l, typeface, 13);
         return(new Size(labelSize.Width + 14, labelSize.Height + 10));
     });
     return(new Output(
                label.Lift(
                    size, pressed,
                    (l, mSize, p) => new DrawableDelegate(d =>
     {
         mSize.Match(sz =>
         {
             d.DrawRectangle(p ? Brushes.DarkGray : Brushes.LightGray, new Pen(Brushes.Black, 1), new Rect(new Point(2, 2), new Size(sz.Width - 5, sz.Height - 5)));
             FormattedText t = FontUtilities.GetStandardFormattedText(l, typeface, 13, Brushes.Black);
             d.DrawText(t, new Point((sz.Width - t.Width) / 2, (sz.Height - t.Height) / 2));
         }, () => { });
     })),
                desiredSize,
                Stream.Never <long>()));
 })
 {
     this.SClicked = sClicked;
 }
Beispiel #9
0
        public async Task TestListenAsync()
        {
            DiscreteCellSink <int> a  = DiscreteCell.CreateSink(1);
            DiscreteCell <int>     a1 = a.Map(x => x + 1);
            DiscreteCell <int>     a2 = a.Map(x => x * 2);
            ValueTuple <List <int>, DiscreteCellLoop <int>, IListener> resultsAndCalled = Transaction.Run(() =>
            {
                DiscreteCell <int> result         = a1.Lift(a2, (x, y) => x + y);
                Stream <Unit> incrementStream     = Operational.Value(result.Cell).MapTo(Unit.Value);
                StreamSink <Unit> decrementStream = Stream.CreateSink <Unit>();
                DiscreteCellLoop <int> calledLoop = DiscreteCell.CreateLoop <int>();
                calledLoop.Loop(incrementStream.MapTo(1).Merge(decrementStream.MapTo(-1), (x, y) => x + y).Snapshot(calledLoop.Cell, (u, c) => c + u).Hold(0));
                List <int> r = new List <int>();
                IListener l  = result.Listen(v =>
                {
                    Task.Run(async() =>
                    {
                        await Task.Delay(900);
                        r.Add(v);
                        decrementStream.Send(Unit.Value);
                    });
                });
                return(ValueTuple.Create(r, calledLoop, l));
            });
            // ReSharper disable once UnusedVariable
            List <int>         results       = resultsAndCalled.Item1;
            DiscreteCell <int> called        = resultsAndCalled.Item2;
            List <int>         calledResults = new List <int>();
            IListener          l2            = called.Listen(calledResults.Add);

            await Task.Delay(500);

            a.Send(2);
            await Task.Delay(500);

            a.Send(3);
            await Task.Delay(2500);

            l2.Unlisten();
            resultsAndCalled.Item3.Unlisten();
        }
Beispiel #10
0
        public MainWindow()
        {
            this.InitializeComponent();

            STextBox txtA = new STextBox("5")
            {
                Width = 100
            };
            STextBox txtB = new STextBox("10")
            {
                Width = 100
            };

            DiscreteCell <int> a   = txtA.Text.Map(ParseInt);
            DiscreteCell <int> b   = txtB.Text.Map(ParseInt);
            DiscreteCell <int> sum = a.Lift(b, (x, y) => x + y);

            SLabel lblSum = new SLabel(sum.Map(i => i.ToString()));

            this.Container.Children.Add(txtA);
            this.Container.Children.Add(txtB);
            this.Container.Children.Add(lblSum);
        }
Beispiel #11
0
 public DiscreteCell <bool> Reify(DiscreteCell <DateTime> dep, DiscreteCell <DateTime> ret) => dep.Lift(ret, this.f);