Expand Minimize

Seq.truncate<'T> Function (F#)

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

count

Type: int

The maximum number of items to enumerate.

source

Type: seq<'T>

The input sequence.

Exception

Condition

ArgumentNullException

Thrown when the input sequence is null.

The result sequence.

This function is named Truncate in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

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

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft