Udostępnij za pośrednictwem


Array.fold2<'T1,'T2,'State> — Funkcja (F#)

Dotyczy funkcji pary elementów z dwóch zbiorów od lewej do prawej, threading argumentu akumulator za pomocą obliczeń.Dwa wejściowe tablice muszą mieć tej samej długości, inaczej ArgumentException jest uruchamiany.

Ścieżka obszaru nazw/modułu: Microsoft.FSharp.Collections.Array

Zestaw: FSharp.Core (w FSharp.Core.dll)

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

// Usage:
Array.fold2 folder state array1 array2

Parametry

  • folder
    Typ:'State -> 'T1 -> 'T2 -> 'State

    Funkcja aktualizacji stanu dane wejściowe elementy.

  • state
    Typ:'State

    Stan początkowy.

  • array1
    Type: 'T1[]

    Pierwsza tablica wejściowego.

  • array2
    Type: 'T2[]

    Wejściowy drugiej tablicy.

Wyjątki

Wyjątek

Warunek

ArgumentException

Zgłoszony podczas wejściowe tablice różnią się długością.

Wartość zwracana

Stan końcowy.

Uwagi

Ta funkcja o nazwie Fold2 w skompilowane zestawy.Jeżeli języka, niż F# lub przez odbicie, uzyskują dostęp do funkcji, należy użyć tej nazwy.

Przykład

Poniższy kod pokazuje, jak używać 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

Dane wyjściowe

  

Platformy

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

Informacje o wersji

F# Core wersji biblioteki

Obsługiwane: 2.0, 4.0, przenośne

Zobacz też

Informacje

Collections.Array — Moduł (F#)

Microsoft.FSharp.Collections — Przestrzeń nazw (F#)