Exemplo n.º 1
0
        public void Execute()
        {
            //
            // Zip拡張メソッド.
            //
            // Zip拡張メソッドは、Pythonのzip関数と同じ動きをするものである。
            // つまり、2つのシーケンスを同時にループさせることが出来る。
            //
            // 第二引数には、resultSelectorを指定する必要があり、好きなデータを返す事ができる。
            //
            // このメソッドは、どちらかのシーケンスが終わるまで処理を続けるという仕様になっているので
            // 2つのシーケンスの要素数が異なる場合は、注意が必要である。
            //
            // つまり、片方のシーケンスが空の場合、このメソッドは一度もループされない。
            //
            IEnumerable<int> numbers1 = new[] {1, 2, 3, 4, 5};
            IEnumerable<int> numbers2 = new[] {6, 7, 8, 9, 0};

            var query = numbers1.Zip(numbers2, Tuple.Create);

            Output.WriteLine("========= 2つのシーケンスの要素数が同じ場合 ===========");
            foreach (var item in query)
            {
                Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
            }

            numbers1 = new[] {1, 2, 3};
            numbers2 = new[] {6, 7, 8, 9, 0};

            query = numbers1.Zip(numbers2, Tuple.Create);

            Output.WriteLine("========= 1つ目のシーケンスの要素が2つ目よりも少ない場合 ===========");
            foreach (var item in query)
            {
                Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
            }

            numbers1 = new[] {1, 2, 3, 4, 5};
            numbers2 = new[] {6, 7, 8};

            query = numbers1.Zip(numbers2, Tuple.Create);

            Output.WriteLine("========= 2つ目のシーケンスの要素が1つ目よりも少ない場合 ===========");
            foreach (var item in query)
            {
                Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
            }

            numbers1 = Enumerable.Empty<int>();
            numbers2 = new[] {6, 7, 8};

            query = numbers1.Zip(numbers2, Tuple.Create);

            Output.WriteLine("========= どちらかのシーケンスが空の場合 ===========");
            foreach (var item in query)
            {
                Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
            }
        }
Exemplo n.º 2
0
        public void CouldMoveOnFirstAndLastPositionOfThreeSeries() {

            var sm1 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    //{ 1, 1},
                    { 2, 2},
                    //{ 3, 3}
                });
            var sm2 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 2},
                    { 2, 4},
                    { 3, 6}
                });
            var sm3 = new SortedMap<int, int>(new Dictionary<int, int>()
                {
                    { 1, 3},
                    { 2, 6},
                    { 3, 9}
                });

            var series = new[] { sm1, sm2, sm3 };
            var sum = series.Zip((k, varr) => k * varr.Sum());

            var zipNCursor = sum.GetCursor();
            var movedFirst = zipNCursor.MoveFirst();
            Assert.IsTrue(movedFirst);
            Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue);
            var movedLast = zipNCursor.MoveLast();
            Assert.IsTrue(movedLast);
            Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue);
        }
Exemplo n.º 3
0
 //private static void Main(string[] args) {
 //    new ProblemB().Solve(Console.In);
 //}
 private void Solve(TextReader input)
 {
     var ns = input.ReadLine().Split(' ').ToArray();
     var ns2 = new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
     var ns3 = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     var count = int.Parse(input.ReadLine());
     var vs = new List<string>();
     for (int i = 0; i < count; i++) {
         vs.Add(input.ReadLine().Trim());
     }
     var answer = vs.Select(
             v => {
                 var result = v;
                 foreach (var t in ns.Zip(ns2, Tuple.Create)) {
                     result = result.Replace(t.Item1, t.Item2);
                 }
                 foreach (var t in ns2.Zip(ns3, Tuple.Create)) {
                     result = result.Replace(t.Item1, t.Item2);
                 }
                 return Tuple.Create(v, result);
             }).Select(t => Tuple.Create(t.Item1, int.Parse(t.Item2))).OrderBy(t => t.Item2);
     foreach (var t in answer) {
         Console.WriteLine(t.Item1);
     }
 }
