Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>collect</code> is the F# equivalent of SelectMany however it doesn't provide all the overloads. Here's how to make the one you referenced.</p> <pre><code>let selectMany (ab:'a -&gt; 'b seq) (abc:'a -&gt; 'b -&gt; 'c) input = input |&gt; Seq.collect (fun a -&gt; ab a |&gt; Seq.map (fun b -&gt; abc a b)) // gives // val selectMany : ('a -&gt; seq&lt;'b&gt;) -&gt; ('a -&gt; 'b -&gt; 'c) -&gt; seq&lt;'a&gt; -&gt; seq&lt;'c&gt; </code></pre> <p>I believe F# doesn't provide all the SelectMany overloads because they would add noise to the library. Here's all four overloads to <a href="http://msdn.microsoft.com/en-us/library/bb345618(v=VS.95).aspx" rel="nofollow noreferrer">SelectMany</a> in Microsoft Naming.</p> <pre><code>let selectMany (source : 'TSource seq) (selector : 'TSource -&gt; 'TResult seq) = source |&gt; Seq.collect selector let selectMany (source : 'TSource seq) (selector : 'TSource -&gt; int -&gt; 'TResult seq) = source |&gt; Seq.mapi (fun n s -&gt; selector s n) |&gt; Seq.concat let selectMany (source : 'TSource) (collectionSelector : 'TSource -&gt; 'TCollection seq) (resultSelector : 'TSource -&gt; 'TCollection -&gt; 'TResult) = source |&gt; Seq.collect (fun sourceItem -&gt; collectionSelector sourceItem |&gt; Seq.map (fun collection -&gt; resultSelector sourceItem collection)) let selectMany (source : 'TSource) (collectionSelector : 'TSource -&gt; int -&gt; 'TCollection seq) (resultSelector : 'TSource -&gt; 'TCollection -&gt; 'TResult) = source |&gt; Seq.mapi (fun n sourceItem -&gt; collectionSelector sourceItem n |&gt; Seq.map (fun collection -&gt; resultSelector sourceItem collection)) |&gt; Seq.concat </code></pre> <p>"F# List operations are more powerful than LINQ..." While seq / list operations are great some real "F# power" comes from <a href="http://blogs.msdn.com/b/chrsmith/archive/2008/06/14/function-composition.aspx" rel="nofollow noreferrer">Function Composition</a> and <a href="https://stackoverflow.com/questions/8448/f-curried-function">Currying</a>.</p> <pre><code>// function composition let collect selector = Seq.map selector &gt;&gt; Seq.concat </code></pre>
 

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