title: Combining monads (IEnumerable and Maybe as an example)
body: <p>I have a general question and a more specific case question.</p>
  
  <p>How does one combine different monads in general? Does some combination of the monad operators allow easy composition? Or does one have to write ad-hoc methods to combine each possible pair of monads?</p>
  
  <p>As a specific example, I wrote a Maybe monad. How would one go about using an <code>IEnumerable&lt;IMaybe&lt;T&gt;&gt;</code> ? Apart from manually digging in to the Maybe monad within the LINQ extensions (such as: <code>if(maybe.HasValue)</code>... within <code>select</code> clauses), is there a "monadic" way of combining the two with their respective Bind etc. monad operations?</p>
  
  <p>Otherwise, if I have to write specific combining methods, is something like this the right way to go about it?</p>
  
  <pre><code>    public static IEnumerable&lt;B&gt; SelectMany&lt;A, B&gt;(this IEnumerable&lt;A&gt; sequence, Func&lt;A, IMaybe&lt;B&gt;&gt; func)
      {
          return from item in sequence
                 let result = func(item)
                 where result.HasValue
                 select result.Value;
      }
  
  
      public static IEnumerable&lt;C&gt; SelectMany&lt;A, B, C&gt;(this IEnumerable&lt;A&gt; sequence, Func&lt;A, IMaybe&lt;B&gt;&gt; func, Func&lt;A, B, C&gt; selector)
      {
          return from item in sequence
                 let value = item
                 let maybe = func(item)
                 where maybe.HasValue
                 select selector(value, maybe.Value);
      }
  </code></pre>