Exemplo n.º 4
0
 public void AdjacentElements()
 {
     IEnumerableWithCount<string> elements = new[] { "a", "b", "c", "d", "e" }.AsEnumerableWithCount();
     IEnumerableWithCount<string> query = elements.Zip(elements.Skip(1), (x, y) => x + y);
     Assert.AreEqual(4, query.Count);
     query.AssertSequenceEqual("ab", "bc", "cd", "de");
 }
Exemplo n.º 5
0
		public void SecondIsInfinite()
		{
			var arr1 = new[] { 1, 2 };

			var result = arr1.Zip(Infinite(), Tuple.Create);

			CollectionAssert.AreEqual(new[] { Tuple.Create(1, 42), Tuple.Create(2, 42) }, result);
		}
Exemplo n.º 6
0
            public void Zip_two_equal_collections()
            {
                var ints = new[] { 1, 2, 3 };
                var strings = new[] { "a", "b", "c" };

                var zipped = ints.Zip(strings, (i, s) => i + s);
                Assert.That(zipped, Is.EqualTo(new[] { "1a", "2b", "3c" }));
            }
Exemplo n.º 7
0
    public static void Main () {
        var ints1 = new[] { 1, 2, 3, 4 };
        var ints2 = new List<int> { 5, 6, 7 };

        foreach (var prod in ints1.Zip(ints2, (l, r) => l * r ))
            Console.WriteLine(prod);

    }
Exemplo n.º 8
0
 public void EqualLengthSequences()
 {
     IEnumerableWithCount<string> first = new[] { "a", "b", "c" }.AsEnumerableWithCount();
     IEnumerableWithCount<int> second = EnumerableWithCount.Range(5, 3);
     Func<string, int, string> resultSelector = (x, y) => x + ":" + y;
     IEnumerableWithCount<string> query = first.Zip(second, resultSelector);
     query.AssertSequenceEqual("a:5", "b:6", "c:7");
 }
Exemplo n.º 9
0
		public void EqualSizeArrays()
		{
			var arr1 = new[] { 1 };
			var arr2 = new[] { 2 };

			var result = arr1.Zip(arr2, Tuple.Create);

			Assert.AreEqual(new[] { Tuple.Create(1, 2) }, result);
		}
Exemplo n.º 10
0
		public void SecondIsEmpty()
		{
			var arr1 = new[] { 1, 2 };
			var arr2 = new int[0];

			var result = arr1.Zip(arr2, Tuple.Create);

			CollectionAssert.IsEmpty(result);
		}
        public void ShouldZipEvenNumberedCollection()
        {
            var data = new[] {1L, 2L, 3L, 4L};
            var results = new[] {3L, 7L};
            Func<long,long,long> map = (a, b) => a + b;

            var zipped = data.Zip(map);
            Assert.True(results.SequenceEqual(zipped));
        }
Exemplo n.º 12
0
        public static string[] GetLines(this string @this)
        {
            int[] linesIndex = @this.IndexOfAll("\n");

            IEnumerable<int> linesStartIndex = new [] { 0 }.Concat(linesIndex.Select(index => index + 1));
            IEnumerable<int> linesEndIndex = linesIndex.Concat(new[] { @this.Length });

            return linesStartIndex.Zip(linesEndIndex, (lineStartIndex, lineEndIndex) => @this.Substring(lineStartIndex, lineEndIndex - lineStartIndex).TrimEnd('\r')).ToArray();
        }
Exemplo n.º 13
0
        public void SplitLinesInBufferTest()
        {
            string input = "Hello World\nTest\n\nOther Stuff\n\n";
            string[] expected = new[] { "Hello World", "Test", "Other Stuff" };

            var output = ActivityRecord.splitLinesInBuffer(input).ToArray();

            Assert.Equal(expected.Length, output.Length);
            expected.Zip(output, (e, a) => new { e, a }).Run(x => Assert.Equal(x.e, x.a));
        }
