Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.