Fold (higher-order Function)

Fold (higher-order Function)

In functional programming, fold – also known variously as reduce, accumulate, compress, or inject – are a family of higher-order functions that analyze a recursive data structure and recombine through use of a given combining operation the results of recursively processing its constituent parts, building up a return value. Typically, a fold is presented with a combining function, a top node of a data structure, and possibly some default values to be used under certain conditions. The fold then proceeds to combine elements of the data structure's hierarchy, using the function in a systematic way.

Folds are in a sense dual to unfolds, which take a "seed" value and apply a function corecursively to decide how to progressively construct a corecursive data structure, whereas a fold recursively breaks that structure down, replacing it with the results of applying a combining function at each node on its terminal values and the recursive results (catamorphism as opposed to anamorphism of unfolds).

Read more about Fold (higher-order Function):  Folds As Structural Transformations, Folds On Lists, Folds in Various Languages, Universality

Famous quotes containing the word fold:

    When the inhabitants of some sequestered island first descry the “big canoe” of the European rolling through the blue waters towards their shores, they rush down to the beach in crowds, and with open arms stand ready to embrace the strangers. Fatal embrace! They fold to their bosoms the vipers whose sting is destined to poison all their joys; and the instinctive feeling of love within their breasts is soon converted into the bitterest hate.
    Herman Melville (1819–1891)