Expand Minimize
This topic has not yet been rated - Rate this topic

Array.foldBack2<'T1,'T2,'State> Function (F#)

Apply a function to pairs of elements drawn from the two collections, right-to-left, threading an accumulator argument through the computation. The two input arrays must have the same lengths, otherwise an ArgumentException is raised.

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

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

// Signature:
Array.foldBack2 : ('T1 -> 'T2 -> 'State -> 'State) -> 'T1 [] -> 'T2 [] -> 'State -> 'State

// Usage:
Array.foldBack2 folder array1 array2 state
folder

Type: 'T1 -> 'T2 -> 'State -> 'State

The function to update the state given the input elements.

array1

Type: 'T1 []

The first input array.

array2

Type: 'T2 []

The second input array.

state

Type: 'State

The initial state.

Exception

Condition

ArgumentException

Thrown when the input arrays differ in length.

The final state.

This function is named FoldBack2 in the .NET assembly. If accessing the member from a .NET language other than F#, or through reflection, use this name.

The following code shows how to use Array.foldBack2.

type Transaction =
    | Deposit
    | Withdrawal

let transactionTypes = [| Deposit; Deposit; Withdrawal |]
let transactionAmounts = [| 100.00; 1000.00; 95.00 |]
let initialBalance = 200.00

let endingBalance = Array.foldBack2 (fun elem1 elem2 acc ->
                        match elem1 with
                        | Deposit -> acc + elem2
                        | Withdrawal -> acc - elem2)
                        transactionTypes
                        transactionAmounts
                        initialBalance
printfn "Ending balance: $%.2f" endingBalance

Output

Ending balance: $1205.00

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.