Recently I tried applying Maybe monad pattern in my C# code using this library.
What I found difficult to grasp was converting such a function into Maybe paradigm:
public Maybe<object> DoSomething(IReader reader)
{
while (true)
{
var result = reader.Read();
if (result == null) return Maybe<object>.Nothing;
if (result.HasValue) return new Maybe<object>(null);
}
}
I would like to have it written using from x in X
form. The functionality that stands behind this function is to read IReader
until it returns a value (Maybe
has a value) or an error occurs (null
gets returned).
the answer to your comment/question is: you don't - yeah you could try it using recursive calls but this might fail horrible in C# and you are way better of with the while
from x in X
is just the monadic - bind (it get's translated into theSelectMany
functions) and there is just no direct way in the LINQ syntax for this.But you can write your own function like this:
and call like (see below)
remarks
first the
Maybe<object>
(object
) part is a smell - because you most certainly want a concrete type in there instead of the genericobject
Then
new Maybe<object>(null)
is very strange tooI would have suggested something like:
then of course this part is there to get some
Maybe
value - the thing you are trying to do withfrom x in X
is the monadic-bind - which you can only use once you have aMaybe
to start with:disclaimer
I did not compile any of this (because I was to lazy to download the github project and stuff) - put you should be able to easily solve any syntax errors that might hide there - sorry for that
In case you have major troubles just leave a comment