6
6
// Copyright (c) 2014 Maxwell Swadling. All rights reserved.
7
7
//
8
8
9
+ #if !XCODE_BUILD
10
+ import Operadics
11
+ #endif
12
+
9
13
/// The identity function.
10
- public func identity< A> ( a : A ) -> A {
14
+ public func identity< A> ( _ a : A ) -> A {
11
15
return a
12
16
}
13
17
14
18
/// The constant combinator ignores its second argument and always returns its first argument.
15
- public func const< A, B> ( x : A ) -> B -> A {
19
+ public func const< A, B> ( _ x : A ) -> ( B ) -> A {
16
20
return { _ in x }
17
21
}
18
22
19
23
/// Flip a function's arguments
20
- public func flip< A, B, C> ( f : ( ( A , B ) -> C ) , _ b : B , _ a : A ) -> C {
24
+ public func flip< A, B, C> ( _ f : ( ( A , B ) -> C ) , _ b : B , _ a : A ) -> C {
21
25
return f ( a, b)
22
26
}
23
27
24
28
/// Flip a function's arguments and return a curried function that takes
25
29
/// the arguments in flipped order.
26
- public func flip< A, B, C> ( f : A -> B -> C ) -> B -> A -> C {
30
+ public func flip< A, B, C> ( _ f : @escaping ( A ) -> ( B ) -> C ) -> ( B ) -> ( A ) -> C {
27
31
return { b in { a in f ( a) ( b) } }
28
32
}
29
33
@@ -32,7 +36,7 @@ public func flip<A, B, C>(f : A -> B -> C) -> B -> A -> C {
32
36
/// f : B -> C
33
37
/// g : A -> B
34
38
/// (f • g)(x) === f(g(x)) : A -> B -> C
35
- public func • < A, B, C> ( f : B -> C , g : A -> B ) -> A -> C {
39
+ public func • < A, B, C> ( f : @escaping ( B ) -> C , g : @escaping ( A ) -> B ) -> ( A ) -> C {
36
40
return { ( a : A ) -> C in
37
41
return f ( g ( a) )
38
42
}
@@ -47,7 +51,7 @@ public func • <A, B, C>(f : B -> C, g : A -> B) -> A -> C {
47
51
/// f § g § h § x = f(g(h(x)))
48
52
///
49
53
/// Key Chord: ⌥ + 6
50
- public func § < A, B> ( f : A -> B , a : A ) -> B {
54
+ public func § < A, B> ( f : ( A ) -> B , a : A ) -> B {
51
55
return f ( a)
52
56
}
53
57
@@ -59,7 +63,7 @@ public func § <A, B>(f : A -> B, a : A) -> B {
59
63
/// f <| g <| h <| x = f (g (h x))
60
64
///
61
65
/// Acts as a synonym for §.
62
- public func <| < A, B> ( f : A -> B , a : A ) -> B {
66
+ public func <| < A, B> ( f : ( A ) -> B , a : A ) -> B {
63
67
return f ( a)
64
68
}
65
69
@@ -75,25 +79,26 @@ public func <| <A, B>(f : A -> B, a : A) -> B {
75
79
/// 1 |> { $0.advancedBy($0) }
76
80
/// |> { $0.advancedBy($0) }
77
81
/// |> { $0 * $0 }
78
- public func |> < A, B> ( a : A , f : A -> B ) -> B {
82
+ public func |> < A, B> ( a : A , f : ( A ) -> B ) -> B {
79
83
return f ( a)
80
84
}
81
85
82
86
/// The fixpoint (or Y) combinator computes the least fixed point of an equation. That is, the first
83
87
/// point at which further application of x to a function is the same x.
84
88
///
85
89
/// x = f(x)
86
- public func fix< A, B> ( f : ( A -> B ) -> A -> B ) -> A -> B {
90
+ public func fix< A, B> ( _ f : @escaping ( ( A ) -> B ) -> ( A ) -> B ) -> ( A ) -> B {
87
91
return { x in f ( fix ( f) ) ( x) }
88
92
}
89
93
90
94
/// The fixpoint (or Y) combinator computes the least fixed point of an equation. That is, the first
91
95
/// point at which further application of x to a function is the same x.
92
96
///
93
97
/// `fixt` is the exception-enabled version of fix.
94
- public func fixt< A, B> ( f : ( A throws -> B ) throws -> ( A throws -> B ) ) rethrows -> A throws -> B {
98
+ public func fixt< A, B> ( _ f : @escaping ( ( A ) throws -> B ) throws -> ( ( A ) throws -> B ) ) rethrows -> ( A ) throws -> B {
95
99
return { x in try f ( fixt ( f) ) ( x) }
96
100
}
101
+
97
102
/// On | Applies the function on its right to both its arguments, then applies the function on its
98
103
/// left to the result of both prior applications.
99
104
///
@@ -103,7 +108,7 @@ public func fixt<A, B>(f : (A throws -> B) throws -> (A throws -> B)) rethrows -
103
108
///
104
109
/// let arr : [(Int, String)] = [(2, "Second"), (1, "First"), (5, "Fifth"), (3, "Third"), (4, "Fourth")]
105
110
/// let sortedByFirstIndex = arr.sort((<) |*| fst)
106
- public func |*| < A, B, C> ( o : B -> B -> C , f : A -> B ) -> A -> A -> C {
111
+ public func |*| < A, B, C> ( o : @escaping ( B ) -> ( B ) -> C , f : @escaping ( A ) -> B ) -> ( A ) -> ( A ) -> C {
107
112
return on ( o) ( f)
108
113
}
109
114
@@ -116,27 +121,27 @@ public func |*| <A, B, C>(o : B -> B -> C, f : A -> B) -> A -> A -> C {
116
121
///
117
122
/// let arr : [(Int, String)] = [(2, "Second"), (1, "First"), (5, "Fifth"), (3, "Third"), (4, "Fourth")]
118
123
/// let sortedByFirstIndex = arr.sort((<) |*| fst)
119
- public func |*| < A, B, C> ( o : ( B , B ) -> C , f : A -> B ) -> A -> A -> C {
124
+ public func |*| < A, B, C> ( o : @escaping ( B , B ) -> C , f : @escaping ( A ) -> B ) -> ( A ) -> ( A ) -> C {
120
125
return on ( o) ( f)
121
126
}
122
127
123
128
/// On | Applies the function on its right to both its arguments, then applies the function on its
124
129
/// left to the result of both prior applications.
125
130
///
126
131
/// (+) |*| f = { x in { y in f(x) + f(y) } }
127
- public func on< A, B, C> ( o : B -> B -> C ) -> ( A -> B ) -> A -> A -> C {
132
+ public func on< A, B, C> ( _ o : @escaping ( B ) -> ( B ) -> C ) -> ( @escaping ( A ) -> B ) -> ( A ) -> ( A ) -> C {
128
133
return { f in { x in { y in o ( f ( x) ) ( f ( y) ) } } }
129
134
}
130
135
131
136
/// On | Applies the function on its right to both its arguments, then applies the function on its
132
137
/// left to the result of both prior applications.
133
138
///
134
139
/// (+) |*| f = { x, y in f(x) + f(y) }
135
- public func on< A, B, C> ( o : ( B , B ) -> C ) -> ( A -> B ) -> A -> A -> C {
140
+ public func on< A, B, C> ( _ o : @escaping ( B , B ) -> C ) -> ( @escaping ( A ) -> B ) -> ( A ) -> ( A ) -> C {
136
141
return { f in { x in { y in o ( f ( x) , f ( y) ) } } }
137
142
}
138
143
139
144
/// Applies a function to an argument until a given predicate returns true.
140
- public func until< A> ( p : A -> Bool ) -> ( A -> A ) -> A -> A {
145
+ public func until< A> ( _ p : @escaping ( A ) -> Bool ) -> ( @escaping ( A ) -> A ) -> ( A ) -> A {
141
146
return { f in { x in p ( x) ? x : until ( p) ( f) ( f ( x) ) } }
142
147
}
0 commit comments