Table of contents
TOC
Collapse the table of content
Expand the table of content

Seq.fold<'T,'State> Function (F#)

Den Delimarsky|Last Updated: 7/25/2016
|
1 Contributor

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.

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

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

Syntax

// Signature:
Seq.fold : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State

// Usage:
Seq.fold folder state source

Parameters

folder Type: 'State -> 'T -> 'State

A function that updates the state with each element from the sequence.

state Type: 'State

The initial state.

source Type: seq<'T>

The input sequence.

Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when the input sequence is null.

Return Value

The final result of the computation.

Remarks

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

Example

The following code shows how to use Seq.fold to implement a function that computes the sum of the elements of a sequence.


    let sumSeq sequence1 = Seq.fold (fun acc elem -> acc + elem) 0 sequence1
    Seq.init 10 (fun index -> index * index)
    |> sumSeq
    |> printfn "The sum of the elements is %d."

Output

The sum of the elements is 285.

Platforms

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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2016 Microsoft