Fold function

Warning

This documentation refers to the Classic QDK, which has been replaced by the Modern QDK.

Please see https://aka.ms/qdk.api for the API documentation for the Modern QDK.

Namespace: Microsoft.Quantum.Arrays

Package: Microsoft.Quantum.Standard

Iterates a function f through an array array, returning f(...f(f(initialState, array[0]), array[1]), ...).

function Fold<'State, 'T> (folder : (('State, 'T) -> 'State), state : 'State, array : 'T[]) : 'State

Input

folder : ('State,'T) -> 'State

A function to be folded over the array.

state : 'State

The initial state of the folder.

array : 'T[]

An array of values to be folded over.

Output : 'State

The final state returned by the folder after iterating over all elements of array.

Type Parameters

'State

The type of states the folder function operates on, i.e., accepts as its first argument and returns.

'T

The type of array elements.

Example

function Plus(a : Double, b : Double) {
    return a + b;
}
function Sum(xs : Double[]) {
    return Fold(Plus, 0.0, xs);
}