1
import Foundation
2

3
/// A MonadFilter is a `Monad` with `FunctorFilter` capabilities. It also provides functionality to create a value that represents an empty element in the context implementing an instance of this typeclass.
4
///
5
/// Implementing this typeclass automatically derives an implementation for `FunctorFilter.mapFilter`.
6
public protocol MonadFilter: Monad, FunctorFilter {
7
    /// Obtains an empty element in the context implementing this instance.
8
    ///
9
    /// - Returns: Empty element.
10
    static func empty<A>() -> Kind<Self, A>
11
}
12

13
// MARK: Related functions
14

15
public extension MonadFilter {
16
    // Docs inherited from `FunctorFilter`
17 1
    static func mapFilter<A, B>(_ fa : Kind<Self, A>, _ f : @escaping (A) -> OptionOf<B>) -> Kind<Self, B>{
18 1
        return flatMap(fa, { a in
19 1
            Option<B>.fix(f(a)).fold(self.empty, self.pure)
20 1
        })
21
    }
22
}
23

24
// MARK: Syntax for MonadFilter
25

26
public extension Kind where F: MonadFilter {
27
    /// Obtains an empty element in this context.
28
    ///
29
    /// - Returns: Empty element.
30 0
    static var empty: Kind<F, A> {
31 0
        return F.empty()
32
    }
33
}

Read our documentation on viewing source code .

Loading