1
import Foundation
2

3
/// A FunctorFilter provides the same capabilities as a `Functor`, while filtering out elements simultaneously.
4
public protocol FunctorFilter: Functor {
5
    /// Maps the value/s in the context implementing this instance, filtering out the ones resulting in `Option.none`.
6
    ///
7
    /// - Parameters:
8
    ///   - fa: A value in the context implementing this instance.
9
    ///   - f: A function to map objects and filter them out.
10
    /// - Returns: Transformed and filtered values, in the context implementing this instance.
11
    static func mapFilter<A, B>(_ fa: Kind<Self, A>, _ f: @escaping (A) -> OptionOf<B>) -> Kind<Self, B>
12
}
13

14
// MARK: Related functions
15

16
public extension FunctorFilter {
17
    /// Removes the `Option.none` value/s in this context and extracts the `Option.some` ones.
18
    ///
19
    /// - Parameter fa: Optional values in the context implementing this instance.
20
    /// - Returns: Plain values in the context implementing this instance.
21 0
    static func flattenOption<A>(_ fa: Kind<Self, OptionOf<A>>) -> Kind<Self, A> {
22 0
        return mapFilter(fa, id)
23
    }
24

25
    /// Filters out the value/s in the context implementing this instance that do not match the given predicate.
26
    ///
27
    /// - Parameters:
28
    ///   - fa: Value in the context implementing this instance.
29
    ///   - f: Filtering predicate.
30
    /// - Returns: Filtered value in the context implementing this instance.
31 1
    static func filter<A>(_ fa: Kind<Self, A>, _ f: @escaping (A) -> Bool) -> Kind<Self, A> {
32 1
        return mapFilter(fa, { a in f(a) ? Option.some(a) : Option.none() })
33
    }
34
}
35

36
// MARK: Syntax for FunctorFilter
37

38
public extension Kind where F: FunctorFilter {
39
    /// Maps the value/s in the context implementing this instance, filtering out the ones resulting in `Option.none`.
40
    ///
41
    /// This is a convenience method to call `FunctorFilter.mapFilter` as an instance method of this type.
42
    ///
43
    /// - Parameters:
44
    ///   - f: A function to map objects and filter them out.
45
    /// - Returns: Transformed and filtered values, in this context.
46 1
    func mapFilter<B>(_ f: @escaping (A) -> OptionOf<B>) -> Kind<F, B> {
47 1
        return F.mapFilter(self, f)
48
    }
49

50
    /// Removes the `Option.none` value/s in this context and extracts the `Option.some` ones.
51
    ///
52
    /// This is a convenience method to call `FunctorFilter.flattenOption` as a static method of this type.
53
    ///
54
    /// - Parameter fa: Optional values in this context.
55
    /// - Returns: Plain values in this context.
56 0
    static func flattenOption(_ fa: Kind<F, OptionOf<A>>) -> Kind<F, A> {
57 0
        return F.flattenOption(fa)
58
    }
59

60
    /// Filters out the value/s in this context that do not match the given predicate.
61
    ///
62
    /// This is a convenience method to call `FunctorFilter.filter` as a static method of this type.
63
    ///
64
    /// - Parameters:
65
    ///   - f: Filtering predicate.
66
    /// - Returns: Filtered value in this context.
67 1
    func filter(_ f: @escaping (A) -> Bool) -> Kind<F, A> {
68 1
        return F.filter(self, f)
69
    }
70
}

Read our documentation on viewing source code .

Loading