Array.fold2<'T1,'T2,'State> 函数 (F#)

更新:2010 年 8 月

按从左到右的顺序将函数应用于从两个集合中提取的元素对,并在整个计算过程中使用一个累加器参数。 两个输入数组的长度必须相同,否则将引发 ArgumentException

命名空间/模块路径: Microsoft.FSharp.Collections.Array

程序集:FSharp.Core(在 FSharp.Core.dll 中)

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

// Usage:
Array.fold2 folder state array1 array2

参数

  • folder
    类型:'State -> 'T1 -> 'T2 -> 'State

    要更新为输入元素指定的状态的函数。

  • state
    类型:'State

    初始状态。

  • array1
    类型:'T1 []

    第一个输入数组。

  • array2
    类型:'T2 []

    第二个输入数组。

异常

异常

Condition

ArgumentException

在输入数组的长度不同时引发。

返回值

最终状态。

备注

此函数在编译的程序集中名为 Fold2。 如果从 F# 以外的语言中访问函数,或通过反射访问成员,请使用此名称。

示例

下面的代码演示如何使用 Array.fold2

// Use Array.fold2 to perform computations over two arrays (of equal size)
// at the same time.
// Example: Add the greater element at each array position.
let sumGreatest array1 array2 =
    Array.fold2 (fun acc elem1 elem2 ->
        acc + max elem1 elem2) 0 array1 array2

let sum = sumGreatest [| 1; 2; 3 |] [| 3; 2; 1 |]
printfn "The sum of the greater of each pair of elements in the two arrays is %d." sum

Output

  

平台

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# 运行时

受以下版本支持:2.0、4.0

Silverlight

受以下版本支持:3

请参见

参考

Collections.Array 模块 (F#)

Microsoft.FSharp.Collections 命名空间 (F#)

修订记录

Date

修订记录

原因

2010 年 8 月

添加了代码示例。

信息补充。