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 |]
the ; could be omit between elements if you write them in seperated lines
let array1 =
[|
1
2
3
|]
all elements should be the same type
let array1 = [| 1; 2; "3"|]
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 |]
- Array.Empty Returns an empty array of the given type
let myEmptyArray = Array.empty // Evaluates to [| |]
- 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 |]
- 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 |]
- Array.zeroCreate
let arrayOfTenZeroes : int array = Array.zeroCreate 5 // [| 0; 0; 0; 0; 0 |]
Get element of Array
- Using index
let array0 = Array.init 5 (fun i -> i)
printfn "first element %A" array0.[0] // first element 0
- 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
- Using build in method get
let inputs = [| "a"; "b"; "c" |]
let secondElement = Array.get inputs 1
printfn "secondElement: %s" secondElement // secondElement: b
Loop an Array
- for...in
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
let array1 = [| for i in 1 .. 5 -> i * i |]
Array.iter (printfn "Number: %d") array1
Modify element of Array
- Using index to set value
let inputs = [| "a"; "b"; "c" |]
inputs.[0] <- "d"
printfn "inputs: %A" inputs // d,b,c
- 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 |]
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
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
- 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
- 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
- Map
let originalArray = [| 1; 2; 3 |]
let doubleArray = Array.map (fun x -> 2 * x) originalArray
printfn "doubleArray: %A" doubleArray // doubleArray: 2,4,6
- 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
Top comments (0)