Fold

open class Fold<S, A> : FoldOf<S, A>

A Fold is an optic that allows to focus into a structure and get multiple results.

Fold is a generalization of an instance of Foldable and it is implemented in terms of foldMap.

Type parameters: - S: Source. - A: Focus.

  • Creates a Fold that has no focus.

    Declaration

    Swift

    public static var void: Fold<S, A> { get }
  • Creates a Fold based on the instance of Foldable for F.

    Declaration

    Swift

    public static func fromFoldable<F>() -> Fold<Kind<F, A>, A> where S : Kind<F, A>, F : Foldable

    Return Value

    A Fold based on Foldable for F.

  • Composes a Fold with a Fold.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Fold<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with an Iso.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Iso<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with a Getter.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Getter<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with a Lens.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Lens<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with a Prism.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Prism<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with an Optional.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Optional<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Composes a Fold with a Traversal.

    Declaration

    Swift

    public static func + <C>(lhs: Fold<S, A>, rhs: Traversal<A, C>) -> Fold<S, C>

    Parameters

    lhs

    Left hand side of the composition.

    rhs

    Right hand side of the composition.

    Return Value

    A Fold resulting from the sequential application of the two provided optics.

  • Map each foci to a type R and use a Monoid to fold the results.

    Declaration

    Swift

    open func foldMap<R: Monoid>(_ s: S, _ f: @escaping (A) -> R) -> R

    Parameters

    s

    Source.

    f

    Mapping function.

    Return Value

    Summary value resulting from the fold.

  • Counts the number of foci in the source.

    Declaration

    Swift

    public func size(_ s: S) -> Int

    Parameters

    s

    Source.

    Return Value

    Number of foci.

  • Checks if all foci match a predicate.

    Declaration

    Swift

    public func forAll(_ s: S, _ predicate: @escaping (A) -> Bool) -> Bool

    Parameters

    s

    Source.

    predicate

    Testing predicate.

    Return Value

    Boolean value indicating if all foci match the predicate.

  • Checks if a source is empty.

    Declaration

    Swift

    public func isEmpty(_ s: S) -> Bool

    Parameters

    s

    Source.

    Return Value

    True if the source has no foci; false otherwise.

  • Checks if a source is non-empty.

    Declaration

    Swift

    public func nonEmpty(_ s: S) -> Bool

    Parameters

    s

    Source.

    Return Value

    False is the source has no foci; true otherwise.

  • Retrieves the first focus, if any.

    Declaration

    Swift

    public func headOption(_ s: S) -> Option<A>

    Parameters

    s

    Source.

    Return Value

    An optional value with the first focus.

  • Retrieves the last focus, if any.

    Declaration

    Swift

    public func lastOption(_ s: S) -> Option<A>

    Parameters

    s

    Source.

    Return Value

    An optional value with the last focus.

  • Gets all foci.

    Declaration

    Swift

    public func getAll(_ s: S) -> ArrayK<A>

    Parameters

    s

    Source.

    Return Value

    An ArrayK with all foci.

  • Joins to Fold with the same focus.

    Declaration

    Swift

    public func choice<C>(_ other: Fold<C, A>) -> Fold<Either<S, C>, A>

    Parameters

    other

    Fold to join with.

    Return Value

    A Fold that operates on either of the two original sources, with the same target.

  • Creates the sum of this Fold with another type, placing this as the left side.

    Declaration

    Swift

    public func left<C>() -> Fold<Either<S, C>, Either<A, C>>

    Return Value

    A Fold that operates on Eithers where the right side remains unchanged.

  • Creates the sum of this Fold with another type, placing this as the right side.

    Declaration

    Swift

    public func right<C>() -> Fold<Either<C, S>, Either<C, A>>

    Return Value

    A Fold that operates on Eithers where the left side remains unchanged.

  • Composes this Fold with a Fold.

    Declaration

    Swift

    public func compose<C>(_ other: Fold<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with an Iso.

    Declaration

    Swift

    public func compose<C>(_ other: Iso<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with a Getter.

    Declaration

    Swift

    public func compose<C>(_ other: Getter<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with a Lens.

    Declaration

    Swift

    public func compose<C>(_ other: Lens<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with a Prism.

    Declaration

    Swift

    public func compose<C>(_ other: Prism<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with an Optional.

    Declaration

    Swift

    public func compose<C>(_ other: Optional<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Composes this Fold with a Traversal.

    Declaration

    Swift

    public func compose<C>(_ other: Traversal<A, C>) -> Fold<S, C>

    Parameters

    other

    Value to compose with.

    Return Value

    A Fold resulting from the sequential application of the two optics.

  • Finds the first focus that matches a predicate.

    Declaration

    Swift

    public func find(_ s: S, _ predicate: @escaping (A) -> Bool) -> Option<A>

    Parameters

    s

    Source.

    predicate

    Testing predicate.

    Return Value

    An optional value containing the focus, if any.

  • Checks if any focus in the provided source match a predicate.

    Declaration

    Swift

    public func exists(_ s: S, _ predicate: @escaping (A) -> Bool) -> Bool

    Parameters

    s

    Source.

    predicate

    Testing predicate.

    Return Value

    True if any focus matches the predicate; false otherwise.

  • Focuses on a specific index of this fold.

    Declaration

    Swift

    func at(_ i: A.AtIndex) -> Fold<S, A.AtFoci>

    Parameters

    i

    Index to focus.

    Return Value

    A fold from this structure to the focused index.

  • Provides a fold over all elements of the foci of this fold.

    Declaration

    Swift

    var every: Fold<S, A.EachFoci> { get }
  • Provides a fold focused on an index of the focus of this fold.

    Declaration

    Swift

    func index(_ i: A.IndexType) -> Fold<S, A.IndexFoci>

    Parameters

    i

    Index to focus.

    Return Value

    A fold focused on an index of the focus of this fold.

  • Provides a fold focused on an index of the focus of this fold.

    Declaration

    Swift

    subscript(i: A.IndexType) -> Fold<S, A.IndexFoci> { get }

    Parameters

    i

    Index to focus.

    Return Value

    A fold focused on an index of the focus of this fold.

  • Provides an identity Fold.

    Declaration

    Swift

    static var identity: Fold<S, S> { get }
  • Provides a Fold that takes either an S or an S and strips the choice of S.

    Declaration

    Swift

    static var codiagonal: Fold<Either<S, S>, S> { get }
  • Provides a Fold based on a predicate on the source.

    Declaration

    Swift

    static func select(_ predicate: @escaping (S) -> Bool) -> Fold<S, S>

    Parameters

    predicate

    Testing predicate.

    Return Value

    A Fold based on the provided predicate.

  • Folds all foci into a summary value using its instance of Monoid.

    Declaration

    Swift

    func fold(_ s: S) -> A

    Parameters

    s

    Source.

    Return Value

    Summary value.

  • Folds all foci into a summary value using its instance of Monoid.

    Declaration

    Swift

    func combineAll(_ s: S) -> A

    Parameters

    s

    Source.

    Return Value

    Summary value.