Exemplo n.º 14
0
        public void PhoneNumberTest()
        {
            var target = createFixture();
            var tests = new[] { "555.555.1234", null, "Elephant Robot", "333.3333.3333", "444.444.2222" };
            var results = new[] { true, false, false, false, true };

            foreach(var testcase in tests.Zip(results, (test, result) => new {test, result})) {
                target.PhoneNumber = testcase.test;
                Assert.AreEqual(testcase.result, target.IsValid(), "Testcase: " + (testcase.test ?? "(null)"));
            }
        }
        public void WhenAnyWithDependencyObjectTest()
        {
            var inputs = new[] {"Foo", "Bar", "Baz"};
            var fixture = new DepObjFixture();

            var outputs = fixture.WhenAnyValue(x => x.TestString).CreateCollection();
            inputs.ForEach(x => fixture.TestString = x);

            Assert.Null(outputs.First());
            Assert.Equal(4, outputs.Count);
            Assert.True(inputs.Zip(outputs.Skip(1), (expected, actual) => expected == actual).All(x => x));
        }
Exemplo n.º 16
0
        public bool Show(Color inputColor, TimeSpan visibleTime)
        {
            var timer = ObservableExt.TimerMaxTick(1, TimeSpan.Zero, visibleTime);

            var color = ProcessColor(inputColor);

            var colors = new[] {color, Color.Black}.ToObservable();

            colors.Zip(timer, (c, t) => new {Color = c, Count = t})
                .Subscribe(item => commandBus.SendCommand(new SetColorCommand(item.Color)));

            return true;
        }
Exemplo n.º 17
0
        public void TestReverse()
        {
            var original = new[] {1.0, 1.4, 1.6, 2.0};
            var expected = original.Reverse();
            var actual = Utility.Reverse(original);
            Assert.IsTrue(original.Zip(actual, (first, second) => Math.Abs(first - second) > 0.001).All(eq => eq));
            Assert.IsTrue(expected.Zip(actual, (first, second) => Math.Abs(first - second) < 0.001).All(eq => eq));

            var original2 = new[] {1, 2, 3, 4};
            var expected2 = original2.Reverse();
            var actual2 = Utility.Reverse(original2);
            Assert.IsTrue(original2.Zip(actual2, (first, second) => first != second).All(eq => eq));
            Assert.IsTrue(expected2.Zip(actual2, (first, second) => first == second).All(eq => eq));
        }
Exemplo n.º 18
0
        public void Should_zip_correctly()
        {
            // Arrange
            var letters = new[] { "a", "b", "c" };
            var numbers = new[] { 1, 2 };

            // Act
            var zipped = letters.Zip(numbers, (f, s) => new { Letter = f, Number = s }).ToList();

            // Assert
            Assert.That(zipped, Has.Count.EqualTo(2));
            Assert.That(zipped[0], Has.Property("Letter").EqualTo("a").And.Property("Number").EqualTo(1));
            Assert.That(zipped[1], Has.Property("Letter").EqualTo("b").And.Property("Number").EqualTo(2));
        }
        public void ToChainTest()
        {
            var v1 = new[] { 42 };
            var v2 = new[] { 42, 43 };
            var v3 = new[] { 42, 43, 44 };

            var o1 = v1.ToChain();
            Assert.AreEqual(v1.Length, o1.Count());
            Assert.IsTrue(v1.Zip(o1, (a, b) => a == b).All(p => p));

            var o2 = v2.ToChain();
            Assert.AreEqual(v2.Length, o2.Count());
            Assert.IsTrue(v2.Zip(o2, (a, b) => a == b).All(p => p));

            var o3 = v3.ToChain();
            Assert.AreEqual(v3.Length, o3.Count());
            Assert.IsTrue(v3.Zip(o3, (a, b) => a == b).All(p => p));
        }
