F# For Dummys - Day 13 Collections Array
Tags: fsharp
Published: 2024-05-23 14:27:57

Today we introduce another collection Array</br>
Array is a type of collection that stores a fixed-size sequence of elements of the same type</br>
Array is mutable, you can change the elements after the array has been created</br>
#### Create Array
- Explicitly specifying elements
```f#
let array1 = [| 1; 2; 3 |]
```
the ; could be omit between elements if you write them in seperated lines
```f#
let array1 =
    [|
        1
        2
        3
     |]
```
all elements should be the same type
```f#
let array1 = [| 1; 2; "3"|]
```
the first elements is int, the last is string, will get compile error:</br>
All elements of an array must be implicitly convertible to the type of the first element, which here is 'int'. This element has type 'string'
- Using comprehensions
```f#
let array1 = [| for i in 1 .. 5 -> i * i |] // [| 1; 4; 9; 16; 25 |]
```
- Array.Empty</br>
Returns an empty array of the given type
```f#
let myEmptyArray = Array.empty // Evaluates to [| |]
```
- Array.init</br>
Creates an array given the dimension and a generator function to compute the elements</br>
syntax: Array.init count initializer
```f#
let array0 = Array.init 5 (fun i -> i) // [| 0; 1; 2; 3; 4 |]
```
- Array.create</br>
syntax: Array.init count value</br>
Creates an array whose elements are all initially the given value
```f#
let zeroes = Array.create 5 0 // zeroes = [| 0; 0; 0; 0; 0 |]
```
- Array.zeroCreate</br>
```f#
let arrayOfTenZeroes : int array = Array.zeroCreate 5 // [| 0; 0; 0; 0; 0 |]
```
#### Get element of Array
- Using index
```f#
let array0 = Array.init 5 (fun i -> i)

printfn "first element %A" array0.[0] // first element 0
```
- Using slice
```f#
let array0 = Array.init 5 (fun i -> i)

printfn "elements from 0 to 2: %A" array0.[0..2] // elements from 0 to 2: 0,1,2
printfn "elements from 2 to the end: %A" array0.[2..] // elements from 2 to the end: 2,3,4
```
- Using build in method get
```f#
let inputs = [| "a"; "b"; "c" |]
let secondElement = Array.get inputs 1

printfn "secondElement: %s" secondElement // secondElement: b
```
#### Loop an Array
- for...in
```f#
let array1 = [| for i in 1 .. 5 -> i * i |]

for number in array1 do
    printfn "Number: %d" number
```
- Array.iter
syntax: Array.iter action array
```f#
let array1 = [| for i in 1 .. 5 -> i * i |]

Array.iter (printfn "Number: %d") array1
```
#### Modify element of Array
- Using index to set value
```f#
let inputs = [| "a"; "b"; "c" |]
inputs.[0] <- "d"

printfn "inputs: %A" inputs // d,b,c
```
- Using build in method set</br>
syntax: Array.set array index value
```f#
let array1 = [| for i in 1 .. 5 -> i * i |]
printfn "array1 before set: %A" array1 // [| 1; 4; 9; 16; 25 |]

for i in 0 .. array1.Length - 1 do
    Array.set array1 i (i * 2)

printfn "array1 after set: %A" array1 // [| 0; 2; 4; 6; 8 |]
```
#### Pattern Matching with Array
```f#
let sumFirstTwoElements arr =
    match arr with
    | [| x; y |] -> x + y
    | [| x |] -> x
    | [| |] -> 0
    | _ -> failwith "Array has more than two elements."

let sum1 = sumFirstTwoElements [| 1; 2 |]
let sum2 = sumFirstTwoElements [| 5 |]
let sum3 = sumFirstTwoElements [| |]

printfn "Sum of first two elements: %d" sum1 // 3
printfn "Sum of first element: %d" sum2 // 5
printfn "Sum of empty array: %d" sum3 // 0
```
#### Operate Array
- Concat</br>
Builds a new array that contains the elements of each of the given sequence of arrays</br>
Array.concat arrays
```f#
let inputs = [ [| 1; 2 |]; [| 3 |]; [| 4; 5 |] ]
let newArray = inputs |> Array.concat

printfn "newArray: %A" newArray
```
- Append</br>
Builds a new array that contains the elements of the first array followed by the elements of the second array
syntax: Array.append array1 array2
```f#
let results = Array.append [| 1; 2 |] [| 3; 4 |]

printfn "results: %A" results
```
- Filter
```f#
let originalArray = [| 1; 2; 3; 4; 5; 6 |]
let evenNumbers = Array.filter (fun x -> x % 2 = 0) originalArray

printfn "evenNumbers: %A" evenNumbers // 2, 4, 6
```
- Map
```f#
let originalArray = [| 1; 2; 3 |]
let doubleArray = Array.map (fun x -> 2 * x) originalArray

printfn "doubleArray: %A" doubleArray // doubleArray: 2,4,6
```
- Fold
```f#
let numbers = [| 1; 2; 3; 4; 5 |]
let sum = Array.fold (fun acc x -> acc + x) 0 numbers

printfn "sum: %i" sum // sum: 15
```