F# Sequences, like lists, also represent an ordered collection of values. However, the elements in a sequence or sequence expression are computed when required. They are not computed at once, and for this reason, they are used to represent infinite data structures.
Defining F# Sequences
Sequences are defined using the following syntax β
seq { expr }
For example,
let seq1 = seq { 1 .. 10 }
Creating Sequences and Sequences Expressions
Similar to lists, you can create sequences using ranges and comprehensions.
Sequence expressions are the expressions you can write for creating sequences. These can be done β
- By specifying the range.
- By specifying the range with increment or decrement.
- By using the yield keyword to produce values that become part of the sequence.
- By using the β operator.
The following examples demonstrate the concept β
Example 1
(* Sequences *) let seq1 = seq { 1 .. 10 } (* ascending order and increment*) printfn "The Sequence: %A" seq1 let seq2 = seq { 1 .. 5 .. 50 } (* descending order and decrement*) printfn "The Sequence: %A" seq2 let seq3 = seq {50 .. -5 .. 0} printfn "The Sequence: %A" seq3 (* using yield *) let seq4 = seq { for a in 1 .. 10 do yield a, a*a, a*a*a } printfn "The Sequence: %A" seq4
When you compile and execute the program, it yields the following output β
The Sequence: seq [1; 2; 3; 4; ...] The Sequence: seq [1; 6; 11; 16; ...] The Sequence: seq [50; 45; 40; 35; ...] The Sequence: seq [(1, 1, 1); (2, 4, 8); (3, 9, 27); (4, 16, 64); ...]
Example 2
The following program prints the prime numbers from 1 to 50 β
(* Recursive isprime function. *) let isprime n = let rec check i = i > n/2 || (n % i <> 0 && check (i + 1)) check 2 let primeIn50 = seq { for n in 1..50 do if isprime n then yield n } for x in primeIn50 do printfn "%d" x
When you compile and execute the program, it yields the following output β
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Basic Operations on Sequence
The following table shows the basic operations on sequence data type β
Value | Description |
---|---|
append : seq<‘T> β seq<‘T> β seq<‘T> | Wraps the two given enumerations as a single concatenated enumeration. |
average: seq<^T> β ^T | Returns the average of the elements in the sequence. |
averageBy : (‘T β ^U) β seq<‘T> β ^U | Returns the average of the results generated by applying the function to each element of the sequence. |
cache: seq<‘T> β seq<‘T> | Returns a sequence that corresponds to a cached version of the input sequence. |
cast: IEnumerable β seq<‘T> | Wraps a loosely-typed System. Collections sequence as a typed sequence. |
choose : (‘T β ‘U option) β seq<‘T> β seq<‘U> | Applies the given function to each element of the list. Return the list comprised of the results for each element where the function returns Some. |
collect : (‘T β ‘Collection) β seq<‘T> β seq<‘U> | Applies the given function to each element of the sequence and concatenates all the results. |
compareWith : (‘T β ‘T β int) β seq<‘T> β seq<‘T> β int | Compares two sequences using the given comparison function, element by element. |
concat : seq<‘Collection> β seq<‘T> | Combines the given enumeration-of-enumerations as a single concatenated enumeration. |
countBy : (‘T β ‘Key) β seq<‘T> β seq<‘Key * int> | Applies a key-generating function to each element of a sequence and returns a sequence yielding unique keys and their number of occurrences in the original sequence. |
delay : (unit β seq<‘T>) β seq<‘T> | Returns a sequence that is built from the given delayed specification of a sequence. |
distinct : seq<‘T> β seq<‘T> | Returns a sequence that contains no duplicate entries according to generic hash and equality comparisons on the entries. If an element occurs multiple times in the sequence then the later occurrences are discarded. |
distinctBy : (‘T β ‘Key) β seq<‘T> β seq<‘T> | Returns a sequence that contains no duplicate entries according to the generic hash and equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded. |
empty : seq<‘T> | Creates an empty sequence. |
exactly one: seq<‘T> β ‘T | Returns the only element of the sequence. |
exists : (‘T β bool) β seq<‘T> β bool | Tests if any element of the sequence satisfies the given predicate. |
exists2 : (‘T1 β ‘T2 β bool) β seq<‘T1> β seq<‘T2> β bool | Tests if any pair of corresponding elements of the input sequences satisfies the given predicate. |
filter : (‘T β bool) β seq<‘T> β seq<‘T> | Returns a new collection containing only the elements of the collection for which the given predicate returns true. |
find : (‘T β bool) β seq<‘T> β ‘T | Returns the first element for which the given function returns true. |
findIndex : (‘T β bool) β seq<‘T> β int | Returns the index of the first element for which the given function returns true. |
fold : (‘State β ‘T β ‘State) β ‘State β seq<‘T> β ‘State | Applies a function to each element of the collection, threading an accumulator argument through the computation. If the input function is f and the elements are i0…iN, then this function computes f (… (f s i0)…) iN. |
forall : (‘T β bool) β seq<‘T> β bool | Tests if all elements of the sequence satisfy the given predicate. |
forall2 : (‘T1 β ‘T2 β bool) β seq<‘T1> β seq<‘T2> β bool | Tests all pairs of elements drawn from the two sequences satisfy the given predicate. If one sequence is shorter than the other then the remaining elements of the longer sequence are ignored. |
groupBy : (‘T β ‘Key) β seq<‘T> β seq<‘Key * seq<‘T>> | Applies a key-generating function to each element of a sequence and yields a sequence of unique keys. Each unique key has also contained a sequence of all elements that match this key. |
head : seq<‘T> β ‘T | Returns the first element of the sequence. |
init: int β (int β ‘T) β seq<‘T> | Generates a new sequence which, when iterated, returns successive elements by calling the given function, up to the given count. The results of calling the function are not saved, that is, the function is reapplied as necessary to regenerate the elements. The function is passed the index of the item being generated. |
initInfinite : (int β ‘T) β seq<‘T> | Generates a new sequence which, when iterated, will return successive elements by calling the given function. The results of calling the function are not saved, that is, the function will be reapplied as necessary to regenerate the elements. The function is passed the index of the item being generated. |
isEmpty : seq<‘T> β bool | Tests whether a sequence has any elements. |
iter : (‘T β unit) β seq<‘T> β unit | Applies the given function to each element of the collection. |
iter2 : (‘T1 β ‘T2 β unit) β seq<‘T1> β seq<‘T2> β unit | Applies the given function to two collections simultaneously. If one sequence is shorter than the other then the remaining elements of the longer sequence are ignored. |
iteri : (int β ‘T β unit) β seq<‘T> β unit | Applies the given function to each element of the collection. The integer passed to the function indicates the index of the element. |
last : seq<‘T> β ‘T | Returns the last element of the sequence. |
length : seq<‘T> β int | Returns the length of the sequence. |
map : (‘T β ‘U) β seq<‘T> β seq<‘U> | Creates a new collection whose elements are the results of applying the given function to each of the elements of the collection. The given function will be applied as elements are demanded using the MoveNext method on enumerators retrieved from the object. |
map2 : (‘T1 β ‘T2 β ‘U) β seq<‘T1> β seq<‘T2> β seq<‘U> | Creates a new collection whose elements are the results of applying the given function to the corresponding pairs of elements from the two sequences. If one input sequence is shorter than the other then the remaining elements of the longer sequence are ignored. |
mapi : (int β ‘T β ‘U) β seq<‘T> β seq<‘U> | Creates a new collection whose elements are the results of applying the given function to each of the elements of the collection. The integer index passed to the function indicates the index (from 0) of an element being transformed. |
max : seq<‘T> β ‘T | Returns the greatest of all elements of the sequence, compared by using Operators. max. |
maxBy : (‘T β ‘U) β seq<‘T> β ‘T | Returns the greatest of all elements of the sequence, compared by using Operators. max on the function result. |
min : seq<‘T> β ‘T | Returns the lowest of all elements of the sequence, compared by using Operators. min. |
minBy : (‘T β ‘U) β seq<‘T> β ‘T | Returns the lowest of all elements of the sequence, compared by using Operators. min on the function result. |
nth : int β seq<‘T> β ‘T | Computes the nth element in the collection. |
ofArray : ‘T array β seq<‘T> | Views the given array as a sequence. |
ofList : ‘T list β seq<‘T> | Views the given list as a sequence. |
pairwise : seq<‘T> β seq<‘T * ‘T> | Returns a sequence of each element in the input sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element. |
pick : (‘T β ‘U option) β seq<‘T> β ‘U | Applies the given function to successive elements, returning the first value where the function returns a Some value. |
readonly: seq<‘T> β seq<‘T> | Creates a new sequence object that delegates to the given sequence object. This ensures the original sequence cannot be rediscovered and mutated by a typecast. For example, if given an array the returned sequence will return the elements of the array, but you cannot cast the returned sequence object to an array. |
reduce : (‘T β ‘T β ‘T) β seq<‘T> β ‘T | Applies a function to each element of the sequence, threading an accumulator argument through the computation. Begin by applying the function to the first two elements. Then feed this result into the function along with the third element and so on. Return the final result. |
scan : (‘State β ‘T β ‘State) β ‘State β seq<‘T> β seq<‘State> | Like Seq. fold, but computes on-demand and returns the sequence of intermediary and final results. |
singleton: ‘T β seq<‘T> | Returns a sequence that yields one item only. |
skip: int β seq<‘T> β seq<‘T> | Returns a sequence that skips a specified number of elements of the underlying sequence and then yields the remaining elements of the sequence. |
skipWhile : (‘T β bool) β seq<‘T> β seq<‘T> | Returns a sequence that, when iterated, skips elements of the underlying sequence while the given predicate returns true, and then yields the remaining elements of the sequence. |
sort : seq<‘T> β seq<‘T> | Yields a sequence ordered by keys. |
sortBy : (‘T β ‘Key) β seq<‘T> β seq<‘T> | Applies a key-generating function to each element of a sequence and yields a sequence ordered by keys. The keys are compared using generic comparison as implemented by Operators. compare. |
sum: seq<^T> β ^T | Returns the sum of the elements in the sequence. |
sumBy | Returns the sum of the results generated by applying the function to each element of the sequence. |
take: int β seq<‘T> β seq<‘T> | Returns the first elements of the sequence up to a specified count. |
takeWhile : (‘T β bool) β seq<‘T> β seq<‘T> | Returns a sequence that, when iterated, yields elements of the underlying sequence while the given predicate returns true, and then returns no further elements. |
toArray : seq<‘T> β ‘T[] | Creates an array from the given collection. |
toList : seq<‘T> β ‘T list | Creates a list from the given collection. |
truncate : int β seq<‘T> β seq<‘T> | Returns a sequence that when enumerated returns no more than a specified number of elements. |
tryFind : (‘T β bool) β seq<‘T> β ‘T option | Returns the first element for which the given function returns true, or None if no such element exists. |
tryFindIndex : (‘T β bool) β seq<‘T> β int option | Returns the index of the first element in the sequence that satisfies the given predicate, or None if no such element exists. |
tryPick : (‘T β ‘U option) β seq<‘T> β ‘U option | Applies the given function to successive elements, returning the first value where the function returns a Some value. |
unfold : (‘State β ‘T * ‘State option) β ‘State β seq<‘T> | Returns a sequence that contains the elements generated by the given computation. |
where : (‘T β bool) β seq<‘T> β seq<‘T> | Returns a new collection containing only the elements of the collection for which the given predicate returnsΒ true. A synonym for Seq. filter. |
windowed : int β seq<‘T> β seq<‘T []> | Returns a sequence that yields sliding windows containing elements drawn from the input sequence. Each window is returned as a fresh array. |
zip : seq<‘T1> β seq<‘T2> β seq<‘T1 * ‘T2> | Combines the two sequences into a list of pairs. The two sequences need not have equal lengths β when one sequence is exhausted any remaining elements in the other sequence are ignored. |
zip3 : seq<‘T1> β seq<‘T2> β seq<‘T3> β seq<‘T1 * ‘T2 * ‘T3> | Combines the three sequences into a list of triples. The sequences need not have equal lengths β when one sequence is exhausted any remaining elements in the other sequences are ignored. |
The following examples demonstrate the uses of some of the above functionalities β
Example 1
This program creates an empty sequence and fills it up later β
(* Creating sequences *) let emptySeq = Seq.empty let seq1 = Seq.singleton 20 printfn"The singleton sequence:" printfn "%A " seq1 printfn"The init sequence:" let seq2 = Seq.init 5 (fun n -> n * 3) Seq.iter (fun i -> printf "%d " i) seq2 printfn"" (* converting an array to sequence by using cast *) printfn"The array sequence 1:" let seq3 = [| 1 .. 10 |] :> seq<int> Seq.iter (fun i -> printf "%d " i) seq3 printfn"" (* converting an array to sequence by using Seq.ofArray *) printfn"The array sequence 2:" let seq4 = [| 2..2.. 20 |] |> Seq.ofArray Seq.iter (fun i -> printf "%d " i) seq4 printfn""
When you compile and execute the program, it yields the following output β
The singleton sequence: seq [20] The init sequence: 0 3 6 9 12 The array sequence 1: 1 2 3 4 5 6 7 8 9 10 The array sequence 2: 2 4 6 8 10 12 14 16 18 20
Please note that β
- The Seq.empty method creates an empty sequence.
- The Seq.singleton method creates a sequence of just one specified element.
- The Seq.init method creates a sequence for which the elements are created by using a given function.
- The Seq.ofArray and Seq.ofList<‘T> methods create sequences from arrays and lists.
- The Seq.iter method allows iterating through a sequence.
Example 2
The Seq. unfold method generates a sequence from a computation function that takes a state and transforms it to produce each subsequent element in the sequence.
The following function produces the first 20 natural numbers β
let seq1 = Seq.unfold (fun state -> if (state > 20) then None else Some(state, state + 1)) 0 printfn "The sequence seq1 contains numbers from 0 to 20." for x in seq1 do printf "%d " x printfn" "
When you compile and execute the program, it yields the following output β
The sequence seq1 contains numbers from 0 to 20. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Example 3
The Seq. truncate method creates a sequence from another sequence but limits the sequence to a specified number of elements.
The Seq. take method creates a new sequence that contains a specified number of elements from the start of a sequence
let mySeq = seq { for i in 1 .. 10 -> 3*i } let truncatedSeq = Seq.truncate 5 mySeq let takeSeq = Seq.take 5 mySeq printfn"The original sequence" Seq.iter (fun i -> printf "%d " i) mySeq printfn"" printfn"The truncated sequence" Seq.iter (fun i -> printf "%d " i) truncatedSeq printfn"" printfn"The take sequence" Seq.iter (fun i -> printf "%d " i) takeSeq printfn""
When you compile and execute the program, it yields the following output β
The original sequence 3 6 9 12 15 18 21 24 27 30 The truncated sequence 3 6 9 12 15 The take sequence 3 6 9 12 15
Next Topic – Click Here
Pingback: F# - Lists - Adglob Infosystem Pvt Ltd