DEV Community

Cover image for F# For Dummys - Day 13 Collections Array
AllenZhu
AllenZhu

Posted on

F# For Dummys - Day 13 Collections Array

Today we introduce another collection Array
Array is a type of collection that stores a fixed-size sequence of elements of the same type
Array is mutable, you can change the elements after the array has been created

Create Array

  • Explicitly specifying elements
let array1 = [| 1; 2; 3 |]
Enter fullscreen mode Exit fullscreen mode

the ; could be omit between elements if you write them in seperated lines

let array1 =
    [|
        1
        2
        3
     |]
Enter fullscreen mode Exit fullscreen mode

all elements should be the same type

let array1 = [| 1; 2; "3"|]
Enter fullscreen mode Exit fullscreen mode

the first elements is int, the last is string, will get compile error:
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
let array1 = [| for i in 1 .. 5 -> i * i |] // [| 1; 4; 9; 16; 25 |]
Enter fullscreen mode Exit fullscreen mode
  • Array.Empty Returns an empty array of the given type
let myEmptyArray = Array.empty // Evaluates to [| |]
Enter fullscreen mode Exit fullscreen mode
  • Array.init Creates an array given the dimension and a generator function to compute the elements syntax: Array.init count initializer
let array0 = Array.init 5 (fun i -> i) // [| 0; 1; 2; 3; 4 |]
Enter fullscreen mode Exit fullscreen mode
  • Array.create syntax: Array.init count value Creates an array whose elements are all initially the given value
let zeroes = Array.create 5 0 // zeroes = [| 0; 0; 0; 0; 0 |]
Enter fullscreen mode Exit fullscreen mode
  • Array.zeroCreate
let arrayOfTenZeroes : int array = Array.zeroCreate 5 // [| 0; 0; 0; 0; 0 |]
Enter fullscreen mode Exit fullscreen mode

Get element of Array

  • Using index
let array0 = Array.init 5 (fun i -> i)

printfn "first element %A" array0.[0] // first element 0
Enter fullscreen mode Exit fullscreen mode
  • Using slice
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
Enter fullscreen mode Exit fullscreen mode
  • Using build in method get
let inputs = [| "a"; "b"; "c" |]
let secondElement = Array.get inputs 1

printfn "secondElement: %s" secondElement // secondElement: b
Enter fullscreen mode Exit fullscreen mode

Loop an Array

  • for...in
let array1 = [| for i in 1 .. 5 -> i * i |]

for number in array1 do
    printfn "Number: %d" number
Enter fullscreen mode Exit fullscreen mode
  • Array.iter syntax: Array.iter action array
let array1 = [| for i in 1 .. 5 -> i * i |]

Array.iter (printfn "Number: %d") array1
Enter fullscreen mode Exit fullscreen mode

Modify element of Array

  • Using index to set value
let inputs = [| "a"; "b"; "c" |]
inputs.[0] <- "d"

printfn "inputs: %A" inputs // d,b,c
Enter fullscreen mode Exit fullscreen mode
  • Using build in method set syntax: Array.set array index value
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 |]
Enter fullscreen mode Exit fullscreen mode

Pattern Matching with Array

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
Enter fullscreen mode Exit fullscreen mode

Operate Array

  • Concat Builds a new array that contains the elements of each of the given sequence of arrays Array.concat arrays
let inputs = [ [| 1; 2 |]; [| 3 |]; [| 4; 5 |] ]
let newArray = inputs |> Array.concat

printfn "newArray: %A" newArray
Enter fullscreen mode Exit fullscreen mode
  • Append 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
let results = Array.append [| 1; 2 |] [| 3; 4 |]

printfn "results: %A" results
Enter fullscreen mode Exit fullscreen mode
  • Filter
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
Enter fullscreen mode Exit fullscreen mode
  • Map
let originalArray = [| 1; 2; 3 |]
let doubleArray = Array.map (fun x -> 2 * x) originalArray

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

printfn "sum: %i" sum // sum: 15
Enter fullscreen mode Exit fullscreen mode

Top comments (0)