Seq
In F# we use the Seq
module on elements that are in IEnumerable
collections. With seq
we generate things as needed (lazily).
With helpful methods, like sum, we count up the values of a collection. And Seq.where
is more complex. It receives a Predicate
, which we can specify with the fun keyword.
Here we introduce an int
list. With Seq.sum
we sum up the values in this collection—the sum of all the elements is 63.
Seq.sum
function uses a declarative style of programming. It accepts one argument, the sequence we want to sum up.let positions = [1; 2; 10; 20; 30] // Sum the elements in the list. let sum = Seq.sum positions printfn "%A" sum63
Here we add some complexity. We use the where method. We first create a list of ints that go from 0 to 10 inclusive. This list has 11 elements in it.
let positions = [0 .. 10] // Get elements that are greater than or equal to 5. let result = Seq.where (fun b -> (b >= 5)) positions printfn "%A" resultseq [5; 6; 7; 8; ...]
Seq
, yieldThis example uses a seq
construct to create a lazily-evaluated sequence. We use a for
-loop to create a series of numbers. We iterate over 10 past the start argument.
seq
, this is a useful feature: values are only generated as they are needed.// Compose doubledNumbers function with one argument. // ... The whitespace is important. let doubledNumbers start = seq { for start in start .. start + 10 do yield start } // Call the doubledNumbers function. let example = doubledNumbers 5 // Print the first few numbers of the sequence. printfn "%A" example // Print the entire sequence. for n in example do printfn "%A" nseq [5; 6; 7; 8; ...] 5 6 7 8 9 10 11 12 13 14 15
List
comprehensionWe can omit the seq
keyword when specifying a sequence comprehension. Inside square brackets, we specify a loop that yields the sequence's elements.
int
list."if
-not statement to test that each number is not even. Logically this yields odd numbers.// Use sequence comprehension to generate odd numbers. // ... Find odd numbers between 0 and 10 inclusive. let odds = [for number = 0 to 10 do if not (number % 2 = 0) then yield number] // Print results. printfn "%A" odds[1; 3; 5; 7; 9]
Seq.iter
, printfn
We can call a function repeatedly over each element in a sequence with Seq.iter
. This can help when we want to print out each element of an array or list.
let values = [|600; 601; 602|] // Use Seq.iter to print values in array. values |> Seq.iter (fun x -> printfn $"x is {x}")x is 600 x is 601 x is 602
When using seq
blocks, we must be careful with indentation. It is usually a good idea to line up lines at the same indent level as the previous line.
Warning FS0058: Possible incorrect indentation: this token is offside of context started at position. Try indenting this token further or using standard formatting conventions.
The Seq.distinct
function removes duplicates from a list. This makes some code simpler. With Seq.distinctBy
we can transform elements before making them distinct.
With Seq
, a module, we call functions on sequences like collections. We pass functions (lambda expressions) to some of the methods like where. This is powerful and expressive code.