public NotifyPointOfSale( LifeCycle lc, Stream <Unit> sClearSale, Fill fi) { DiscreteCell <bool> locked = lc.SStart.Map(u => true).OrElse(sClearSale.Map(u => false)).Hold(false); this.SStart = lc.SStart.Gate(locked.Map(l => !l)); this.SEnd = lc.SEnd.Gate(locked); this.FuelFlowing = this.SStart.Map(Maybe.Just).OrElse( this.SEnd.Map(f => Maybe.Nothing <Fuel>())).Hold(Maybe.Nothing <Fuel>()); this.FillActive = this.SStart.Map(Maybe.Just).OrElse( sClearSale.Map(f => Maybe.Nothing <Fuel>())).Hold(Maybe.Nothing <Fuel>()); this.SBeep = sClearSale; this.SSaleComplete = this.SEnd.Snapshot( this.FuelFlowing.Lift( fi.Price, fi.DollarsDelivered, fi.LitersDelivered, (oFuel, price, dollars, liters) => oFuel.Match(v => Maybe.Just( new Sale(v, price, dollars, liters)), Maybe.Nothing <Sale>))).FilterMaybe(); }
public TestObject(Stream <bool> s, Stream <int> s1, Stream <int> s2) { (DiscreteCell <int> cell, IStrongListener l) = Transaction.Run(() => { var cellLocal = s.Map(v => v ? 1 : 0).OrElse(s1).OrElse(s2).Hold(0); var cell2 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell3 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell4 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell5 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell6 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell7 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell8 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell9 = s1.Snapshot(cellLocal.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var lLocal = Transaction.Run(() => { this.currentValue = cellLocal.Cell.SampleLazy(); return(cellLocal.Updates.Listen(v => this.CurrentValue = v)); }); return(cellLocal, lLocal); }); this.Cell = cell; this.l = l; }
public Keypad(Stream<Key> sKeypad, Stream<Unit> sClear) { CellLoop<int> value = new CellLoop<int>(); this.Value = value; Stream<int> sKeyUpdate = sKeypad.Snapshot(value, (key, valueLocal) => { if (key == Key.Clear) { return Maybe.Just(0); } int x10 = valueLocal * 10; return x10 >= 1000 ? Maybe.Nothing<int>() : Maybe.Just( key == Key.Zero ? x10 : key == Key.One ? x10 + 1 : key == Key.Two ? x10 + 2 : key == Key.Three ? x10 + 3 : key == Key.Four ? x10 + 4 : key == Key.Five ? x10 + 5 : key == Key.Six ? x10 + 6 : key == Key.Seven ? x10 + 7 : key == Key.Eight ? x10 + 8 : x10 + 9); }).FilterMaybe(); value.Loop(sKeyUpdate.OrElse(sClear.Map(u => 0)).Hold(0)); this.SBeep = sKeyUpdate.Map(_ => Unit.Value); }
public Keypad(Stream <Key> sKeypad, Stream <Unit> sClear) { CellLoop <int> value = new CellLoop <int>(); this.Value = value; Stream <int> sKeyUpdate = sKeypad.Snapshot(value, (key, valueLocal) => { if (key == Key.Clear) { return(Maybe.Some(0)); } int x10 = valueLocal * 10; return(x10 >= 1000 ? Maybe.None : Maybe.Some( key == Key.Zero ? x10 : key == Key.One ? x10 + 1 : key == Key.Two ? x10 + 2 : key == Key.Three ? x10 + 3 : key == Key.Four ? x10 + 4 : key == Key.Five ? x10 + 5 : key == Key.Six ? x10 + 6 : key == Key.Seven ? x10 + 7 : key == Key.Eight ? x10 + 8 : x10 + 9)); }).FilterMaybe(); value.Loop(sKeyUpdate.OrElse(sClear.Map(u => 0)).Hold(0)); this.SBeep = sKeyUpdate.Map(_ => Unit.Value); }
public void TestStringStream_Observed_Mapped_Copy() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); subject.OnNext("1"); subject.OnNext("2"); subject.OnNext("3"); subject.OnNext("4"); subject.OnNext("5"); stream.Read(); stream.Read(); stream.Read(); stream.SetOffset(3); // Add first mapping IStream <string> mappedStream1 = stream.Map(s => "map1_" + s); // Add second mapping IStream <string> mappedStream2 = mappedStream1.Map(s => "map2_" + s); // Add fanout IStream <string> output = mappedStream2.Copy(); // read fanout and check all mappings are called string result = output.Read(); Assert.AreEqual("map2_map1_4", result); }
public TestObject(Stream <bool> s, Stream <int> s1, Stream <int> s2) { var t = Transaction.Run(() => { var cell = s.Map(v => v ? 1 : 0).OrElse(s1).OrElse(s2).Hold(0); var cell2 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell3 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell4 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell5 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell6 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell7 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell8 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var cell9 = s1.Snapshot(cell.Cell, (left, right) => left + right).Filter(v => v > 5).OrElse(s.Snapshot(s1.Hold(0).Lift(s2.Hold(1), (left, right) => left + right).Cell).Map(v => v + 1)).Hold(3); var l = Transaction.Run(() => { this.currentValue = cell.Cell.SampleLazy(); return(cell.Updates.Listen(v => this.CurrentValue = v)); }); return(Tuple.Create(cell, l)); }); this.Cell = t.Item1; this.l = t.Item2; }
public void TestEndOfStream_Observed_Mapped_Delayed() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); int mappingsHit = 0; IStream <string> mappedStream = stream.Map(s => { Debug.WriteLine("mapping " + s); mappingsHit++; return("m" + s); }); Assert.IsFalse(mappedStream.EndOfStream); Assert.AreEqual(null, mappedStream.Read()); Assert.IsFalse(mappedStream.EndOfStream, "Stream should not be end or stream yet"); subject.OnNext("1"); subject.OnNext("2"); subject.OnNext("3"); subject.OnCompleted(); Assert.AreEqual("m1", mappedStream.Read()); Assert.AreEqual("m2", mappedStream.Read()); Assert.AreEqual("m3", mappedStream.Read()); Assert.IsNull(mappedStream.Read()); Assert.IsTrue(mappedStream.EndOfStream); }
private static Stream <DateTime> Periodic(ITimerSystem <DateTime> sys, TimeSpan period) { Cell <DateTime> time = sys.Time; DiscreteCellLoop <Maybe <DateTime> > oAlarm = new DiscreteCellLoop <Maybe <DateTime> >(); Stream <DateTime> sAlarm = sys.At(oAlarm); oAlarm.Loop(sAlarm.Map(t => Maybe.Some(t + period)).Hold(Maybe.Some(time.Sample() + period))); return(sAlarm); }
public void Test_MapS_TestCase() { Tuple <Stream <int>, Dictionary <int, Action> > st = MkStream(new Dictionary <int, int> { { 0, 5 }, { 1, 10 }, { 2, 12 } }); Stream <int> s = st.Item1; Dictionary <int, Action> sf = st.Item2; List <int> @out = RunSimulation <int>(s.Map(x => x + 1).Listen, new[] { sf }); CollectionAssert.AreEqual(new[] { 6, 11, 13 }, @out); }
public SComboBox(Stream <IMaybe <T> > setSelectedItem, IMaybe <T> initSelectedItem, IEnumerable <T> items) { Action <IMaybe <T> > setSelectedItemImpl = m => base.SelectedItem = m.Match <object>(v => v, () => null); this.ItemsSource = items ?? new T[0]; setSelectedItemImpl(initSelectedItem); List <IListener> listeners = new List <IListener>(); StreamSink <int> sDecrement = new StreamSink <int>(); Cell <bool> allow = setSelectedItem.Map(_ => 1).OrElse(sDecrement).Accum(0, (b, d) => b + d).Map(b => b == 0); Func <IMaybe <T> > getSelectedItem = () => { object sel = base.SelectedItem; return(sel == null?Maybe.Nothing <T>() : Maybe.Just((T)sel)); }; StreamSink <IMaybe <T> > sUserSelectedItem = new StreamSink <IMaybe <T> >(); this.SUserSelectedItem = sUserSelectedItem; this.SelectedItem = sUserSelectedItem.Gate(allow).OrElse(setSelectedItem).Hold(initSelectedItem); SelectionChangedEventHandler selectionChangedEventHandler = (sender, args) => { IMaybe <T> selectedItem = getSelectedItem(); this.Dispatcher.InvokeAsync(() => sUserSelectedItem.Send(selectedItem)); }; this.SelectionChanged += selectionChangedEventHandler; listeners.Add(setSelectedItem.Listen(m => { this.Dispatcher.InvokeAsync(() => { this.SelectionChanged -= selectionChangedEventHandler; setSelectedItemImpl(m); this.SelectionChanged += selectionChangedEventHandler; sDecrement.Send(-1); }); })); this.disposeListeners = () => { foreach (IListener l in listeners) { using (l) { } } }; }
public SComboBox(Stream <Maybe <T> > setSelectedItem, Maybe <T> initSelectedItem, IEnumerable <T> items) { void SetSelectedItemImpl(Maybe <T> m) => base.SelectedItem = m.Match <object>(v => v, () => null); this.ItemsSource = items ?? new T[0]; SetSelectedItemImpl(initSelectedItem); List <IListener> listeners = new List <IListener>(); StreamSink <int> sDecrement = Stream.CreateSink <int>(); Cell <bool> allow = setSelectedItem.Map(_ => 1).OrElse(sDecrement).Accum(0, (b, d) => b + d).Map(b => b == 0); Maybe <T> GetSelectedItem() { object sel = base.SelectedItem; return(sel == null ? Maybe.None : Maybe.Some((T)sel)); } StreamSink <Maybe <T> > sUserSelectedItem = Stream.CreateSink <Maybe <T> >(); this.SUserSelectedItem = sUserSelectedItem; this.SelectedItem = sUserSelectedItem.Gate(allow).OrElse(setSelectedItem).Hold(initSelectedItem); void SelectionChangedEventHandler(object sender, SelectionChangedEventArgs args) { Maybe <T> selectedItem = GetSelectedItem(); this.Dispatcher.InvokeAsync(() => sUserSelectedItem.Send(selectedItem)); } this.SelectionChanged += SelectionChangedEventHandler; listeners.Add(setSelectedItem.Listen(m => { this.Dispatcher.InvokeAsync(() => { this.SelectionChanged -= SelectionChangedEventHandler; SetSelectedItemImpl(m); this.SelectionChanged += SelectionChangedEventHandler; sDecrement.Send(-1); }); })); this.disposeListeners = () => { foreach (IListener l in listeners) { l.Unlisten(); } }; }
public static Cell<double> Accumulate( Stream<Unit> sClearAccumulator, Stream<int> sPulses, Cell<double> calibration) { CellLoop<int> total = new CellLoop<int>(); total.Loop(sClearAccumulator.Map(u => 0) .OrElse(sPulses.Snapshot(total, (pulsesLocal, totalLocal) => pulsesLocal + totalLocal)) .Hold(0)); return total.Lift( calibration, (totalLocal, calibrationLocal) => totalLocal * calibrationLocal); }
private FrButton(Cell <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(); Cell <bool> pressed = sPressed.Map(_ => true).OrElse(sReleased.Map(_ => false)).Hold(false); sClicked.Loop(sReleased.Gate(pressed)); Typeface typeface = new Typeface(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); Cell <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; }
public static DiscreteCell <double> Accumulate( Stream <Unit> sClearAccumulator, Stream <int> sPulses, DiscreteCell <double> calibration) { DiscreteCellLoop <int> total = new DiscreteCellLoop <int>(); total.Loop(sClearAccumulator.Map(u => 0) .OrElse(sPulses.Snapshot(total, (pulsesLocal, totalLocal) => pulsesLocal + totalLocal)) .Hold(0)); return(total.Lift( calibration, (totalLocal, calibrationLocal) => totalLocal * calibrationLocal)); }
public void TestStringStream_Normal_Map_Ideal() { string[] source = { "1", "2", "3" }; Stream <string> stream = new Stream <string>(source); IStream <int[]> mappedStream = stream.Map(s => new int[int.Parse(s)]); Assert.IsTrue(new int[1].SequenceEqual(mappedStream.Read())); Assert.IsTrue(new int[2].SequenceEqual(mappedStream.Read())); Assert.IsTrue(new int[3].SequenceEqual(mappedStream.Read())); Assert.IsNull(mappedStream.Read()); Assert.AreEqual(null, stream.Read()); // non mapped stream is continueing to read }
private static Cell <double> PausableClock(Stream <Unit> sPause, Stream <Unit> sResume, Cell <double> clock) { Cell <Maybe <double> > pauseTime = sPause.Snapshot(clock, (_, t) => Maybe.Some(t)) .OrElse(sResume.Map(_ => Maybe <double> .None)).Hold(Maybe.None); Cell <double> lostTime = sResume.Accum(0.0, (_, total) => { double tPause = pauseTime.Sample().Match(v => v, () => 0); double now = clock.Sample(); return(total + (now - tPause)); }); return(pauseTime.Lift(clock, lostTime, (otPause, tClk, tLost) => otPause.Match(v => v, () => tClk) - tLost)); }
public NotifyPointOfSale( LifeCycle lc, Stream<Unit> sClearSale, Fill fi) { Cell<bool> locked = lc.SStart.Map(u => true).OrElse(sClearSale.Map(u => false)).Hold(false); this.SStart = lc.SStart.Gate(locked.Map(l => !l)); this.SEnd = lc.SEnd.Gate(locked); this.FuelFlowing = this.SStart.Map(Maybe.Just).OrElse( this.SEnd.Map(f => Maybe.Nothing<Fuel>())).Hold(Maybe.Nothing<Fuel>()); this.FillActive = this.SStart.Map(Maybe.Just).OrElse( sClearSale.Map(f => Maybe.Nothing<Fuel>())).Hold(Maybe.Nothing<Fuel>()); this.SBeep = sClearSale; this.SSaleComplete = this.SEnd.Snapshot( this.FuelFlowing.Lift( fi.Price, fi.DollarsDelivered, fi.LitersDelivered, (oFuel, price, dollars, liters) => oFuel.Match(v => Maybe.Just( new Sale(v, price, dollars, liters)), Maybe.Nothing<Sale>))).FilterMaybe(); }
public STextBox(Stream <string> setText, string initText, Cell <bool> enabled) { base.Text = initText; List <IListener> listeners = new List <IListener>(); StreamSink <int> sDecrement = new StreamSink <int>(); Cell <bool> allow = setText.Map(_ => 1).OrElse(sDecrement).Accum(0, (b, d) => b + d).Map(b => b == 0); StreamSink <string> sUserChanges = new StreamSink <string>(); this.SUserChanges = sUserChanges; this.Text = sUserChanges.Gate(allow).OrElse(setText).Hold(initText); TextChangedEventHandler textChangedEventHandler = (sender, args) => { string text = base.Text; this.Dispatcher.InvokeAsync(() => sUserChanges.Send(text)); }; this.TextChanged += textChangedEventHandler; // Set the initial value at the end of the transaction so it works with CellLoops. Transaction.Post(() => this.Dispatcher.InvokeIfNecessary(() => this.IsEnabled = enabled.Sample())); listeners.Add(setText.Listen(t => { this.Dispatcher.InvokeAsync(() => { this.TextChanged -= textChangedEventHandler; base.Text = t; this.TextChanged += textChangedEventHandler; sDecrement.Send(-1); }); })); listeners.Add(Operational.Updates(enabled).Listen(e => this.Dispatcher.InvokeIfNecessary(() => this.IsEnabled = e))); this.disposeListeners = () => { foreach (IListener l in listeners) { using (l) { } } }; }
public void TestStringStream_Fluent_Map_MoreStages() { string[] source = { "1,1", "2,1", "3,1" }; Stream <string> stream = new Stream <string>(source); IStream <string[]> mappedStream1 = stream.Map(s => s.Split(',')); IStream <int[]> mappedStream2 = mappedStream1.Map(s => new int[int.Parse(s[0])]); Assert.IsTrue(new int[1].SequenceEqual(mappedStream2.Read())); Assert.IsTrue(new int[2].SequenceEqual(mappedStream2.Read())); Assert.IsTrue(new int[3].SequenceEqual(mappedStream2.Read())); Assert.IsNull(mappedStream2.Read()); Assert.AreEqual(null, stream.Read()); }
public void TestStringStream_Normal_Foreach() { string[] source = { "1", "2", "3" }; Stream <string> stream = new Stream <string>(source); IStream <int[]> mappedStream = stream.Map <int[]>(s => new int[int.Parse(s)]); int counter = 0; mappedStream.ForEach(v => { Assert.IsNotNull(v); counter++; }); Assert.AreEqual(3, counter); }
public void TestStringStream_Observed_Replay_Mapped_Copy_InterruptedSupply() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); int mappingsHit = 0; IStream <string> mappedStream = stream.Map(s => { Debug.WriteLine("mapping " + s); mappingsHit++; return("m" + s); }); IStream <string> copyStream = mappedStream.Copy(); for (int i = 0; i < 100; i++) { subject.OnNext(i.ToString()); } for (int i = 0; i < 50; i++) { Assert.AreEqual("m" + i, copyStream.Read()); } Assert.AreEqual(50, mappingsHit); for (int i = 100; i < 200; i++) { subject.OnNext(i.ToString()); } for (int i = 50; i < 200; i++) { Assert.AreEqual("m" + i, copyStream.Read()); } Assert.AreEqual(200, mappingsHit); Assert.AreEqual(200, copyStream.Count()); }
public void TestStringStream_Observed_Replay_Mapped_Copy() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); IStream <int[]> mappedStream = stream.Map(s => new int[int.Parse(s)]); IStream <int[]> copyStream = mappedStream.Copy(); subject.OnNext("1"); Assert.IsTrue(new int[1].SequenceEqual(copyStream.Read())); subject.OnNext("2"); Assert.IsTrue(new int[2].SequenceEqual(copyStream.Read())); Assert.AreEqual(2, copyStream.Count()); }
public void TestStringStream_Yielding_Mapped_Copy() { string[] source = { "1", "2", "3", "4", "5" }; Stream <string> stream = new Stream <string>(GetDataSourceYielding(source)); stream.Read(); stream.Read(); stream.Read(); stream.SetOffset(3); // Add first mapping IStream <string> mappedStream1 = stream.Map(s => "map1_" + s); // Add second mapping IStream <string> mappedStream2 = mappedStream1.Map(s => "map2_" + s); // Add fanout IStream <string> output = mappedStream2.Copy(); // read fanout and check all mappings are called string result = output.Read(); Assert.AreEqual("map2_map1_4", result); }
public void TestStringStream_Observed_Replay_Mapped() { ReplaySubject <string> subject = new ReplaySubject <string>(); Stream <string> stream = new Stream <string>(subject.AsObservable()); IStream <int[]> mappedStream = stream.Map(s => new int[int.Parse(s)]); subject.OnNext("1"); Debug.WriteLine("> Reading from mapped stream"); Assert.IsTrue(new int[1].SequenceEqual(mappedStream.Read())); Debug.WriteLine("> Reading from base stream"); Assert.AreEqual(null, stream.Read()); subject.OnNext("2"); Debug.WriteLine("> Reading from base stream"); Assert.AreEqual("2", stream.Read()); Debug.WriteLine("> Reading from mapped stream"); Assert.AreEqual(null, mappedStream.Read()); Assert.AreEqual(2, stream.Count()); Assert.AreEqual(2, mappedStream.Count()); }
private FrTextField(string initText, CellLoop <string> text) : base((size, sMouse, sKey, focus, idSupply) => { Stream <double> 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(p.X - 2) : Maybe.Nothing <double>()); }, Maybe.Nothing <double>)).FilterMaybe(); CellLoop <int> x = new CellLoop <int>(); long myId = idSupply.Get(); Cell <bool> haveFocus = focus.Map(fId => fId == myId); Typeface typeface = new Typeface(new FontFamily("Helvetica"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); Stream <TextUpdate> sTextUpdate = sKey.Gate(haveFocus).Snapshot(text, (key, txt) => { int xValue = x.Sample(); if (key is BackspaceKeyEvent) { return(xValue > 0 ? Maybe.Just(new TextUpdate( txt.Substring(0, xValue - 1) + txt.Substring(xValue), xValue - 1)) : Maybe.Nothing <TextUpdate>()); } StringKeyEvent stringKey = key as StringKeyEvent; if (stringKey == null) { throw new InvalidOperationException("Unexpected type encountered for " + typeof(KeyEvent).FullName + ": " + key.GetType().FullName + "."); } string keyString = stringKey.String; return(keyString == "\b" ? Maybe.Nothing <TextUpdate>() : Maybe.Just(new TextUpdate( txt.Substring(0, xValue) + keyString + txt.Substring(xValue), xValue + 1))); }).FilterMaybe(); x.Loop(sPressed.Snapshot(text, (xCoord, txt) => { for (int i = 1; i <= txt.Length; i++) { if (xCoord < FontUtilities.MeasureString(txt.Substring(0, i), typeface, 13).Width) { return(i - 1); } } return(txt.Length); }) .OrElse(sTextUpdate.Map(tu => tu.NewX)) .Hold(0)); text.Loop(sTextUpdate.Map(tu => tu.Txt).Hold(initText)); Cell <Size> desiredSize = text.Map(txt => { Size s = FontUtilities.MeasureString(txt, typeface, 13); return(new Size(s.Width + 14, s.Height + 10)); }); return(new Output( text.Lift( x, haveFocus, size, (txt, xValue, haveFocusValue, mSize) => new DrawableDelegate(d => { mSize.Match( sz => { d.DrawRectangle(Brushes.White, new Pen(Brushes.Black, 1), new Rect(new Point(2, 2), new Size(sz.Width - 5, sz.Height - 5))); FormattedText t = FontUtilities.GetStandardFormattedText(txt, typeface, 13, Brushes.Black); FormattedText tCursor = FontUtilities.GetStandardFormattedText(txt.Substring(0, xValue), typeface, 13, Brushes.Black); d.DrawText(t, new Point(4, (sz.Height - t.Height) / 2)); if (haveFocusValue) { double cursorX = tCursor.Width; d.DrawLine(new Pen(Brushes.Red, 1), new Point(4 + cursorX, 4), new Point(4 + cursorX, sz.Height - 5)); } }, () => { }); })), desiredSize, sPressed.Map(_ => myId))); }) { this.Text = text; }
public IsBusy(Func <Stream <T>, Stream <TResult> > action, Stream <T> sIn) { this.SOut = action(sIn); this.Busy = sIn.Map(_ => true).OrElse(this.SOut.Map(_ => false)).Hold(false); }