Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:08:16+00:00 2026-05-26T22:08:16+00:00

I have a special need for a kind of reverse Maybe monad that when

  • 0

I have a special need for a kind of reverse Maybe monad that when being “Nothing” will continue to try to get a value and will keep the first valid value that it gets.

I have tried to outline what I mean here:

public void Test()
{
    var r = from x in SomeActionToGetResult()
            from y in Stop()
            from z in SomeOtherActionToGetResult()
            select x | y | z;
}

public Maybe<Result> SomeActionToGetResult()
{
    return new Result().Unit();
}

public Maybe<Result> SomeOtherActionToGetResult()
{
    return new Result().Unit();
}

public Maybe<Result> Stop()
{
    return new Nothing<Result>();
}

The Result class looks like this:

public class Result
{
    public static Result operator |(Result a, Result b)
    {
        if (a != null)
            return a;

        return b;
    }
}

The Unit function is like normal, but the Bind functions gives me trouble:

public static Maybe<U> Bind<T, U>(this Maybe<T> m, Func<T, Maybe<U>> k)
{
    if (!m.HasAValidValue)
        return k(m.Value);

    return m; // <--- problem right here
}

As can be seen, I will – given that m has no value – try to run k() to obtain a valid value. If m already has a value I will like to use that and never run k().

T and U start out as the same type – but as soon as the anonymous types in the linq query kicks in, I can no longer rely on that.

Is this at all possible to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:08:17+00:00Added an answer on May 26, 2026 at 10:08 pm

    (This may not be any help to you, in which case I’ll just delete it. If you’re playing around with monads this is quite possibly teaching my grandmother to suck eggs, but…)

    Are you aware that C# already has this at a language level with the null-coalescing operator?

    var r = SomeActionToGetResult() ?? Stop() ?? SomeOtherActionToGetResult();
    

    If you need to achieve this in a more library-based form, that’s fine (and I’ll see what I can think of) but if you can just use the existing operator, I would.

    EDIT: I’ve had a go at your puzzle, and I think basically it’s not a good fit for LINQ. The problems are mostly to do with the types that LINQ expects in SelectMany. For example, your second line is effectively transformed to:

    SomeActionToGetResult().SelectMany(x => Stop(), (x, y) => new { x, y });
    

    Here the problems are:

    • We don’t use the value of x in the Stop() expression, and logically can’t as we only want it to be executed if x evaluates to None
    • The anonymous type is necessarily not the same type as the T part of the return type of SomeActionToGetResult(), so we can’t convert it into a Maybe<T>. We’d have to introduce a new type to extract them both.

    Then later when we have:

    select x | y | z
    

    Is that logically the only thing you could have? Would it make sense to have

    select z | y | x
    

    and if so, what would that mean? We can’t avoid SomeActionToGetResult() from being executed to start with, as the extension method only works on the result of that method call… so either the projection has to be ignored and you always evaluate x, then y, then z – or you accept that the ordering won’t always be preserved exactly.

    Fundamentally I suspect you haven’t really got a monad here, otherwise I’d expect it to fit – but I’m not really competent enough in the maths to say.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Listview and I need a special behavior. When I click the
I have a e-mail address validator but I need to add special characters as
Ok, I understand that using strings that have special characters is an encoding issue.
I have some special parameters to all my wcf service methods that are handled
I have seen a few OSX apps that have a Special Characters menu item
I seem to remember that Regular Expressions in DotNet have a special mechanism that
I have a linkedserver that will change. Some procedures call the linked server like
I have a heirarchy of classes that get initialized from a database. I am
I've one kind of request that I often use in Linq: I need a
Is it possible to have special characters like åäö in the key? If i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.