# Foldable

``public protocol Foldable``

Foldable describes types that have the ability to be folded to a summary value.

• ``` foldLeft(_:_:_:) ```

Eagerly folds a value to a summary value from left to right.

#### Declaration

Swift

``````static func foldLeft<A, B>(
_ fa: Kind<Self, A>,
_ b: B,
_ f: @escaping (B, A) -> B) -> B``````

#### Parameters

 ``` fa ``` Value to be folded. ``` b ``` Initial value for the folding process. ``` f ``` Folding function.

#### Return Value

Summary value resulting from the folding process.

• ``` foldRight(_:_:_:) ```

Lazily folds a value to a summary value from right to left.

#### Declaration

Swift

``````static func foldRight<A, B>(
_ fa: Kind<Self, A>,
_ b: Eval<B>,
_ f: @escaping (A, Eval<B>) -> Eval<B>) -> Eval<B>``````

#### Parameters

 ``` fa ``` Value to be folded. ``` b ``` Initial value for the folding process. ``` f ``` Folding function.

#### Return Value

Summary value resulting from the folding process.

• ``` fold(_:) ``` Extension method

Folds a structure of values provided that its type has an instance of `Monoid`.

It uses the monoid empty value as initial value and the combination method for the fold.

#### Declaration

Swift

``static func fold<A>(_ fa: Kind<Self, A>) -> A where A : Monoid``

#### Parameters

 ``` fa ``` Value to be folded.

#### Return Value

Summary value resulting from the folding process.

• ``` reduceLeftToOption(_:_:_:) ``` Extension method

Reduces the elements of a structure down to a single value by applying the provided transformation and aggregation funtions in a left-associative manner.

#### Declaration

Swift

``````static func reduceLeftToOption<A, B>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> B,
_ g: @escaping (B, A) -> B) -> Option<B>``````

#### Parameters

 ``` fa ``` Value to be folded. ``` f ``` Transforming function. ``` g ``` Folding function.

#### Return Value

Optional summary value resulting from the folding process. It will be an `Option.none` if the structure is empty, or a value if not.

• ``` reduceRightToOption(_:_:_:) ``` Extension method

Reduces the elements of a structure down to a single value by applying the provided transformation and aggregation functions in a right-associative manner.

#### Declaration

Swift

``````static func reduceRightToOption<A, B>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> B,
_ g: @escaping (A, Eval<B>) -> Eval<B>) -> Eval<Option<B>>``````

#### Parameters

 ``` fa ``` Value to be folded. ``` f ``` Transforming function. ``` g ``` Folding function.

#### Return Value

Optional summary value resulting from the folding process. It will be an `Option.none` if the structure is empty, or a value if not.

• ``` reduceLeftOption(_:_:) ``` Extension method

Reduces the elements of a structure down to a single value by applying the provided aggregation function in a left-associative manner.

#### Declaration

Swift

``````static func reduceLeftOption<A>(
_ fa: Kind<Self, A>,
_ f: @escaping (A, A) -> A) -> Option<A>``````

#### Parameters

 ``` fa ``` Value to be folded. ``` f ``` Folding function.

#### Return Value

Optional summary value resulting from the folding process.

• ``` reduceRightOption(_:_:) ``` Extension method

Reduces the elements of a structure down to a single value by applying the provided aggregation function in a right-associative manner.

#### Declaration

Swift

``````static func reduceRightOption<A>(
_ fa: Kind<Self, A>,
_ f: @escaping (A, Eval<A>) -> Eval<A>) -> Eval<Option<A>>``````

#### Parameters

 ``` fa ``` Value to be folded. ``` f ``` Folding function.

#### Return Value

Optional summary value resulting from the folding process.

• ``` combineAll(_:) ``` Extension method

Folds a structure of values provided that its type has an instance of `Monoid`.

It uses the monoid empty value as initial value and the combination method for the fold.

#### Declaration

Swift

``static func combineAll<A>(_ fa: Kind<Self, A>) -> A where A : Monoid``

#### Parameters

 ``` fa ``` Value to be folded.

#### Return Value

Summary value resulting from the folding process.

• ``` foldMap(_:_:) ``` Extension method

Transforms the elements of a structure to a type with a `Monoid` instance and folds them using the empty and combine methods of such `Monoid` instance.

#### Declaration

Swift

``````static func foldMap<A, B: Monoid>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> B) -> B``````

#### Parameters

 ``` fa ``` Value to be transformed and folded. ``` f ``` Transforming function.

#### Return Value

Summary value resulting from the transformation and folding process.

• ``` traverse_(_:_:) ``` Extension method

Traverses a structure of values, transforming them with a provided function and discarding the result of its effect.

#### Declaration

Swift

``````static func traverse_<G: Applicative, A, B>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> Kind<G, B>) -> Kind<G, Unit>``````

#### Parameters

 ``` fa ``` Structure of values. ``` f ``` Transforming function.

#### Return Value

Unit in the context of the effect of the result of the transforming function.

• ``` sequence_(_:) ``` Extension method

Traverses a structure of effects, performing them and discarding their result.

#### Declaration

Swift

``````static func sequence_<G: Applicative, A, B>(_ fga: Kind<Self, B>) -> Kind<G, Unit>
where B: Kind<G, A>``````

