F# by example
Toggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Seq Module

The Seq module, however, is a built-in module in F# that provides a collection of functions for working with sequences. These functions are designed to manipulate sequences efficiently and provide a functional programming approach to sequence operations. It offers a wide range of functions to perform common operations on sequences, such as filtering, mapping, folding, sorting, and more. These functions are designed to be used with the seq<‘T> type, which represents sequences in F#.

Considering the sequence:

let mySequence = seq { 1 .. 10 }

map

Mapping allows us to transform each element of a sequence into another value using a given function. F# provides the map function in the Seq module for this purpose. Let’s illustrate this with an example:

 let squaredNumbers = Seq.map (fun x -> x * x) mySequence

Here, we use the map function to create a new sequence squaredNumbers by squaring each element of mySequence.

filter

The Seq module provides various functions to filter sequences based on certain conditions. One such function is filter. The filter function takes a predicate and returns a new sequence that contains only the elements that satisfy the predicate. Let’s see an example:

let evenNumbers = Seq.filter (fun x -> x % 2 = 0) mySequence

In this example, we use the filter function to create a new sequence evenNumbers that contains only the even numbers from mySequence.

fold

Folding, also known as reduction, is a common operation that combines all the elements of a sequence into a single value. The fold and foldBack functions in the Seq module allow us to perform folding operations. Let’s consider an example:

let sum = Seq.fold (fun acc x -> acc + x) 0 mySequence

In this example, we use the fold function to calculate the sum of all elements in mySequence. The initial value 0 is provided as the starting point for the accumulation, and the function fun acc x -> acc + x defines how to combine the accumulator acc with each element x of the sequence.

take

The take function allows us to take a specified number of elements from the beginning of a sequence. Let’s see an example:

let firstThree = Seq.take 3 mySequence

In this example, we use the take function to create a new sequence firstThree that contains the first three elements from mySequence.

skip

The skip function allows us to skip a specified number of elements from the beginning of a sequence and return the remaining elements. Let’s see an example:

let remainingSeven = Seq.skip 3 mySequence

In this example, we use the skip function to create a new sequence remainingSeven that contains the remaining seven elements from mySequence after skipping the first three elements.

sortBy

The sortBy function enables sorting a sequence based on a specified order. Let’s see an example:

let sortedNumbers = Seq.sortBy (fun x -> -x) mySequence

In this example, we use the sortBy function to create a new sequence sortedNumbers that contains the elements of mySequence sorted in descending order.

sortWith

The sortWith function enables sorting a sequence based on a custom comparison function. Let’s see an example:

let sortedNumbers = Seq.sortWith (fun x y -> compare y x) mySequence

In this example, we use the sortWith function to create a new sequence sortedNumbers that contains the elements of mySequence sorted in descending order.

exists

The exists function checks whether any element in the sequence satisfies a given predicate. It returns true if at least one element matches the condition, otherwise false. Let’s see an example:

let hasEvenNumber = Seq.exists (fun x -> x % 2 = 0) mySequence

In this example, we use the exists function to check if mySequence contains any even number.

forAll

The forAll function checks if all elements in the sequence satisfy a given predicate. It returns true if every element matches the condition, otherwise false. Let’s see an example:

let allEvenNumbers = Seq.forAll (fun x -> x % 2 = 0) mySequence

In this example, we use the forAll function to check if all numbers in mySequence are even.