title: LINQ query expressions that operate on types (monads?) other than IEnumerable<T> -- Possible uses?
body: <p>I'm reading the book <a href="http://www.functional-programming.net/" rel="noreferrer"><em>Real-world functional programming</em> by Tomas Petricek and Jon Skeet</a> and I'm having a hard time digesting the section on computation expressions<sup>1)</sup> (aka monads).</p>
  
  <p>Through this book, I learnt that &mdash; contrary to my previous experiences &mdash; <strong>LINQ query expressions aren't restricted to <code>IEnumerable&lt;T&gt;</code>, but can work on other custom types as well.</strong> This seems very interesting to me, and I am wondering if there are scenarios where the query expression syntax (<code>from x in ... select ...</code>) would be a nice fit.</p>
  
  <hr>
  
  <h2>Some background info:</h2>
  
  <p>Apparently, such custom types are called <em>computation types</em>, which are portrayed as being essentially the same thing as <a href="http://www.google.com/search?q=haskell+monad" rel="noreferrer"><em>monads</em> in Haskell</a>. I have never been able to grasp what exactly monads are, but according to the book, they are defined through two operations called <em>bind</em> and <em>return</em>.</p>
  
  <p>In functional programming, the type signatures of these two operations would be (I think):</p>
  
  <pre><code>//    Bind      :    M&lt;A'&gt; -&gt; (A' -&gt; B') -&gt; M&lt;B'&gt;
  //
  //    Return    :    A' -&gt; M&lt;A'&gt;
  </code></pre>
  
  <p>where <code>M</code> is the monadic type's name.</p>
  
  <p>In C#, this corresponds to:</p>
  
  <pre><code>Func&lt; M&lt;A&gt;, Func&lt;A,B&gt;, M&lt;B&gt; &gt;   Bind;
  
  Func&lt; A, M&lt;A&gt; &gt;                 Return;
  </code></pre>
  
  <p>It turns out that LINQ's <code>Enumerable.Select</code> (the projection operator) has exactly the same signature as the bind operation with <code>M := IEnumerable</code>.</p>
  
  <h2>My custom LINQ computation type:</h2>
  
  <p>Using this knowledge, I can now write a custom computation type which is <em>not</em> <code>IEnumerable</code>:</p>
  
  <pre><code>// my custom computation type:
  class Wrapped&lt;A&gt;
  {
      // this corresponds to the Return operation:
      public Wrapped(A value)
      {
          this.Value = value;
      }
  
      public readonly A Value;
  }
  
  static class Wrapped
  {
      // this corresponds to the Bind operation:
      public static Wrapped&lt;B&gt; Select&lt;A, B&gt;(this Wrapped&lt;A&gt; x, Func&lt;A,B&gt; selector)
      {
          return new Wrapped&lt;B&gt;(selector(x.Value));
      }
  }
  </code></pre>
  
  <p>And now I can use <code>Wrapped&lt;T&gt;</code> in LINQ query expressions, e.g.:</p>
  
  <pre><code>Wrapped&lt;int&gt; wrapped = new Wrapped&lt;int&gt;(41);
  
  Wrapped&lt;int&gt; answer  = from x in wrapped   // works on int values instead 
                         select x + 1;       // of Wrapped&lt;int&gt; values!
  </code></pre>
  
  <p>Of course this example is not very useful, but it demonstrates how <strong>query expressions can be made to do something else than working with collections</strong>, e.g. wrapping and unwrapping values with some type.</p>
  
  <hr>
  
  <h2>Question:</h2>
  
  <p>The above computation type doesn't seem very useful. Therefore I wonder, what other reasonable uses (besides processing collections) could there be that make use of LINQ query expressions?</p>
  
  <hr>
  
  <p><sup><strong>1)</strong></sup> Section 12.4: "Introducing alternative workflows", starting on page 334.</p>