#### Parameters

 ``` fga ``` Structure of effects.

#### Return Value

Unit in the context of the effects contained in the structure.

• ``` find(_:_:) ``` Extension method

Looks for an element that matches a given predicate.

#### Declaration

Swift

``````static func find<A>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> Bool) -> Option<A>``````

#### Parameters

 ``` fa ``` Structure of values where the element matching the predicate needs to be found. ``` f ``` Predicate.

#### Return Value

A value if there is any that matches the predicate, or `Option.none`.

• ``` exists(_:_:) ``` Extension method

Checks if any element in a structure matches a given predicate.

#### Declaration

Swift

``````static func exists<A>(
_ fa: Kind<Self, A>,
_ predicate: @escaping (A) -> Bool) -> Bool``````

#### Parameters

 ``` fa ``` Structure of values where the element matching the predicate needs to be found. ``` predicate ``` Predicate.

#### Return Value

A boolean value indicating if any elements in the structure match the predicate.

• ``` forall(_:_:) ``` Extension method

Checks if all elements in a structure match a given predicate.

#### Declaration

Swift

``````static func forall<A>(
_ fa: Kind<Self, A>,
_ predicate: @escaping (A) -> Bool) -> Bool``````

#### Parameters

 ``` fa ``` Structure of values where all elements should match the predicate. ``` predicate ``` Predicate.

#### Return Value

A boolean value indicating if all elements in the structure match the predicate.

• ``` isEmpty(_:) ``` Extension method

Checks if a structure of values is empty.

#### Declaration

Swift

``static func isEmpty<A>(_ fa: Kind<Self, A>) -> Bool``

#### Parameters

 ``` fa ``` Structure of values.

#### Return Value

`false` if the structure contains any value, `true` otherwise.

• ``` nonEmpty(_:) ``` Extension method

Checks if a structure of values is not empty.

#### Declaration

Swift

``static func nonEmpty<A>(_ fa: Kind<Self, A>) -> Bool``

#### Parameters

 ``` fa ``` Structure of values.

#### Return Value

`true` if the structure contains any value, `false` otherwise.

• ``` foldM(_:_:_:) ``` Extension method

Performs a monadic left fold from the source context to the target monad.

#### Declaration

Swift

``````static func foldM<G: Monad, A, B>(
_ fa: Kind<Self, A>,
_ b: B,
_ f: @escaping (B, A) -> Kind<G, B>) -> Kind<G, B>``````

#### Parameters

 ``` fa ``` Structure of values. ``` b ``` Initial value for the fold. ``` f ``` Folding function.

#### Return Value

Summary value resulting from the folding process in the context of the target monad.

• ``` foldMapM(_:_:) ``` Extension method

Performs a monadic left fold by mapping the values in a structure to ones in the target monad context and using the `Monoid` instance to combine them.

#### Declaration

Swift

``````static func foldMapM<G: Monad, A, B: Monoid>(
_ fa: Kind<Self, A>,
_ f: @escaping (A) -> Kind<G, B>) -> Kind<G, B>``````

#### Parameters

 ``` fa ``` Structure of values. ``` f ``` Trasnforming function.

#### Return Value

Summary value resulting from the transformation and folding process in the context of the target monad.

• ``` get(_:_:) ``` Extension method

Obtains a specific element of a structure of elements given its indexed position.

#### Declaration

Swift

``````static func get<A>(
_ fa: Kind<Self, A>,
_ index: Int64) -> Option<A>``````

#### Parameters

 ``` fa ``` Structure of values. ``` index ``` Indexed position of the element to retrieve.

#### Return Value

A value if there is any at the given position, or `Option.none` otherwise.

• ``` count(_:) ``` Extension method

Counts how many elements a structure contains.

#### Declaration

Swift

``static func count<A>(_ fa: Kind<Self, A>) -> Int64``

#### Parameters

 ``` fa ``` Structure of values.

#### Return Value

An integer value with the count of how many elements are contained in the structure.

• ``` foldK(_:) ``` Extension method

Combines the elements of an structure using their `MonoidK` instance.

#### Declaration

Swift

``````static func foldK<A, G: MonoidK, B>(_ fga: Kind<Self, B>) -> Kind<G, A>
where B: Kind<G, A>``````

#### Parameters

 ``` fga ``` Structure to be reduced.

#### Return Value

A value in the context providing the `MonoidK` instance.

• ``` reduceK(_:) ``` Extension method

Combines the elements of an structure using their `MonoidK` instance.

#### Declaration

Swift

``````static func reduceK<A, G: MonoidK, B>(_ fga: Kind<Self, B>) -> Kind<G, A>
where B: Kind<G, A>``````

#### Parameters

 ``` fga ``` Structure to be reduced.

#### Return Value

A value in the context providing the `MonoidK` instance.

• ``` asArray(_:) ``` Extension method

Combines the elements of a structure into an `ArrayK`.

#### Declaration

Swift

``static func asArray<A>(_ fa: Kind<Self, A>) -> ArrayK<A>``

#### Parameters

 ``` fa ``` Structure to be converted to an `ArrayK`.

#### Return Value

An array with the elements of `fa`.