I'm trying to grok how to deal with functions that turn one "Either" into many "Either"s and how you then merge those back into a single stream.
The following turns one string into many numbers, squaring each and ignoring any errors. Given "1,2,Foo,4" it writes out 1,4,16.
It works but I don't understand why Bind(SafeSplit) returns an EitherData which has a Right property I need to dereference.
private static void ValidationPipelineVersion()
{
var result = Right<Exception, string>("1,2,Foo,4")
.Bind(SafeSplit)
.Bind(numStrs => numStrs.Right.Select(SafeParse))
.Rights() // ignore exceptions for now
.Map(num => num * num) // Squared
.Iter(num => WriteLine(num));
}
private static Either<Exception,string[]> SafeSplit(string str)
{
try
{
return str.Split(",");
}
catch (Exception e)
{
return e;
}
}
private static Either<Exception,int> SafeParse(string str)
{
try
{
return int.Parse(str);
}
catch (Exception e)
{
return e;
}
}