Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>SelectMany</code> in C# corresponds to bind in Haskell <code>(&gt;&gt;=)</code> or <code>flatMap</code> in Scala. The signature of <code>&gt;&gt;=</code> in Haskell is:</p> <pre><code>(&gt;&gt;=) :: Monad m =&gt; m a -&gt; (a -&gt; m b) -&gt; m b </code></pre> <p>so bind is an operation used to construct one monadic value from another.</p> <p>In the case of C# <code>m</code> in the above signature is <code>IEnumerable</code>, <code>IObservable</code>, <code>IQueryable</code> etc. For <code>IEnumerable</code>, <code>SelectMany</code> is therefore</p> <pre><code>IEnumerable&lt;A&gt; -&gt; (A -&gt; IEnumerable&lt;B&gt;) -&gt; IEnumerable&lt;B&gt; </code></pre> <p>or in C#</p> <pre><code>public static IEnumerable&lt;B&gt; SelectMany&lt;A, B&gt;(this IEnumerable&lt;A&gt; first, Func&lt;A, IEnumerable&lt;B&gt;&gt; selector) </code></pre> <p>The meaning of bind depends on the monad type, for IEnumerable, each element in the input sequence is used to create a new sequence, and the resulting sequence of sequences is flattened to produce the output sequence.</p> <p>There is another formulation of bind which may make this more clear. While monads are often described in terms of their implementation of bind, monads must also support two other operations, <code>map</code> and <code>join</code>.</p> <p><code>map</code> corresponds to Select in C# and looks like:</p> <pre><code>map :: Monad m =&gt; (a -&gt; b) -&gt; (ma -&gt; m b) </code></pre> <p>so it is a 'structure preserving' way of lifting a regular function over a monadic value.</p> <p><code>join</code> has the type</p> <pre><code>join :: Monad m =&gt; m m a -&gt; m a </code></pre> <p>so join is used to flatten nested monadic values. In C# this would look like</p> <pre><code>public static IEnumerable&lt;A&gt; Join&lt;A&gt;(this IEnumerable&lt;IEnumerable&lt;A&gt;&gt; nested) </code></pre> <p><code>bind</code> can be implemented in terms of map and join as</p> <pre><code>m &gt;&gt;= f = join (map f m) </code></pre> <p>so to answer the original question, <code>SelectMany</code> corresponds to <code>bind</code> or <code>flatMap</code> in other languages. Bind is not just flattening, but can be seen as a transformation followed by a flattening of nested monadic values (e.g. sequences in the case of <code>IEnumerable&lt;T&gt;</code>). <code>join</code> for <code>IEnumerable&lt;T&gt;</code> does not exist in the current linq extensions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload