The document discusses mutable lists and dictionaries in F#. It describes lists as mutable collections that can be resized and allow adding elements using methods like Add. Lists are implemented as resizable arrays under the hood. The List<T> class provides properties like Count and Capacity and methods for adding, removing, and accessing elements. Dictionaries are also mutable and map keys to values, providing similar functionality to lists.
2. Mutable List<T>
• Mutable collections are found in
the System.Collections.Generic namespace. These
built-in collections.
• The List<'T> class is a strongly typed list of objects
that can be accessed by index
• Conceptually, this makes the List<'T> class similar to
arrays
• It provide methods to search, sort, and manipulate
lists.
3. Muttable List<T>
• But Lists<T> can be resized. Therefore you need not
specify a size during declaration.
• Lists are created using the new keyword and calling
the list's constructor
> open System.Collections.Generic;;
> let myList = new List<string>();;
4. Muttable List<T>
> myList.Add("hello");;
val it : unit = ()
>myList.Add("world");;
val it : unit = ()
> myList.[0];;
val it : string = "hello"
lists are mutable
because their Add
methods return unit
rather than returning
another list.
6. Example :Creating a Books List
open System.Collections.Generic
let booksList = new List<string>()
booksList.Add(“ASP.NET")
booksList.Add("AI")
booksList.Add("Cloud Computing")
booksList.Add("JAVA")
booksList
|> Seq.iteri (fun index item -> printfn "%i: %s" index
booksList.[index])
0: ASP.NET
1: AI
2: Cloud Computing
3: OOP
4: JAVA
7. The List(T) Class :Properties
Property Description
Capacity List<'T>.Capacity collection returns the size of
the underlying array
Count The List<'T>.Count property returns the
number of items currently held in the
collection
Item Gets or sets the element at the specified index.
8. open System
open System.Collections.Generic
let items = new List<string>()
let printList (1 : List<_>) =
printfn “1.Count: %i, 1.Capacity: %i” 1.Count 1.Capacity
printfn "Items:"
l |> Seq.iteri (fun index item ->
printfn " 1.[%i]: %s" index 1.[index])
printfn "-----------"
Output :
l.Count: 0, l.Capacity: 0
Items:
-----------
l.Count: 1, l.Capacity: 4
10. printfn "Removing entry for "doggy"n--------n"
items.Remove("doggy") |> ignore
printList items
printfn "Removing item at index 3n--------n"
items.RemoveAt(3)
printList items
Console.ReadKey(true) |> ignore
main()
11. Removing entry for "doggy"
--------
l.Count: 5, l.Capacity: 8
Items:
l.[0]: monkey
l.[1]: kitty
l.[2]: bunny
l.[3]: octopussy
l.[4]: ducky
-----------
Removing item at index 3
--------
l.Count: 4, l.Capacity: 8
Items:
l.[0]: monkey
l.[1]: kitty
l.[2]: bunny
l.[3]: ducky
-----------
Underlying implementation
• The list<'t> class is just a fancy
wrapper for an array.
• When a list<'t> is
constructed, it creates
an 4-element array in
memory.
• Adding the first 4 items is an o(1)
operation.
• However, as soon as the 5th element
needs to be added, the list double the
the size of the internal array
• Which means it has to reallocate new
memory and copy elements in the
existing list;
14. > let myList = new List<int>(1000);;
val myList : List<int>
> myList.Count, myList.Capacity;;
val it : int * int = (0, 1000)
>seq { 1 .. 1000 }
|> Seq.iter (fun x – >myList.Add(x));;
val it : unit = ()
> myList.Count, myList.Capacity;;
val it : int * int = (1000, 1000)
Points To Remember
If the maximum size of
the list is known
beforehand, it is
possible to avoid the
performance hit by
calling the
List<'T>(size : int)
constructor
15. Constructor
List(T)() Initializes a new instance of the List(T) class that
is empty and has the default initial
capacity.
List(T)(Int32) Initializes a new instance of the List(T) class that
is empty and has the specified initial
capacity.
List(T)(IEnumerable(T)) Initializes a new instance of the List(T) class that
contains elements copied from the
specified collection and has sufficient
capacity to store the number of elements
copied.
16. Methods
Add Adds an object to the end of the List(T).
AddRange Adds the elements of the specified collection to the
end of the List(T).
Clear Removes all elements from the List(T).
Contains Determines whether an element is in the List(T).
17. Method
RemoveAt
Removes the element at the specified index of
the List(T).
RemoveRange Removes a range of elements from the List(T).
Remove Removes the first occurrence of a specific object
from the List(T).
RemoveAll Removes all the elements that match the
conditions defined by the specified predicate.
18. Methods
Equals(Object) Determines whether the specified object is equal to the
current object. (Inherited from Object.)
Exists Determines whether the List(T) contains elements that
match the conditions
Clear Removes all elements from the List(T).
Find Searches for an element that matches the conditions
defined by the specified predicate, and returns the first occurrence
within the entire List(T).
19. Example :Methods
open System.Collections.Generic
let booksList = new List<string>()
booksList.Add(".NET")
booksList.Add("AI")
booksList.Add("Cloud Computing")
booksList.Add("JAVA")
booksList
|> Seq.iteri (fun index item -> printfn "%i: %s" index
booksList.[index])
0: ASP.NET
1: AI
2: Cloud Computing
3: OOP
4: JAVA
5: Andriod
20. Example :Insert
printfn "Total %d books" booksList.Count
booksList
|> Seq.iteri (fun index item -> printfn "%i: %s" index
booksList.[index])
booksList.Insert(2, "Roots")
printfn("after inserting at index 2")
printfn"Total %d books" booksList.Count
21. Example :RemoveAt
booksList
|> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
booksList.RemoveAt(3)
printfn("after removing from index 3")
printfn"Total %d books" booksList.Count
booksList
|> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
22. F# - Mutable Dictionary
• The Dictionary<'TKey, 'TValue> class is the mutable
• It is equivalent to map data structure and contains many of the
same functions.