Exemplo n.º 20
0
        public IVariable Set(IMilpManager milpManager, CompositeConstraintType type, ICompositeConstraintParameters parameters,
            IVariable leftVariable, params IVariable[] rightVariable)
        {
            var one = milpManager.FromConstant(1);
            var maximumIntegerValue = milpManager.FromConstant(milpManager.MaximumIntegerValue);

            var allVariables = new[] {leftVariable}.Concat(rightVariable).ToArray();
            var boundaryVariables = allVariables.Select(v => milpManager.CreateAnonymous(Domain.BinaryInteger)).ToArray();
            milpManager.Operation(OperationType.Addition, boundaryVariables).Set(ConstraintType.LessOrEqual, one);
            foreach (var pair in allVariables.Zip(boundaryVariables, Tuple.Create))
            {
                pair.Item1
                    .Set(ConstraintType.LessOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue))
                    .Set(ConstraintType.GreaterOrEqual, pair.Item2.Operation(OperationType.Multiplication, maximumIntegerValue).Operation(OperationType.Negation));
            }

            return leftVariable;
        }
        public void ChangedObservableTest()
        {
            var input = new[] { 1, 2, 3 };
            var fixture = new HostTestFixture();

            IList<IObservedChange<HostTestFixture, object>> result = null;

            fixture.Changed()
                .Take(input.Length)
                .ToList()
                .Subscribe(x => result = x);

            foreach (var v in input) { fixture.SomeOtherParam = v; }

            Assert.IsNotNull(result);
            Assert.AreEqual(input.Length, result.Count);
            foreach (var v in input.Zip(result.Select(x => (int)x.Value), (expected, actual) => new { expected, actual })) {
                Assert.AreEqual(v.expected, v.actual);
            }
        }
Exemplo n.º 22
0
        public void MakeSureMemoizedReleaseFuncGetsCalled()
        {
            var input = new[] { 1, 1, 2, 2, 1, 1, 3, 3 };
            var output = new[] { 5, 5, 10, 10, 5, 5, 15, 15 };

            var fixture = new ReactiveAsyncCommand(null, 0);
            var results = new List<Timestamped<int>>();
            var released = new List<int>();

            fixture.RegisterMemoizedFunction<int>(x => { Thread.Sleep(250); return ((int)x) * 5; }, 2, x => released.Add(x))
                   .Timestamp()
                   .DebugObservable()
                   .Subscribe(x => results.Add(x));

            Assert.IsTrue(fixture.CanExecute(1));

            var start = DateTimeOffset.Now;
            foreach(var i in input) {
                Assert.IsTrue(fixture.CanExecute(i));
                fixture.Execute(i);
            }

            Thread.Sleep(1000);

            this.Log().Info("Timestamp Deltas");
            results.Select(x => x.Timestamp - start)
                   .Run(x => this.Log().Info(x));

            this.Log().Info("Release list");
            released.Run(x => this.Log().Info(x));

            output.Zip(results.Select(x => x.Value), (expected, actual) => new { expected, actual })
                  .Run(x => Assert.AreEqual(x.expected, x.actual));

            Assert.IsTrue(results.Count == 8);

            Assert.IsTrue(released.Count == 1);
            Assert.IsTrue(released[0] == 2*5);
        }
Exemplo n.º 23
0
		public void Zip4_Source4Null ()
		{
			IEnumerable<int>  s  = new[]{1};
			IEnumerable<int>  v1 = new[]{1};
			IEnumerable<int>  v2 = new[]{1};
			IEnumerable<int>  v3 = null;
			s.Zip (v1, v2, v3);
		}
Exemplo n.º 24
0
		public void Zip3_Source3Null ()
		{
			IEnumerable<int>  s  = new[]{1};
			IEnumerable<int>  v1 = new[]{1};
			IEnumerable<int>  v2 = null;
			s.Zip (v1, v2);
		}
