public Validator <T> Validate <U>(Func <T, U> projection, Predicate <U> validation, string message) { //return Validate(t => validation(projection(t)), message); return(Validate(projection.AndThen(validation), message)); }
public void AndThen() { Func <int, long> plus2 = a => a + 2; Func <string, int> length = s => s.Length; Func <string, long> lengthPlus2 = length.AndThen(plus2); Assert.AreEqual(5, lengthPlus2("abc")); }
public void AndThen_Executes_Functions_In_Correct_Order() { Func <int, int> add1 = x => x + 1; Func <int, int> doubleValue = x => x * 2; Func <int, int> add1AndDoubleValue = add1.AndThen(doubleValue); add1AndDoubleValue(5).ShouldBe(12); }
public void Test_Func_AndThen() { Func <string, string> first = input => input + "1"; Func <string, string> second = input => input + "2"; Func <string, string> composed = first.AndThen(second); string result = composed("0"); Assert.Equal("012", result); }
public void GivenALisOfItemHolds_WhenCalculateTotalCostCalled_ThenReturnsTotalCostWithSeviceTaxApplied() { // Arrange var items = new List <ItemHolder>() { new ItemHolder("1", 10, 50), new ItemHolder("2", 1, 500), new ItemHolder("3", 5, 100) }; Func <List <ItemHolder>, double> totalCalculator = SuperMarket.CalculateTotalCost; var costCalculator = totalCalculator.AndThen(SuperMarket.CalculateServiceTax(12.5)); // Act var totalCost = costCalculator(items); // Assert Assert.That(totalCost, Is.EqualTo(1687.5)); }
public ICoreStatBuilder Resolve(ResolveContext context) => new BuffCoreStatBuilder( _buffs.Select(b => b.Resolve(context)).ToList(), _statFactory.AndThen(b => b.Resolve(context)), _restrictionsBuilder.Resolve(context));
public ICoreStatBuilder Resolve(ResolveContext context) => new StatBuilderWithValueConverter(_inner.Resolve(context), _entity, _createValue.AndThen(b => b.Resolve(context)), _combineValues);
public void GivenEmptyCartProductWithBuyXAndGetYOffer_When5ProductsAdded_ThenReturnsExpectedTotalPriceAndDiscount() { // Arrange var doveProduct = new Product("Dove Soap", 39.99); var offerBuy2Get1Free = new Func <IProduct, uint, IProduct>((p, q) => { uint numberOfTimesToApply = q / 3; if (numberOfTimesToApply > 0) { return(new DiscountAsProduct( p.Name + " - Buy 2 Get 1 Free offer", numberOfTimesToApply * p.UnitPrice)); } return(new NullProduct()); }); var discountCalculator = new Func <IEnumerable <IProduct>, double>((products) => { var discount = 0.0d; var offerApplied = new List <IProduct>(); var enumerable = products.ToList(); foreach (var product in enumerable) { if (!(product is IProductWithAssociatedOffer productWithAssociatedOffer) || offerApplied.Contains(product)) { continue; } var quantity = enumerable.Count(p => p.Name == product.Name); discount -= productWithAssociatedOffer.GetOffer()(product, (uint)quantity).UnitPrice; offerApplied.Add(product); } return(discount); }); var priceCalculator = new Func <IEnumerable <IProduct>, double>((products) => { var price = 0.0d; var enumerable = products.ToList(); price = enumerable.Select(p => p.UnitPrice).Sum(); price -= discountCalculator(enumerable); return(price); }); var serviceTaxCalculator = new Func <double, double>((price) => price + price * 0.125); var productWithOffer = new ProductWithAssociatedOffer(doveProduct, offerBuy2Get1Free); var cart = new DiscountShoppingCart( priceCalculator.AndThen(serviceTaxCalculator), discountCalculator); // Act cart.AddProduct(productWithOffer, 3); // Assert Assert.That(cart.Products.Count(p => p.Name == "Dove Soap"), Is.EqualTo(3)); Assert.That(cart.GetTotalPrice(), Is.EqualTo(89.98)); Assert.That(cart.GetTotalDiscount(), Is.EqualTo(39.99)); }
/// <summary> /// Composes two functions together. /// (Outer with the value of the result of inner will be called, if the resulting function will be called) /// </summary> /// <param name="outer">The outer function</param> /// <param name="inner">The inner function</param> /// <typeparam name="T1">Type of the first input parameter</typeparam> /// <typeparam name="T2">Type of the second input parameter</typeparam> /// <typeparam name="T3">The result type of the resulting function</typeparam> /// <returns>The composed function</returns> public static Func <T1, T3> Compose <T1, T2, T3>(this Func <T2, T3> outer, Func <T1, T2> inner) => inner.AndThen(outer);