I'm using C# LanguageExt https://github.com/louthy/language-ext
I have a class MyDto
parsed from some Json. The parsing functions returns Either<Error, Mydto>
.
If the dto matches a given rule, or it is an error, then I would like to get back the result, otherwise, nothing.
The final result should then be of type Option<Either<Error, Mydto>>
.
I ended up having something like this
Option<Either<Error, MyDto>> finalResult =
MyDto.From("some json") // Returns Either<Error, MyDto>
.Right(dto => (dto.Equals("something")
? Some<Either<Error, IDhResponse>>(Right(dto))
: None))
.Left(error => Some<Either<Error, IDhResponse>>(Left(error)));
I don't really like it, as it presents too much repetition.
I then tried this
MyDto.From("some json") // Returns Either<Error, MyDto>
.Map(dto => dto.Equals("something")
? Some(dto)
: None)
but it returns Either<Error, Option<MyDto>>
that looks not so bad if at this point i would be able to bring Option out. Unfortunately I was not able to find anything to that does it.
Does something exist? Or there exist some better way to do what's in my purpose?
maybe something like
MyDto.From("some json") // Returns Either<Error, MyDto>
.Where(dto => dto.Equals("something")) // Only applies to Right branch and wrap everything in Option when fails
Thank you for any advice on this.