Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ F# For Dummys - Day 13 Collections Array


๐Ÿ“š F# For Dummys - Day 13 Collections Array


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

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
...



๐Ÿ“Œ [dos] Microsoft Edge Chakra JIT - 'Array.prototype.reverse' Array Type Confusion


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ #0daytoday #Microsoft Edge Chakra JIT - Array.prototype.reverse Array Type Confusion Exploit [#0day #Exploit]


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ FFmpeg Array Access MXF File Out-of-Array Pufferรผberlauf


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ FFmpeg Array Access MXF File Out-of-Array memory corruption


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Learn the JavaScript Array.every() and Array.some() methods


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Bash For Loop Array: Iterate Through Array Values


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Array.from VS Array.prototype.map


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Maximize Array sum by adding multiple of another Array element in given ranges


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Convert Array of Starings to Array of Numbers in JavaScript


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Print the sum of array after doing k queries on the array


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ How to pass array in another array of objects?


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Convert 1d Array to 2d Array Python


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ How to extract value of a property as array from an array of objects ?


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ JavaScript Sort Array - How to Sort an Array Accurately


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ How to Create an Array in Java โ€“ Array Declaration Example


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ ES6 Map an Array of Objects to Return an Array of Objects With New Keys


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ JavaScript Array Tutorial โ€“ Array Methods in JS


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ JS Sum of an Array โ€“ How to Add the Numbers in a JavaScript Array


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Generate an Array from given Array according to given conditions


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ JavaScript Array Length โ€“ How to Find the Length of an Array in JS


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Array / Array Method / Function


๐Ÿ“ˆ 22.69 Punkte

๐Ÿ“Œ Belden stellt "Industrielle Cybersicherheit fรผr Dummys" vor


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ Fotos zeigen 6,5 Zoll iPhone X Plus und 6,1 Zoll LCD-iPhone (Dummys)


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ Belden stellt "Time-Sensitive Networking fรผr Dummys" vor


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ DDR4-Module ohne Speicher - Corsair bietet RGB-beleuchtete Dummys an


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 11: Dummys der neuen iPhone-Generation im Video


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ Fotos der Dummys zeigen, wie die neuen iPhones aussehen (kรถnnten)


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 11: Dummys der neuen iPhone-Generation im Video


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone-12-Dummys: SIM-Karten-Slot wechselt auf die andere Seite


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 12 im iPad Pro-Design: CAD-Zeichnungen und Dummys zeigen klare Kante


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 11: Detaillierte Dummys zeigen neue Modelle in voller "Pracht"


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 12: Dummys zeigen, was sich bei Apple รคndert und was nicht


๐Ÿ“ˆ 21.08 Punkte

๐Ÿ“Œ iPhone 12 im iPad Pro-Design: CAD-Zeichnungen und Dummys zeigen klare Kante


๐Ÿ“ˆ 21.08 Punkte











matomo