Partager via


Option.foldBack<'T,'State> Function (F#)

Performs the equivalent of the List.foldBack operation on an option.

Namespace/Module Path: Microsoft.FSharp.Core.Option

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

// Signature:
foldBack : ('T -> 'State -> 'State) -> 'T option -> 'State -> 'State

// Usage:
foldBack folder option state

Parameters

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

    A function to update the state data when given a value from an option.

  • option
    Type: 'Toption

    The input option.

  • state
    Type: 'State

    The initial state.

Return Value

If the option is None, it returns the initial value of state. Otherwise, it returns the updated state, the result of applying the folder function with the option value and the initial state.

Remarks

The expression fold f inp s evaluates to match inp with None -> s | Some x -> f x s.

This function is named FoldBack 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 illustrates the use of Option.foldBack.

let consOption list opt =
    Option.foldBack (fun value state -> value :: state) list opt
printfn "%A" <| consOption None [ 1 .. 10 ]
printfn "%A" <| consOption (Some(0)) [1 .. 10] 

Output

[1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
[0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

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

Reference

Core.Option Module (F#)

Microsoft.FSharp.Core Namespace (F#)