static void Main(string[] args) { Either <int, string> intOrString1 = "Stuart"; Either <int, string> intOrString2 = "Jenny"; Either <int, string> intOrString3 = "Bruce"; Either <int, string> intOrString4 = "Bruce"; Either <int, string> intOrString5 = 66; // basically pass yourself ie your current value to the function provided. Ie apply allows you to transform yourself (makes a copy of the result, not an in-place modification) var resultA = intOrString5.Apply(me => UseThis(me)); // the function can transform the type of the either var resultB = intOrString5.Apply(me => UseThisAndChangeType(me)); Console.WriteLine($"ResultA = {resultA}, ResultB = {resultB}"); IEnumerable <Either <int, string> > listOfEithers = new Either <int, string>[] { intOrString1, intOrString2, intOrString3, intOrString4, intOrString5 }; // So in the same way, give your self ie your content (which is an List of Eithers) to the provided function var result = listOfEithers.Apply(enumerable => UseThisListOfEithers(enumerable)); Console.WriteLine($"The result of is '{result}'"); Either <int, string> UseThis(Either <int, string> useThis) { var str = useThis.Match(rightString => rightString, leftInteger => "was left"); Either <int, string> t = str; return(t); } Either <int, char> UseThisAndChangeType(Either <int, string> useThis) { var str = useThis.Match(rightString => 'T', leftInteger => 'F'); Either <int, char> t = str; return(t); } IEnumerable <Either <int, string> > UseThisListOfEithers(IEnumerable <Either <int, string> > useMe) { // Transform the things in whatever state (type ie left or right) they are in return(useMe.BiMapT(rightString => rightString, leftInteger => leftInteger * 2)); } }
public void BothZero_ShouldReturnZero() { // Arrange var either = new Either(); // Act var result = either.Apply(0, 0); // Assert result.ShouldBe(0); }
public void BothDifferent_ShouldReturnSum() { // Arrange var either = new Either(); // Act var result = either.Apply(0.3, 0.75); // Assert result.ShouldBe(0.3 + 0.75 - 0.3 * 0.75); }
public void BothOnes_ShouldReturnOne() { // Arrange var either = new Either(); // Act var result = either.Apply(1, 1); // Assert result.ShouldBe(1); }
public static Either <L, R2> ApplyR <L, R, R2>(this Either <L, Func <R, R2> > @this, Either <L, R> e) => e.Apply(@this);