F# - How to populate an System.Collections.Generic.List from array

f# seq
f# array
f# resizearray
f# lists
f# array scan
f# map collection
list.filter f#
f list reduce

I have following code that populates a System.Collections.Generic.List I don't like it so I was wondering if there is a better way to do this.

let getDirectories = 
let languagesList = new System.Collections.Generic.List<string>()
Seq.cast getDirectories 
|> Seq.map(fun dir -> (new DirectoryInfo(dir)).Name) 
|> fun range -> languagesList.AddRange(range)

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.

Collection Types, Generic namespace, in that the F# collection types are designed from a functional programming Data structures such as hash tables, linked lists, and arrays have different performance NET Framework type that implements System. fill, O(N), Sets a range of elements of the array to the given value. public: cli::array <T> ^ ToArray(); public T[] ToArray (); member this.ToArray : unit -> 'T[] Public Function ToArray As T() Returns T[] An array containing copies of the elements of the List<T>. Examples. The following example demonstrates the ToArray method and other methods of the List<T> class that act on ranges.

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 = 
let languagesList = 
      |> Seq.map (fun dir -> (new DirectoryInfo(dir)).Name)
      |> BclListUtil.ofSeq

List<T>.Insert(Int32, T) Method (System.Collections , Collections.Generic.List<'T> by its type // abbreviation ResizeArray<'T> to avoid conflicts with the F# List module. // Note: In F# code, F# linked lists are usually  System.Collections.Generic.List<_> It can be confusing initially since list in F# is not the same as List<> in C#. The equivalent of a C# list in F# is ResizeArray. // FSharp.Core type ResizeArray<'T> = System.Collections.Generic.List<'T> You can convert F# types to a ResizeArray.

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.

ResizeArray, Generic operations on the type System.Collections.Generic.List, which is called ResizeArray in the F# libraries. ResizeArray.fill arg1 arg2 arg3 arg4. As elements are added to a List<T>, the capacity is automatically increased as required by reallocating the internal array. If the size of the collection can be estimated, using the List<T>(Int32) constructor and specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List<T> .

Use ResizeArray that equals System.Collections.Generic.List


let languagesList = ResizeArray<string>()

Choosing between collection functions, In C# there are only a few LINQ methods you need to know1 ( Select All functions are available for list , seq and array in F# v4 unless noted. let head = [​1;2;3] |> List.head // val head : int = 1 let badHead : int = [] |> List.head // System. Array.fill: target:'T[] -> targetIndex:int -> count:int -> value:'T -> unit . These collection types differ from the collection types in the .NET Framework, such as those in the System.Collections.Generic namespace, in that the F# collection types are designed from a functional programming perspective rather than an object-oriented perspective. More specifically, only the array collection has mutable elements.

Converting between F# and C# types, Breaking down conversions between C# and F# collections and functions. Posted by System.Array, obj seq, System.Linq.Enumerable.OfType<obj> Generic.List<int> = listTProp and set(v : System.Collections.Generic. Returns an array that contains no duplicate entries according to the generic hash and equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the array then the later occurrences are discarded. ResizeArray.exists arg1 arg2

C# Convert List to Array, These C# programs convert Lists and arrays. They use In part A, the example creates a new List and populates it with some strings. Linq; using System.Text​  Array Types and Modules. The type of all F# arrays is the .NET Framework type System.Array. Therefore, F# arrays support all the functionality available in System.Array. The library module Microsoft.FSharp.Collections.Array supports operations on one-dimensional arrays.

Beginning F# 4.0, Then you must bind the resulting string array to the control that will display it; you do Second, you can bind the grid to a list of objects that have properties; the various properties will become the grid's columns. Collections.Generic open System. MyConnection"] // create a data adapter to fill the dataset let adapter = new  CopyTo(Array, Int32) Copies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from ICollection) GetEnumerator() Returns an enumerator that iterates through a collection. (Inherited from IEnumerable) IndexOf(Object) Determines the index of a specific item in the IList. Insert(Int32, Object)