Exemplo n.º 25
0
		public void Zip_Source2Null ()
		{
			IEnumerable<int>  s = new[]{1};
			IEnumerable<int>  v = null;
			s.Zip (v);
		}
Exemplo n.º 26
0
        public void DivisionDataFrames()
        {
            var result = df / df2;
            var expected = new[]
                               {
                                    0.1,	0.1,	0.1,
                                    0.1,	0.1,	0.1,
                                    0.1,	0.1,	0.1,
                                    0.1,	0.1,	0.1,
                               };

            Assert.IsTrue(expected.Zip(result, (a, b) => AlmostEqual(a, b, 0.0001)).All(o => object.Equals(o, true)));
        }
Exemplo n.º 27
0
        public void MultiplicationDataFrames()
        {
            var result = df * df2;
            var expected = new[]
                               {
                                    0.1, 2.5, 8.1,
                                    0.4, 3.6, 10.0,
                                    0.9, 4.9, 12.1,
                                    1.6, 6.4, 14.4
                               };

            Assert.IsTrue(expected.Zip(result, (a, b) => AlmostEqual(a, b, 0.0001)).All(o => object.Equals(o, true) ));
        }
Exemplo n.º 28
0
 public void Zip()
 {
     var ints1 = new[] { 1, 2, 3 };
     var ints2 = new[] { 4, 5, 6 };
     var zipped = ints1.Zip(ints2, (x, y) => new { x, y }).ToArray();
     AssertEquals(zipped[0].x, 1);
     AssertEquals(zipped[0].y, 4);
     AssertEquals(zipped[1].x, 2);
     AssertEquals(zipped[1].y, 5);
     AssertEquals(zipped[2].x, 3);
     AssertEquals(zipped[2].y, 6);
 }
Exemplo n.º 29
0
        public void ObservableCanExecuteShouldShowUpInCommand()
        {
            var can_execute = new Subject<bool>();
            var fixture = new ReactiveCommand(can_execute, null);
            var changes_as_observable = new ListObservable<bool>(fixture.CanExecuteObservable);

            var input = new[] { true, false, false, true, false, true };

            int change_event_count = 0;
            fixture.CanExecuteChanged += (o, e) => { change_event_count++; };
            input.Run(x => {
                can_execute.OnNext(x);
                Assert.AreEqual(x, fixture.CanExecute(null));
            });

            // N.B. We check against '5' instead of 6 because we're supposed to
            // suppress changes that aren't actually changes i.e. false => false
            can_execute.OnCompleted();
            Assert.AreEqual(5, change_event_count);

            input.Zip(changes_as_observable.ToList(), (expected, actual) => new { expected, actual })
                 .Do(Console.WriteLine)
                 .Run(x => Assert.AreEqual(x.expected, x.actual));
        }
Exemplo n.º 30
0
        public void ActionExceptionShouldntPermabreakCommands()
        {
            var input = new[] {1,2,3,4};
            var fixture = new ReactiveCommand(x => {
                if (((int)x) == 2)
                    throw new Exception("Die!");
            });

            var exception_list = new List<Exception>();
            var out_list = new List<int>();

            fixture.Subscribe(x => out_list.Add((int)x), ex => exception_list.Add(ex));
            bool we_threw = false;
            foreach (int i in input) {
                try {
                    fixture.Execute(i);
                } catch {
                    we_threw = true;
                    if (i != 2)
                        throw;
                }
            }

            Assert.IsTrue(we_threw);
            input.Zip(out_list, (expected, actual) => new { expected, actual })
                 .Run(x => Assert.AreEqual(x.expected, x.actual));

            // Now, make sure that the command isn't broken
            fixture.Execute(5);
            Console.WriteLine(String.Join(",", out_list));
            Assert.AreEqual(5, out_list.Count);
        }