Skip to content

Commit d84ae7f

Browse files
Merge pull request #6 from CodaFi/contemplated-alias
Solve our polymorphic typealias problem
2 parents 8aebd98 + 1ba44b4 commit d84ae7f

File tree

8 files changed

+137
-101
lines changed

8 files changed

+137
-101
lines changed

Aquifer/Auxiliary.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Swiftz
1212

1313
/// Lifts an arrow into a pipe by connecting its inputs to the upstream input and its outputs to the
1414
/// downstream output of the pipe.
15-
public func arr<A, B, R>(f: A -> B) -> Proxy<(), A, (), B, R> {
15+
public func arr<A, B, R>(f: A -> B) -> Pipe<A, B, R>.T {
1616
return map(f)
1717
}
1818

@@ -21,7 +21,7 @@ public func arr<A, B, R>(f: A -> B) -> Proxy<(), A, (), B, R> {
2121
/// Values sent along the upstream input in a `.Left` will appear downstream in a `.Left` after the
2222
/// pipe has operated on them. Values appearing along the upstream input in a `.Right` will appear
2323
/// downstream in a `.Right` unchanged.
24-
public func left<A, B, C, R>(p: Proxy<(), A, (), B, R>) -> Proxy<(), Either<A, C>, (), Either<B, C>, R> {
24+
public func left<A, B, C, R>(p: Pipe<A, B, R>.T) -> Pipe<Either<A, C>, Either<B, C>, R>.T {
2525
return leftInner() >~ for_(p) { v in yield(Either.Left(v)) }
2626
}
2727

@@ -30,7 +30,7 @@ public func left<A, B, C, R>(p: Proxy<(), A, (), B, R>) -> Proxy<(), Either<A, C
3030
/// Values sent along the upstream input in a `.Right` will appear downstream in a `.Right` after
3131
/// the pipe has operated on them. Values appearing along the upstream input in a `.Left` will
3232
/// appear downstream in a `.Left` unchanged.
33-
public func right<A, B, C, R>(p: Proxy<(), A, (), B, R>) -> Proxy<(), Either<C, A>, (), Either<C, B>, R> {
33+
public func right<A, B, C, R>(p: Pipe<A, B, R>.T) -> Pipe<Either<C, A>, Either<C, B>, R>.T {
3434
return rightInner() >~ for_(p) { v in yield(Either.Right(v)) }
3535
}
3636

@@ -44,7 +44,7 @@ precedence 180
4444
/// `.Left` values fed to the pipe's upstream input will appear downstream as `.Left`s that have
4545
/// been operated on by the first pipe. `.Right` values fed to the pipe's upstream input will
4646
/// appear downstream as `.Right`s that have been operated on by the second pipe.
47-
public func +++ <A, B, C, D, R>(p: Proxy<(), A, (), B, R>, q: Proxy<(), C, (), D, R>) -> Proxy<(), Either<A, C>, (), Either<B, D>, R> {
47+
public func +++ <A, B, C, D, R>(p: Pipe<A, B, R>.T, q: Pipe<C, D, R>.T) -> Pipe<Either<A, C>, Either<B, D>, R>.T {
4848
return left(p) >-> right(q)
4949
}
5050

@@ -59,19 +59,19 @@ public func mapOutput<UO, UI, DI, DO, NO, FR>(p: Proxy<UO, UI, DI, DO, FR>, _ f:
5959
}
6060

6161
/// Yields a pipe that produces left-scanned values with the given step function.
62-
public func scan1i<DT, FR>(stepWith step: (DT, DT) -> DT) -> Proxy<(), DT, (), DT, FR> {
62+
public func scan1i<DT, FR>(stepWith step: (DT, DT) -> DT) -> Pipe<DT, DT, FR>.T {
6363
return scan1(stepWith: step, initializeWith: identity, extractWith: identity)
6464
}
6565

6666
/// Yields a pipe that produces left-scanned values with the given step function. The pipe is not
6767
/// required to have an initial value, but one is expected to be produced by the `initial` function.
68-
public func scan1<A, UI, DO, FR>(stepWith step: (A, UI) -> A, initializeWith initial: UI -> A, extractWith extractor: A -> DO) -> Proxy<(), UI, (), DO, FR> {
68+
public func scan1<A, UI, DO, FR>(stepWith step: (A, UI) -> A, initializeWith initial: UI -> A, extractWith extractor: A -> DO) -> Pipe<UI, DO, FR>.T {
6969
return await() >>- { scan(stepWith: step, initializeWith: initial($0), extractWith: extractor) }
7070
}
7171

7272
// MARK: - Implementation Details Follow
7373

74-
private func leftInner<A, B, C>() -> Proxy<(), Either<A, C>, (), Either<B, C>, A> {
74+
private func leftInner<A, B, C>() -> Pipe<Either<A, C>, Either<B, C>, A>.T {
7575
return await() >>- {
7676
switch $0 {
7777
case let .Left(x): return pure(x)
@@ -81,7 +81,7 @@ private func leftInner<A, B, C>() -> Proxy<(), Either<A, C>, (), Either<B, C>, A
8181
}
8282

8383

84-
private func rightInner<A, B, C>() -> Proxy<(), Either<C, A>, (), Either<C, B>, A> {
84+
private func rightInner<A, B, C>() -> Pipe<Either<C, A>, Either<C, B>, A>.T {
8585
return await() >>- {
8686
switch $0 {
8787
case let .Left(x): return yield(Either.Left(x)) >>- { _ in rightInner() }

Aquifer/Basic.swift

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ public func drain<UI, DI, DO, FR>() -> Proxy<(), UI, DI, DO, FR> {
2323
}
2424

2525
/// Returns a `Pipe` that applies a (side-effecting) action to all values flowing downstream.
26-
public func chain<DT, FR>(action: DT -> Void) -> Proxy<(), DT, (), DT, FR> {
26+
public func chain<DT, FR>(action: DT -> Void) -> Pipe<DT, DT, FR>.T {
2727
return for_(cat()) { action($0); return yield($0) }
2828
}
2929

3030
// MARK: - Data.Bool
3131

3232
/// Returns a `Pipe` that negates any `Bool`ean input flowing downstream.
33-
public func not<FR>() -> Proxy<(), Bool, (), Bool, FR> {
33+
public func not<FR>() -> Pipe<Bool, Bool, FR>.T {
3434
return map(!)
3535
}
3636

3737
// MARK: - Data.Monoid
3838

3939
/// Folds the values inside the given pipe using the `Monoid` op.
40-
public func mconcat<V: Monoid>(p: Proxy<X, (), (), V, ()>) -> V {
40+
public func mconcat<V: Monoid>(p: Producer<V, ()>.T) -> V {
4141
return fold(p, stepWith: { $0.op($1) }, initializeWith: V.mempty, extractWith: { $0 })
4242
}
4343

4444
/// Mark: - Data.Foldable
4545

4646
/// Returns a representation of the given pipe as a list.
47-
public func toList<V>(p: Proxy<X, (), (), V, ()>) -> List<V> {
47+
public func toList<V>(p: Producer<V, ()>.T) -> List<V> {
4848
return toListRepr(p.repr)
4949
}
5050

@@ -53,15 +53,15 @@ public func toList<V>(p: Proxy<X, (), (), V, ()>) -> List<V> {
5353
// MARK: Basics
5454

5555
/// Returns the first element in the given pipe, if it exists.
56-
public func head<V>(p: Proxy<X, (), (), V, ()>) -> V? {
56+
public func head<V>(p: Producer<V, ()>.T) -> V? {
5757
switch next(p) {
5858
case .Left(_): return nil
5959
case let .Right(k): return k.0
6060
}
6161
}
6262

6363
/// Returns the last element in the given pipe, if it exists.
64-
public func last<V>(p: Proxy<X, (), (), V, ()>) -> V? {
64+
public func last<V>(p: Producer<V, ()>.T) -> V? {
6565
switch next(p) {
6666
case .Left(_): return nil
6767
case let .Right((dO, q)):
@@ -70,90 +70,90 @@ public func last<V>(p: Proxy<X, (), (), V, ()>) -> V? {
7070
}
7171

7272
/// Returns whether the given pipe has terminated.
73-
public func isEmpty<V>(p: Proxy<X, (), (), V, ()>) -> Bool {
73+
public func isEmpty<V>(p: Producer<V, ()>.T) -> Bool {
7474
return next(p).isLeft
7575
}
7676

7777
/// Counts the number of elements in the given pipe.
78-
public func length<V>(p: Proxy<X, (), (), V, ()>) -> Int {
78+
public func length<V>(p: Producer<V, ()>.T) -> Int {
7979
return fold(p, stepWith: { n, _ in n + 1 }, initializeWith: 0, extractWith: { $0 })
8080
}
8181

8282
// MARK: Transformations
8383

8484
/// Returns a pipe that applies the given function to all values flowing downstream.
85-
public func map<UI, DO, FR>(f: UI -> DO) -> Proxy<(), UI, (), DO, FR> {
85+
public func map<UI, DO, FR>(f: UI -> DO) -> Pipe<UI, DO, FR>.T {
8686
return for_(cat()) { v in yield(f(v)) }
8787
}
8888

8989
/// Returns a pipe that uses the given function to produce sequences that are subsequently flattened
9090
/// downstream.
91-
public func mapMany<UI, S: SequenceType, FR>(f: UI -> S) -> Proxy<(), UI, (), S.Generator.Element, FR> {
91+
public func mapMany<UI, S: SequenceType, FR>(f: UI -> S) -> Pipe<UI, S.Generator.Element, FR>.T {
9292
return for_(cat()) { each(f($0)) }
9393
}
9494

9595
/// Returns a pipe that yields the description of each value flowing downstream.
96-
public func description<UI: CustomStringConvertible, FR>() -> Proxy<(), UI, (), String, FR> {
96+
public func description<UI: CustomStringConvertible, FR>() -> Pipe<UI, String, FR>.T {
9797
return map { $0.description }
9898
}
9999

100100
/// Returns a pipe that yields the debug description of each value flowing downstream.
101-
public func debugDescription<UI: CustomDebugStringConvertible, FR>() -> Proxy<(), UI, (), String, FR> {
101+
public func debugDescription<UI: CustomDebugStringConvertible, FR>() -> Pipe<UI, String, FR>.T {
102102
return map { $0.debugDescription }
103103
}
104104

105105
// MARK: Folds
106106

107107
/// Folds over the elements of the given `Producer`.
108-
public func fold<A, V, R>(p: Proxy<X, (), (), V, ()>, stepWith step: (A, V) -> A, initializeWith initial: A, extractWith extractor: A -> R) -> R {
108+
public func fold<A, V, R>(p: Producer<V, ()>.T, stepWith step: (A, V) -> A, initializeWith initial: A, extractWith extractor: A -> R) -> R {
109109
return foldRepr(p.repr, stepWith: step, initializeWith: initial, extractWith: extractor)
110110
}
111111

112112
/// A version of `fold` that preserve the return value of the given `Producer`.
113-
public func foldRet<A, V, FR, R>(p: Proxy<X, (), (), V, FR>, stepWith step: (A, V) -> A, initializeWith initial: A, extractWith extractor: A -> R) -> (R, FR) {
113+
public func foldRet<A, V, FR, R>(p: Producer<V, FR>.T, stepWith step: (A, V) -> A, initializeWith initial: A, extractWith extractor: A -> R) -> (R, FR) {
114114
return foldRetRepr(p.repr, stepWith: step, initializeWith: initial, extractWith: extractor)
115115
}
116116

117117
// MARK: Special Folds
118118

119119
/// Returns a pipe that flattens the elements of any sequences flowing downstream.
120-
public func concat<S: SequenceType, FR>() -> Proxy<(), S, (), S.Generator.Element, FR> {
120+
public func concat<S: SequenceType, FR>() -> Pipe<S, S.Generator.Element, FR>.T {
121121
return for_(cat(), each)
122122
}
123123

124124

125125
/// Returns whether all elements of the receiver satisfy the given predicate.
126-
public func all<V>(p: Proxy<X, (), (), V, ()>, predicate: V -> Bool) -> Bool {
126+
public func all<V>(p: Producer<V, ()>.T, predicate: V -> Bool) -> Bool {
127127
return isEmpty(p >-> filter { !predicate($0) })
128128
}
129129

130130
/// Returns whether any element of the receiver satisfies the given predicate.
131-
public func any<V>(p: Proxy<X, (), (), V, ()>, predicate: V -> Bool) -> Bool {
131+
public func any<V>(p: Producer<V, ()>.T, predicate: V -> Bool) -> Bool {
132132
return !isEmpty(p >-> filter(predicate))
133133
}
134134

135135
/// Returns the conjunct of all values inside the pipe.
136-
public func and(p: Proxy<X, (), (), Bool, ()>) -> Bool {
136+
public func and(p: Producer<Bool, ()>.T) -> Bool {
137137
return all(p) { b in b }
138138
}
139139

140140
/// Returns the disjunct of all values inside the pipe.
141-
public func or(p: Proxy<X, (), (), Bool, ()>) -> Bool {
141+
public func or(p: Producer<Bool, ()>.T) -> Bool {
142142
return any(p) { b in b }
143143
}
144144

145145
/// Returns the sum of the values in the given pipe.
146-
public func sum<V: NumericType>(p: Proxy<X, (), (), V, ()>) -> V {
146+
public func sum<V: NumericType>(p: Producer<V, ()>.T) -> V {
147147
return fold(p, stepWith: { $0.plus($1) }, initializeWith: V.zero, extractWith: { $0 })
148148
}
149149

150150
/// Returns the product of the values in the given pipe.
151-
public func product<V: NumericType>(p: Proxy<X, (), (), V, ()>) -> V {
151+
public func product<V: NumericType>(p: Producer<V, ()>.T) -> V {
152152
return fold(p, stepWith: { $0.times($1) }, initializeWith: V.one, extractWith: { $0 })
153153
}
154154

155155
/// Finds the maximum value among all the elements of the given pipe.
156-
public func maximum<V: Comparable>(p: Proxy<X, (), (), V, ()>) -> V? {
156+
public func maximum<V: Comparable>(p: Producer<V, ()>.T) -> V? {
157157
func step(x: V?, _ v: V) -> V? {
158158
if let w = x {
159159
return max(v, w)
@@ -165,7 +165,7 @@ public func maximum<V: Comparable>(p: Proxy<X, (), (), V, ()>) -> V? {
165165
}
166166

167167
/// Finds the minimum value among all the elements of the given pipe.
168-
public func minimum<V: Comparable>(p: Proxy<X, (), (), V, ()>) -> V? {
168+
public func minimum<V: Comparable>(p: Producer<V, ()>.T) -> V? {
169169
func step(x: V?, _ v: V) -> V? {
170170
if let w = x {
171171
return min(v, w)
@@ -179,7 +179,7 @@ public func minimum<V: Comparable>(p: Proxy<X, (), (), V, ()>) -> V? {
179179
// MARK: Scans
180180

181181
/// Returns a pipe that uses the given function as a left-scan on all values flowing downstream.
182-
public func scan<A, UI, DO, FR>(stepWith step: (A, UI) -> A, initializeWith initial: A, extractWith extractor: A -> DO) -> Proxy<(), UI, (), DO, FR> {
182+
public func scan<A, UI, DO, FR>(stepWith step: (A, UI) -> A, initializeWith initial: A, extractWith extractor: A -> DO) -> Pipe<UI, DO, FR>.T {
183183
return yield(extractor(initial)) >>- { _ in await() >>- { scan(stepWith: step, initializeWith: step(initial, $0), extractWith: extractor) } }
184184
}
185185

@@ -218,12 +218,12 @@ public func takeWhile<DT>(predicate: DT -> Bool) -> Proxy<(), DT, (), DT, ()> {
218218
}
219219

220220
/// Returns a `Pipe` that discards a given amount of values.
221-
public func drop<DT, FR>(n: Int) -> Proxy<(), DT, (), DT, FR> {
221+
public func drop<DT, FR>(n: Int) -> Pipe<DT, DT, FR>.T {
222222
return dropInner(n) >>- { _ in cat() }
223223
}
224224

225225
/// Returns a `Pipe` that discards values as long as the given predicate is true.
226-
public func dropWhile<DT, FR>(predicate: DT -> Bool) -> Proxy<(), DT, (), DT, FR> {
226+
public func dropWhile<DT, FR>(predicate: DT -> Bool) -> Pipe<DT, DT, FR>.T {
227227
return await() >>- { v in
228228
if predicate(v) {
229229
return dropWhile(predicate)
@@ -236,24 +236,24 @@ public func dropWhile<DT, FR>(predicate: DT -> Bool) -> Proxy<(), DT, (), DT, FR
236236
// MARK: Searching with Equality
237237

238238
/// Returns whether a given value matches any of the values inside the given pipe.
239-
public func elem<V: Equatable>(p: Proxy<X, (), (), V, ()>, _ x: V) -> Bool {
239+
public func elem<V: Equatable>(p: Producer<V, ()>.T, _ x: V) -> Bool {
240240
return any(p) { x == $0 }
241241
}
242242

243243
/// Returns whether a given value does not match any of the values inside the given pipe.
244-
public func notElem<V: Equatable>(p: Proxy<X, (), (), V, ()>, _ x: V) -> Bool {
244+
public func notElem<V: Equatable>(p: Producer<V, ()>.T, _ x: V) -> Bool {
245245
return all(p) { x != $0 }
246246
}
247247

248248
// MARK: Searching with Predicates
249249

250250
/// Finds the first element in the pipe that satisfies a given predicate.
251-
public func find<V>(p: Proxy<X, (), (), V, ()>, _ predicate: V -> Bool) -> V? {
251+
public func find<V>(p: Producer<V, ()>.T, _ predicate: V -> Bool) -> V? {
252252
return head(p >-> filter(predicate))
253253
}
254254

255255
/// Returns a `Pipe` that only forwards values that satisfy the given predicate.
256-
public func filter<DT, FR>(predicate: DT -> Bool) -> Proxy<(), DT, (), DT, FR> {
256+
public func filter<DT, FR>(predicate: DT -> Bool) -> Pipe<DT, DT, FR>.T {
257257
return for_(cat()) { v in
258258
if predicate(v) {
259259
return yield(v)
@@ -266,31 +266,31 @@ public func filter<DT, FR>(predicate: DT -> Bool) -> Proxy<(), DT, (), DT, FR> {
266266
// MARK: Indexing
267267

268268
/// Returns a pipe that outputs the indices of all elements that match the given element
269-
public func elemIndices<UI: Equatable, FR>(@autoclosure(escaping) x: () -> UI) -> Proxy<(), UI, (), Int, FR> {
269+
public func elemIndices<UI: Equatable, FR>(@autoclosure(escaping) x: () -> UI) -> Pipe<UI, Int, FR>.T {
270270
return findIndices { x() == $0 }
271271
}
272272

273273
/// Finds the index of the first element in the pipe that satisfies a given predicate.
274-
public func findIndex<V>(p: Proxy<X, (), (), V, ()>, _ predicate: V -> Bool) -> Int? {
274+
public func findIndex<V>(p: Producer<V, ()>.T, _ predicate: V -> Bool) -> Int? {
275275
return head(p >-> findIndices(predicate))
276276
}
277277

278278
/// Returns a pipe that outputs the indices of all elements that match the given predicate.
279-
public func findIndices<UI, FR>(predicate: UI -> Bool) -> Proxy<(), UI, (), Int, FR> {
279+
public func findIndices<UI, FR>(predicate: UI -> Bool) -> Pipe<UI, Int, FR>.T {
280280
return findIndicesInner(predicate, 0)
281281
}
282282

283283
// MARK: Zipping
284284

285285
/// Returns a `Producer` of pairs that zips the downstream output of the two given `Producer`s
286286
/// together.
287-
public func zip<V0, V1, R>(p: Proxy<X, (), (), V0, R>, _ q: Proxy<X, (), (), V1, R>) -> Proxy<X, (), (), (V0, V1), R> {
287+
public func zip<V0, V1, R>(p: Producer<V0, R>.T, _ q: Producer<V1, R>.T) -> Producer<(V0, V1), R>.T {
288288
return zipWith(p, q) { ($0, $1) }
289289
}
290290

291291
/// Returns a `Producer` of values that zips the downstream output of the two given pipes together
292292
/// using the given function.
293-
public func zipWith<V0, V1, V2, R>(p: Proxy<X, (), (), V0, R>, _ q: Proxy<X, (), (), V1, R>, _ f: (V0, V1) -> V2) -> Proxy<X, (), (), V2, R> {
293+
public func zipWith<V0, V1, V2, R>(p: Producer<V0, R>.T, _ q: Producer<V1, R>.T, _ f: (V0, V1) -> V2) -> Producer<V2, R>.T {
294294
switch next(p) {
295295
case let .Left(x): return pure(x)
296296
case let .Right((dO0, r)):
@@ -303,11 +303,11 @@ public func zipWith<V0, V1, V2, R>(p: Proxy<X, (), (), V0, R>, _ q: Proxy<X, (),
303303
}
304304

305305
// this seems to required higher-kinded types to implement, even though none appear in its signature
306-
/*public func tee<V, R>(p: Proxy<(), V, (), X, R>) -> Proxy<(), V, (), V, R> {
306+
/*public func tee<V, R>(p: Pipe<V, X, R>.T) -> Pipe<V, V, R>.T {
307307
}*/
308308

309309
// this seems to required higher-kinded types to implement, even though none appear in its signature
310-
/*public func generalize<UT, UI, DO, FR>(p: Proxy<(), UI, (), DO, FR>) -> Proxy<UT, UI, UT, DO, FR> {
310+
/*public func generalize<UT, UI, DO, FR>(p: Pipe<UI, DO, FR>.T) -> Proxy<UT, UI, UT, DO, FR> {
311311
}*/
312312

313313
// MARK: - Implementation Details Follow
@@ -320,7 +320,7 @@ private func dropInner<DT>(n: Int) -> Proxy<(), DT, (), DT, ()> {
320320
}
321321
}
322322

323-
private func findIndicesInner<UI, FR>(predicate: UI -> Bool, _ n: Int) -> Proxy<(), UI, (), Int, FR> {
323+
private func findIndicesInner<UI, FR>(predicate: UI -> Bool, _ n: Int) -> Pipe<UI, Int, FR>.T {
324324
return await() >>- {
325325
if predicate($0) {
326326
return yield(n) >>- { _ in findIndicesInner(predicate, n + 1) }
@@ -346,7 +346,7 @@ private func foldRetRepr<A, V, FR, R>(p: ProxyRepr<X, (), (), V, FR>, stepWith s
346346
}
347347
}
348348

349-
private func lastInner<V>(x: V, _ p: Proxy<X, (), (), V, ()>) -> V? {
349+
private func lastInner<V>(x: V, _ p: Producer<V, ()>.T) -> V? {
350350
switch next(p) {
351351
case .Left(_): return x
352352
case let .Right((dO, q)):

0 commit comments

Comments
 (0)