Share via


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

Returns the elements of the sequence up to a specified count.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
Seq.take : int -> seq<'T> -> seq<'T>

// Usage:
Seq.take count source

Parameters

  • count
    Type: int

    The number of items to take.

  • source
    Type: seq<'T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input sequence is empty.

ArgumentNullException

Thrown when the input sequence is null.

InvalidOperationException

Thrown when count exceeds the number of elements in the sequence.

Return Value

The result sequence.

Remarks

Seq.truncate returns as many items as the sequence contains instead of throwing an exception.

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

Example

The following example demonstrates the use of Seq.take and contrasts the behavior with Seq.truncate.

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

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)