public void ChooseTest() { var withNothing = new[] { Maybe.Just(1), Maybe.Nothing }; var withoutNothing = new[] { Maybe.Just(1), Maybe.Just(2) }; var onlyNothing = new Maybe<int>[] { Maybe.Nothing }; var emtpy = new Maybe<int>[0]; Assert.AreEqual(new[] { 1 }, withNothing.Choose()); Assert.AreEqual(new[] { 1, 2 }, withoutNothing.Choose()); Assert.IsEmpty(onlyNothing.Choose()); Assert.IsEmpty(emtpy.Choose()); }
public void Choose2Test() { var collection = new[] { "1", "2" }; Assert.AreEqual(collection.Choose(x => x == "1" ? Maybe.Just(1) : Maybe.Nothing), new[] { 1 }); Assert.IsEmpty(collection.Choose(x => Maybe<string>.Nothing)); }
public void ChooseWithSelectorTest() { var collection = new[] {1, 2, 3}; var result = collection.Choose( input => input == 1 ? Maybe.Just(input.ToString()) : Maybe.Nothing, (input, v) => new {Input = input, Output = v}).ToList(); Assert.AreEqual(new[] {new {Input = 1, Output = "1"}}, result); Assert.IsEmpty(collection.Choose<int, string, string>( x => Maybe<string>.Nothing, (a, b) => { throw new Exception("This should never be thrown"); })); }