Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# list to C# IEnumerable: most efficient method?

Tags:

c#

.net

list

f#

seq

I'm currently working on an F# library with GUI written in C# and I would like to ask what is the best or correct way to pass an F# (generic) list to a C# code (generic IEnumerable).

I've found three ways so far:

[1; 2; 3; 4; 5;] |> List.toSeq

[1; 2; 3; 4; 5;] |> Seq.ofList 

[1; 2; 3; 4; 5;] :> seq<int>

Is there any practical difference between these three methods, please?

like image 610
eMko Avatar asked May 31 '16 19:05

eMko


1 Answers

If you look in the F# library source code, you'll find out that they are all the same:

  • Seq.ofList just calls List.ofSeq as you can see here in the "list.fs" file

  • List.toSeq is implemented using s :> seq<_> as you can see here in the "seq.fs" file

In terms of readability, I would probably use Seq.ofList or List.toSeq, especially if the code is a part of a larger F# pipeline, because then it makes the code a bit nicer:

someInput
|> List.map (fun x -> whatever) 
|> List.toSeq
like image 69
Tomas Petricek Avatar answered Sep 18 '22 04:09

Tomas Petricek