Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert .NET generic List to F# list

Is there a built-in method to convert the .NET List<> into the F# list?

like image 314
BuddyJoe Avatar asked Jun 23 '10 20:06

BuddyJoe


2 Answers

Try List.ofSeq in the Microsoft.FSharp.Collections namespace.

#                     List.ofSeq : seq<'T> -> 'T list 

It's not specifically for System.Collections.Generic.List<T>, but for IEnumerable<T> (seq<'T> in F#) types in general, so it should still work.

(It's also not strictly built into the F# language, but neither is List<T> built into C# or VB.NET. Those are all part of the respective standard libraries.)

like image 95
stakx - no longer contributing Avatar answered Oct 11 '22 05:10

stakx - no longer contributing


Given IEnumerable<T> foo you would do the following (in C#) to get an F# list<T>:

 var fsharpList = ListModule.OfSeq(foo); 

ListModule refers to Microsoft.FSharp.Collections.ListModule, and is referred to as List from within F# itself.

like image 43
basarat Avatar answered Oct 11 '22 06:10

basarat