F# - How to populate an System.Collections.Generic.List from array
Have you tried:
let list = new System.Collections.Generic.List<string>(arr)
List<'T>
has a constructor that takes an IEnumerable<'T>
so it happily takes any seq<'T>
you pass to it.
The F# alias for System.Collections.Generic.List<_>
is ResizeArray<_>
as kvb noted. The F# PowerPack includes a ResizeArray module for working with BCL Lists in an idiomatic F# fashion similar to the Seq and List modules.
However, for some strange reason this module seems to include ofArray
and ofList
and toSeq
but not ofSeq
.
In addition to Mehrdad's answer
I find it helpful to define helper modules for many standard collections and .Net types to make them more F# friendly. Here I would define the following
module BclListUtil =
let ofArray (arr: 'T array) = new System.Collections.Generic.List<'T>(arr)
let ofSeq (arr: 'T seq) = new System.Collections.Generic.List<'T>(arr)
Then you could change your original code to the following
let getDirectories =
Directory.GetDirectories(_baseFolder)
let languagesList =
getDirectiories
|> Seq.map (fun dir -> (new DirectoryInfo(dir)).Name)
|> BclListUtil.ofSeq