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 3212712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:52:00+00:00 2026-05-17T14:52:00+00:00

Edit: fixed several syntax and consistency issues to make the code a little more

  • 0

Edit: fixed several syntax and consistency issues to make the code a little more apparent and close to what I actually am doing.

I’ve got some code that looks like this:

SomeClass someClass;
var finalResult = 
  DoSomething(() => 
  {
    var result = SomeThingHappensHere();
    someClass = result.Data;
    return result;
  })
  .DoSomething(() => return SomeOtherThingHappensHere(someClass))
  .DoSomething(() => return AndYetAnotherThing())
  .DoSomething(() => return AndOneMoreThing(someClass))
  .Result;

HandleTheFinalResultHere(finalResult);

where the DoSomething method is an extension method, and it expects a Func passed into it. So, each of the method calls in each of the DoSomething => lambda’s returns a Result type.

this is similar to a Maybe monad. Except instead of checking for nulls, I am checking the status of the Result class, and either calling the Func that was passed into DoSomething or returning the previous Result without calling the Func

the problem i face is that want to have this kind of composition in my code, but i also need to be able to pass data from one of the composed call results into the call of another, as you can see with the someClass variable.

My question isn’t whether or not this is technically correct… i know this works, because I’m currently doing it. My question is whether or not this is an abuse of closures, or command-query separation, or any other principles… and then to ask what better patterns there are for handling this situation, because I’m fairly sure that I’m stuck in a “shiny new hammer” mode with this type of code, right now.

  • 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-17T14:52:00+00:00Added an answer on May 17, 2026 at 2:52 pm

    As has already been noted, you’ve almost implemented a Monad here.

    Your code is a bit inelegant in that the lambdas have side-effects. Monads solve this more elegantly.

    So, why not turn your code into a proper Monad?

    Bonus: you can use LINQ syntax!


    I present:

    LINQ to Results

     
    Example:

    var result =
        from a in SomeThingHappensHere()
        let someData = a.Data
        from b in SomeOtherThingHappensHere(someData)
        from c in AndYetAnotherThing()
        from d in AndOneMoreThing(someData)
        select d;
    
    HandleTheFinalResultHere(result.Value);
    

    With LINQ to Results, this first executes SomeThingHappensHere. If that succeeds, it gets the value of the Data property of the result and executes SomeOtherThingHappensHere. If that succeeds, it executes AndYetAnotherThing, and so on.

    As you can see, you can easily chain operations and refer to results of previous operations. Each operation will be executed one after another, and execution will stop when an error is encountered.

    The from x in bit each line is a bit noisy, but IMO nothing of comparable complexity will get more readable than this!


    How do we make this work?

    Monads in C# consist of three parts:

    • a type Something-of-T,

    • Select/SelectMany extension methods for it, and

    • a method to convert a T into a Something-of-T.

    All you need to do is create something that looks like a Monad, feels like a Monad and smells like a Monad, and everything will work automagically.


    The types and methods for LINQ to Results are as follows.

    Result<T> type:

    A straightforward class that represents a result. A result is either a value of type T, or an error. A result can be constructed from a T or from an Exception.

    class Result<T>
    {
        private readonly Exception error;
        private readonly T value;
    
        public Result(Exception error)
        {
            if (error == null) throw new ArgumentNullException("error");
            this.error = error;
        }
    
        public Result(T value) { this.value = value; }
    
        public Exception Error
        {
            get { return this.error; }
        }
    
        public bool IsError
        {
            get { return this.error != null; }
        }
    
        public T Value
        {
            get
            {
                if (this.error != null) throw this.error;
                return this.value;
            }
        }
    }
    

    Extension methods:

    Implementations for the Select and SelectMany methods. The method signatures are given in the C# spec, so all you have to worry about is their implementations. These come quite naturally if you try to combine all method arguments in a meaningful way.

    static class ResultExtensions
    {
        public static Result<TResult> Select<TSource, TResult>(this Result<TSource> source, Func<TSource, TResult> selector)
        {
            if (source.IsError) return new Result<TResult>(source.Error);
            return new Result<TResult>(selector(source.Value));
        }
    
        public static Result<TResult> SelectMany<TSource, TResult>(this Result<TSource> source, Func<TSource, Result<TResult>> selector)
        {
            if (source.IsError) return new Result<TResult>(source.Error);
            return selector(source.Value);
        }
    
        public static Result<TResult> SelectMany<TSource, TIntermediate, TResult>(this Result<TSource> source, Func<TSource, Result<TIntermediate>> intermediateSelector, Func<TSource, TIntermediate, TResult> resultSelector)
        {
            if (source.IsError) return new Result<TResult>(source.Error);
            var intermediate = intermediateSelector(source.Value);
            if (intermediate.IsError) return new Result<TResult>(intermediate.Error);
            return new Result<TResult>(resultSelector(source.Value, intermediate.Value));
        }
    }
    

    You can freely modify the Result<T> class and the extension methods, for example, to implement more complex rules. Only the signatures of the extension methods must be exactly as stated.

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

Sidebar

Related Questions

Edit: This is a confirmed bug in jQuery 1.3.1. It is fixed in jQuery
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT: minor fixes (virtual Print; return mpInstance) following remarks in the answers. I am
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT What small things which are too easy to overlook do I need to
Edit : Solved, there was a trigger with a loop on the table (read
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know

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.