LINQ equivalent to f # builder.Zero ()?

So, I became quite dependent on f # evaluation expressions and custom assemblers. I should use C # for most of my daily work, but still want to use LINQ expressions with my own monads / monoids. Does anyone know if there is a C # method analog to f # Zero?

Relevant f # docs

Here is what I do in f #:

type OptionBuilder() =
    member x.Bind(v,f) = Option.bind f v
    member x.Return v = Some v
    member x.ReturnFrom o = o
    member x.Zero() = Option.None

let option = OptionBuilder()

// Example usage (I want something similar from c#)

let t : Option<int> =
    option { if false then return 5 }
+4
source share
2 answers

I'm not sure what exactly you are asking here, but I will give him a chance. Explain the question.

Equivalent in C # on ifwithout elsein a monadic workflow:

from a in b
where c(a)
select a

Logically, this is equivalent (using your Bind, Return, and Zero)

Bind(b, a => c(a) ? Return(a) : Zero)

# where SelectMany ( , # Bind). # Where

Where(M<T>, Func<T, bool>)

: # ; Select, SelectMany, Where .. . . , "" , : , , , - .

"Where" . [a, b, c] b, , [[a], [], [c]]. , , . , .

# , : monad yield , comonad await .. , , Haskell.

?

+10

, :

        var result =
        from value in Option.Some(5)
        where false
        select value;

# LINQ comprehension , Where. :

   public static Option<T> Where<T>(this Option<T> option, Func<T, bool> condition)
        {
            if(option is None || !condition(option.Value))
                return None;

            return option;
        }

Where .

+2

Source: https://habr.com/ru/post/1664016/


All Articles