Example #1
0
            public void UnfoldFlow_should_unfold_Collatz_conjecture_with_a_sequence_of_111_elements_with_function()
            {
                Option <Tuple <int, int> > Map(int x)
                {
                    if (x == 1)
                    {
                        return(Option <Tuple <int, int> > .None);
                    }

                    if (x % 2 == 0)
                    {
                        return(new Option <Tuple <int, int> >(Tuple.Create(x / 2, x)));
                    }

                    return(new Option <Tuple <int, int> >(Tuple.Create(x * 3 + 1, x)));
                }

                var source = SourceGen.UnfoldFlowWith(27, Flow.FromFunction <int, int>(x => x), Map, _timeout);
                var sink   = source.RunWith(this.SinkProbe <int>(), Sys.Materializer());

                foreach (var output in _outputs)
                {
                    sink.Request(1);
                    sink.ExpectNext(output);
                }
                sink.Request(1);
                sink.ExpectComplete();
            }
Example #2
0
            public void UnfoldFlow_should_unfold_Collatz_conjecture_with_a_sequence_of_111_elements_with_buffered_flow()
            {
                (int, int) Map(int x)
                {
                    if (x == 1)
                    {
                        throw _done;
                    }

                    if (x % 2 == 0)
                    {
                        return(x / 2, x);
                    }

                    return(x * 3 + 1, x);
                };

                Source <int, NotUsed> BufferedSource(int buffSize)
                {
                    return
                        (SourceGen.UnfoldFlow(27,
                                              Flow.FromFunction <int, (int, int)>(Map)
                                              .Recover(ex =>
                    {
                        if (ex == _done)
                        {
                            return new Option <(int, int)>((1, 1));
                        }

                        return Option <(int, int)> .None;
                    }), _timeout)
                         .Buffer(buffSize, OverflowStrategy.Backpressure));
                }

                var sink = BufferedSource(10).RunWith(this.SinkProbe <int>(), Sys.Materializer());

                sink.Request(_outputs.Length);
                foreach (var output in _outputs)
                {
                    sink.ExpectNext(output);
                }

                sink.Request(1);
                sink.ExpectNext(1);
                sink.ExpectComplete();
            }
Example #3
0
            public void UnfoldFlow_should_unfold_Collatz_conjecture_with_a_sequence_of_111_elements_with_flow()
            {
                (int, int) Map(int x)
                {
                    if (x == 1)
                    {
                        throw _done;
                    }

                    if (x % 2 == 0)
                    {
                        return(x / 2, x);
                    }

                    return(x * 3 + 1, x);
                };

                var source = SourceGen.UnfoldFlow(27,
                                                  Flow.FromFunction <int, (int, int)>(Map)
                                                  .Recover(ex =>
                {
                    if (ex == _done)
                    {
                        return(new Option <(int, int)>((1, 1)));
                    }

                    return(Option <(int, int)> .None);
                }),
                                                  _timeout);

                var sink = source.RunWith(this.SinkProbe <int>(), Sys.Materializer());

                foreach (var output in _outputs)
                {
                    sink.Request(1);
                    sink.ExpectNext(output);
                }

                sink.Request(1);
                sink.ExpectNext(1);
                sink.ExpectComplete();
            }
Example #4
0
            public WithSimpleFlow()
            {
                var controlledFlow = Flow.FromSinkAndSource(this.SinkProbe <int>(), this.SourceProbe <(int, int)>(), Keep.Both);

                _source = SourceGen.UnfoldFlow(1, controlledFlow, _timeout);
            }
Example #5
0
            public WithFunction()
            {
                var controlledFlow = Flow.FromSinkAndSource(this.SinkProbe <int>(), this.SourceProbe <int>(), Keep.Both);

                _source = SourceGen.UnfoldFlowWith(1, controlledFlow, n => new Option <Tuple <int, int> >(Tuple.Create(n + 1, n)), _timeout);
            }