SlideShare a Scribd company logo
1 of 22
Mutable Data, Lists,
Dictionary
F Sharp - Lists & Dictionary
Dr. Rajeshree Khande
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.
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>();;
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.
Muttable List<T>
>myList
|> Seq.iteri (fun index item -> printfn "%i: %s" index myList.[index]);;
Output
0: hello
1: world
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
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.
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
let main() =
printList items
items.Add("monkey")
printList items
items.Add("kitty")
items.Add("bunny")
printList items
items.Add("doggy")
items.Add("octopussy")
items.Add("ducky")
printList items
Items:
l.[0]: monkey
-----------
l.Count: 3, l.Capacity: 4
Items:
l.[0]: monkey
l.[1]: kitty
l.[2]: bunny
-----------
l.Count: 6, l.Capacity: 8
Items:
l.[0]: monkey
l.[1]: kitty
l.[2]: bunny
l.[3]: doggy
l.[4]: octopussy
l.[5]: ducky
-----------
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()
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;
List -1
1 2 3 4
O(4)
1 2 3 4 5 6 7 8
O(8)
• 1-list = 4-element array in memory.
List -1 1 2 3 4
List1.capcity = 8
List1.Add(“Item…9”) === capcity 16
List -1 1 2 3 4 5 6 7 8
List1.capcity = 4
List1.Add(“Item….5”) === capcity 8
1 2 3 4 5 6 7 8 9
> 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
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.
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).
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.
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).
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
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
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])
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.

More Related Content

What's hot (20)

C# Collection classes
C# Collection classesC# Collection classes
C# Collection classes
 
16 containers
16   containers16   containers
16 containers
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
L11 array list
L11 array listL11 array list
L11 array list
 
C# Filtering in generic collections
C# Filtering in generic collectionsC# Filtering in generic collections
C# Filtering in generic collections
 
Groovy
GroovyGroovy
Groovy
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 
Array list
Array listArray list
Array list
 
Concept of Stream API Java 1.8
Concept of Stream API Java 1.8Concept of Stream API Java 1.8
Concept of Stream API Java 1.8
 
Python list
Python listPython list
Python list
 
LIST IN PYTHON
LIST IN PYTHONLIST IN PYTHON
LIST IN PYTHON
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
List data structure
List data structure List data structure
List data structure
 
List interface
List interfaceList interface
List interface
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
List in java
List in javaList in java
List in java
 
Lists
Lists Lists
Lists
 
Chapter 20 generic collections
Chapter 20   generic collectionsChapter 20   generic collections
Chapter 20 generic collections
 
Python List Comprehensions
Python List ComprehensionsPython List Comprehensions
Python List Comprehensions
 

Similar to F# Lists & Dictionaries: Mutable Data Structures

COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptxSruthyPJ
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfalivaisi1
 
C++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsC++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsMohammad Shaker
 
This assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfThis assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfadislifestyle
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfalsofshionchennai
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptxHammadTariq51
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionssuseredfbe9
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfmayorothenguyenhob69
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptRithwikRanjan
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Poojith Chowdhary
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212Mahmoud Samir Fayed
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 

Similar to F# Lists & Dictionaries: Mutable Data Structures (20)

COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
C++ Windows Forms L07 - Collections
C++ Windows Forms L07 - CollectionsC++ Windows Forms L07 - Collections
C++ Windows Forms L07 - Collections
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
This assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdfThis assignment uses a rubric of 200 points It focuses on O.pdf
This assignment uses a rubric of 200 points It focuses on O.pdf
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Prompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdfPrompt Your task is to create a connected list implementation and .pdf
Prompt Your task is to create a connected list implementation and .pdf
 
List in Python
List in PythonList in Python
List in Python
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
Ch02
Ch02Ch02
Ch02
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdf
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
 
Lists
ListsLists
Lists
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212The Ring programming language version 1.10 book - Part 30 of 212
The Ring programming language version 1.10 book - Part 30 of 212
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 

More from DrRajeshreeKhande

More from DrRajeshreeKhande (19)

.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
Exception Handling in .NET F#
Exception Handling in .NET F#Exception Handling in .NET F#
Exception Handling in .NET F#
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
 
.NET F# Class constructor
.NET F# Class constructor.NET F# Class constructor
.NET F# Class constructor
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
F# Console class
F# Console classF# Console class
F# Console class
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
.Net (F # ) Records, lists
.Net (F # ) Records, lists.Net (F # ) Records, lists
.Net (F # ) Records, lists
 
MS Office for Beginners
MS Office for BeginnersMS Office for Beginners
MS Office for Beginners
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
 
Java String class
Java String classJava String class
Java String class
 
JAVA AWT components
JAVA AWT componentsJAVA AWT components
JAVA AWT components
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Dr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive inputDr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive input
 
Dr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to javaDr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to java
 
Java Exceptions Handling
Java Exceptions Handling Java Exceptions Handling
Java Exceptions Handling
 
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande  : Java BasicsDr. Rajeshree Khande  : Java Basics
Dr. Rajeshree Khande : Java Basics
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Recently uploaded (20)

Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

F# Lists & Dictionaries: Mutable Data Structures

  • 1. Mutable Data, Lists, Dictionary F Sharp - Lists & Dictionary Dr. Rajeshree Khande
  • 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.
  • 5. Muttable List<T> >myList |> Seq.iteri (fun index item -> printfn "%i: %s" index myList.[index]);; Output 0: hello 1: world
  • 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
  • 9. let main() = printList items items.Add("monkey") printList items items.Add("kitty") items.Add("bunny") printList items items.Add("doggy") items.Add("octopussy") items.Add("ducky") printList items Items: l.[0]: monkey ----------- l.Count: 3, l.Capacity: 4 Items: l.[0]: monkey l.[1]: kitty l.[2]: bunny ----------- l.Count: 6, l.Capacity: 8 Items: l.[0]: monkey l.[1]: kitty l.[2]: bunny l.[3]: doggy l.[4]: octopussy l.[5]: ducky -----------
  • 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;
  • 12. List -1 1 2 3 4 O(4) 1 2 3 4 5 6 7 8 O(8)
  • 13. • 1-list = 4-element array in memory. List -1 1 2 3 4 List1.capcity = 8 List1.Add(“Item…9”) === capcity 16 List -1 1 2 3 4 5 6 7 8 List1.capcity = 4 List1.Add(“Item….5”) === capcity 8 1 2 3 4 5 6 7 8 9
  • 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.