Arrays in F#
Contents
Arrays are one of the basic data structures in F#. In this article we’re going to see an introduction of what can we do with them.
Creation
There are several ways to create an array in F#
Create from a literal
We can create an array with a predefined set of values. To do that, we just need to specify the values separated by semicolons and wrapped between [|
and |]
|
|
Create a range
We can create an array of predifined values using the range notation:
|
|
In the previous code, we are creating an array of numbers between 100 and 120.
Wan can specify the gap between those numbers:
|
|
And we can use an expression inside the brackets too:
|
|
Create from a function in the Array module
We can create an array using the Array.create function. This function takes two parameters, the number of positions you want to create and the value you want to use.
|
|
Another function we can use is init, which is very similar to create but instead of taking the value it takes a function to create the different values
|
|
We can also use zeroCreate to create an array filled with zeros
|
|
Finally we can create an array from other array or IEnumerable
|
|
Accessing elements in an Array
It’s easy to access an element in an Array. Just use the following notation:
|
|
You must take into account that arrays are 0 based.
Operations in Array module
There are more than 70 functions in the Array module. Let’s see some of the most used.
Array.map
Takes an array and returns another array of the same length with the result of applying a function to each element.
|
|
Array.mapi
Is very similar to Array.map but it provides the index of each element.
|
|
Array.iter
Iterates and call a function with each element, but it doesn’t returns anything (only has side effects). We can user Array.iteri if we need the index.
|
|
Array.filter
Given an array only returns those elements on which the function applied returns true.
|
|
Array.choose
Given an array only returns those elements on wich the function applied returns a ‘Some’ result. So, the function applied must return an option type.
|
|
Array.sum
Sum the values of the array. The type of the array must support addition and must have a zero member.
|
|
Array.sumBy
Same as sum but takes a function that select the element to sum.
Let’s start the example defining a function to get random strings
|
|
Now, create some random strings
|
|
And finally, sum the length of those strings
|
|
Array.sort
Given an array, returns the array sorted by the element. If we use sortBy, we can specify a function to be used to sort
|
|
Array.reduce
Given an array, uses the supplied function to calculate a value that is used as accumulator for the next calculation. Throws an exception in an empty input list.
|
|
Array.fold
Same as reduce, but takes as a parameter the first value of the accumulator.
|
|
Array.scan
Like fold, but returns each intermediate result
|
|
Array.zip
Takes two arrays of the same size and produce another array of the same size with tuples of elements from each input array.
|
|
There’s a very similar function called zip3, wich take three array as inputs, and another call unzip (and unzip3) with takes an array of tuples and decomposes it in two arrays of single values.
Summary
We’ve seen the basics of the Array module. We’ve seen how to create arrays and some of the most used functions in the Array module.
Author Vicenç García
LastMod 01-01-0001