Reputation: 2103
Are the List.collect equivalent of LINQ List.SelectMany?
[1;2;3;4] |> List.collect (fun x -> [x * x]) // [1;4;9;16]
in LINQ
new List<int>() { 1, 2, 3, 4 }
.SelectMany(x => new List<int>() { x * x }); // 1;4;9;16
Edited:
More appropriate example
let list1 = [1;2;3;4]
let list2 = [2;4;6]
// [2; 4; 6; 4; 8; 12; 6; 12; 18; 8; 16; 24]
list1 |> List.collect (fun a -> list2 |> List.map (fun b -> a * b))
...
var list1 = new List<int>() { 1, 2, 3, 4 };
var list2 = new List<int>() { 2, 4, 6 }
// 2,4,6,4,8,12,6,12,18,8,16,24
list1.SelectMany(a => list2.Select(b => a * b));
Upvotes: 8
Views: 1946
Reputation: 6324
Just wanted to mention that nothing will stop you from using the LINQ extension methods directly in F#:
open System.Linq
open System.Collections.Generic
open System
let xs = seq [seq [1;2;3]; seq [4;5;6]]
xs.SelectMany(fun x -> x ).Select(fun x -> x * x)
// val it: IEnumerable<int> = seq [1; 4; 9; 16; ...]
And for a more idiomatic solution for your specific example in F# 4.1 (though it only works on two lists):
let list1 = [1;2;3;4]
let list2 = [2;4;6]
(list1,list2) ||> List.allPairs |> List.map (fun (a,b) -> a * b)
//val it : int list = [2; 4; 6; 4; 8; 12; 6; 12; 18; 8; 16; 24]
Upvotes: 2
Reputation: 12184
More or less. The direct F# equivalent to SelectMany
would be Seq.collect
which has the signature:
Seq.collect : ('T -> 'Collection) -> seq<'T> -> seq<'U> (requires 'Collection :> seq<'U>)
seq<'T>
is just a type alias for IEnumerable<T>
.
F# list
is a concrete collection (an immutable list) and consequently List.collect
is evaluated strictly.
Note also that F# list
and the .NET System.Collections.Generic.List<T>
types are not equivalent. System.Collections.Generic.List<T>
is a mutable collection and is normally referred to via its type alias ResizeArray<'T>
in F#.
Upvotes: 13
Reputation: 144136
They behave the same however Enumerable.SelectMany
returns a lazy sequence (IEnumerable<T>
) while List.collect
returns a list which is created strictly. Also be aware that F# lists are persistent linked lists while C# lists are backed by an array.
Upvotes: 8