Seq.truncate<'T> Function (F#)
Visual Studio 2012
Returns a sequence that when enumerated returns at most N elements.
Namespace/Module Path: Microsoft.FSharp.Collections.Seq
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature: Seq.truncate : int -> seq<'T> -> seq<'T> // Usage: Seq.truncate count source
The following example demonstrates the use of Seq.truncate and contrasts the behavior with Seq.take.
let mySeq = seq { for i in 1 .. 10 -> i*i } let truncatedSeq = Seq.truncate 5 mySeq let takenSeq = Seq.take 5 mySeq let truncatedSeq2 = Seq.truncate 20 mySeq let takenSeq2 = Seq.take 20 mySeq let printSeq seq1 = Seq.iter (printf "%A ") seq1; printfn "" // Up to this point, the sequences are not evaluated. // The following code causes the sequences to be evaluated. truncatedSeq |> printSeq truncatedSeq2 |> printSeq takenSeq |> printSeq // The following line produces a run-time error (in printSeq): takenSeq2 |> printSeq
1 4 9 16 25 1 4 9 16 25 36 49 64 81 100 1 4 9 16 25 1 4 9 16 25 36 49 64 81 100