1
import Foundation
2

3
/// A MonadCombine has the capabilities of `MonadFilter` and `Alternative` together.
4
public protocol MonadCombine: MonadFilter, Alternative {}
5

6
public extension MonadCombine {
7
    /// Fold over the inner structure to combine all of the values with our combine method inherited from `MonoidK.
8
    ///
9
    /// - Parameter fga: Nested contexts value.
10
    /// - Returns: A value in the context implementing this instance where the inner context has been folded.
11 0
    static func unite<G: Foldable, A>(_ fga: Kind<Self, Kind<G, A>>) -> Kind<Self, A> {
12 0
        return flatMap(fga, { ga in G.foldLeft(ga, empty(), { acc, a in combineK(acc, pure(a)) })})
13
    }
14
}
15

16
// MARK: Syntax for MonadCombine
17

18
public extension Kind where F: MonadCombine {
19
    /// Fold over the inner structure to combine all of the values with our combine method inherited from `MonoidK.
20
    ///
21
    /// This is a convenience method to call `MonadCombine.unite` as a static method of this type.
22
    ///
23
    /// - Parameter fga: Nested contexts value.
24
    /// - Returns: A value in the context implementing this instance where the inner context has been folded.
25 0
    static func unite<G: Foldable>(_ fga: Kind<F, Kind<G, A>>) -> Kind<F, A> {
26 0
        return F.unite(fga)
27
    }
28
}

Read our documentation on viewing source code .

Loading