F# List SelectMany

Posted by Tuomas Hietanen on Stack Overflow See other posts from Stack Overflow or by Tuomas Hietanen
Published on 2011-01-04T23:56:34Z Indexed on 2011/01/05 0:53 UTC
Read the original article Hit count: 331

Filed under:
|
|

This is quite simple question but I didn't find an answer:

Is there any Seq/List operation in F# to match the LINQ SelectMany?

  • I know I can use System.Linq in F# if I want to.
  • I know I can make a recursive method and use F# Computation Expressions (and make even more powerful things).

But if I try to prove that F# List operations are more powerful than LINQ...

  • .Where = List.filter
  • .Select = List.map
  • .Aggregate = List.fold
  • ...

In C# SelectMany usage syntax is pretty simple:

var flattenedList = from i in items1
                    from j in items2
                    select ...

Is there any easy direct match, List.flatten, List.bind or something like that?

SelectMany has a couple of signatures, but the most complex one seems to be:

IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
    this IEnumerable<TSource> source, 
    Func<TSource, IEnumerable<TCollection>> collectionSelector, 
    Func<TSource, TCollection, TResult> resultSelector
);

In F# terms this would be:

('a -> 'b list) -> ('a -> 'b -> 'c) -> 'a list -> 'c list

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about F#