Skip to content

Commit f0c5e99

Browse files
committed
Refactor zips.
1 parent 0596b80 commit f0c5e99

File tree

2 files changed

+25
-90
lines changed

2 files changed

+25
-90
lines changed

Sources/Coding/Decoding+Zips.swift

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
import Foundation
22

3-
public func zip<A, B>(
4-
_ a: Decoding<A>,
5-
_ b: Decoding<B>
6-
) -> Decoding<(A, B)> {
7-
.init { decoder in
8-
try (
9-
a.decode(decoder),
10-
b.decode(decoder)
11-
)
12-
}
13-
}
14-
15-
public func zip<A, B, Output>(
16-
with f: @escaping (A, B) -> Output) -> (
17-
Decoding<A>,
18-
Decoding<B>
19-
) -> Decoding<Output> {
20-
{ zip($0, $1).map(f) }
21-
}
22-
233
public func zip<A, B, C>(
244
_ a: Decoding<A>,
255
_ b: Decoding<B>,
266
_ c: Decoding<C>
277
) -> Decoding<(A, B, C)> {
28-
.init { decoder in
29-
try (
30-
a.decode(decoder),
31-
b.decode(decoder),
32-
c.decode(decoder)
33-
)
34-
}
8+
zip(zip(a, b), c).map { ($0.0, $0.1, $1) }
359
}
3610

3711
public func zip<A, B, C, Output>(
@@ -49,14 +23,7 @@ public func zip<A, B, C, D>(
4923
_ c: Decoding<C>,
5024
_ d: Decoding<D>
5125
) -> Decoding<(A, B, C, D)> {
52-
.init { decoder in
53-
try (
54-
a.decode(decoder),
55-
b.decode(decoder),
56-
c.decode(decoder),
57-
d.decode(decoder)
58-
)
59-
}
26+
zip(zip(a, b), c, d).map { ($0.0, $0.1, $1, $2) }
6027
}
6128

6229
public func zip<A, B, C, D, Output>(
@@ -76,15 +43,7 @@ public func zip<A, B, C, D, E>(
7643
_ d: Decoding<D>,
7744
_ e: Decoding<E>
7845
) -> Decoding<(A, B, C, D, E)> {
79-
.init { decoder in
80-
try (
81-
a.decode(decoder),
82-
b.decode(decoder),
83-
c.decode(decoder),
84-
d.decode(decoder),
85-
e.decode(decoder)
86-
)
87-
}
46+
zip(zip(a, b), c, d, e).map { ($0.0, $0.1, $1, $2, $3) }
8847
}
8948

9049
public func zip<A, B, C, D, E, Output>(
@@ -106,16 +65,7 @@ public func zip<A, B, C, D, E, F>(
10665
_ e: Decoding<E>,
10766
_ f: Decoding<F>
10867
) -> Decoding<(A, B, C, D, E, F)> {
109-
.init { decoder in
110-
try (
111-
a.decode(decoder),
112-
b.decode(decoder),
113-
c.decode(decoder),
114-
d.decode(decoder),
115-
e.decode(decoder),
116-
f.decode(decoder)
117-
)
118-
}
68+
zip(zip(a, b), c, d, e, f).map { ($0.0, $0.1, $1, $2, $3, $4) }
11969
}
12070

12171
public func zip<A, B, C, D, E, F, Output>(
@@ -139,17 +89,7 @@ public func zip<A, B, C, D, E, F, G>(
13989
_ f: Decoding<F>,
14090
_ g: Decoding<G>
14191
) -> Decoding<(A, B, C, D, E, F, G)> {
142-
.init { decoder in
143-
try (
144-
a.decode(decoder),
145-
b.decode(decoder),
146-
c.decode(decoder),
147-
d.decode(decoder),
148-
e.decode(decoder),
149-
f.decode(decoder),
150-
g.decode(decoder)
151-
)
152-
}
92+
zip(zip(a, b), c, d, e, f, g).map { ($0.0, $0.1, $1, $2, $3, $4, $5) }
15393
}
15494

15595
public func zip<A, B, C, D, E, F, G, Output>(
@@ -175,18 +115,7 @@ public func zip<A, B, C, D, E, F, G, H>(
175115
_ g: Decoding<G>,
176116
_ h: Decoding<H>
177117
) -> Decoding<(A, B, C, D, E, F, G, H)> {
178-
.init { decoder in
179-
try (
180-
a.decode(decoder),
181-
b.decode(decoder),
182-
c.decode(decoder),
183-
d.decode(decoder),
184-
e.decode(decoder),
185-
f.decode(decoder),
186-
g.decode(decoder),
187-
h.decode(decoder)
188-
)
189-
}
118+
zip(zip(a, b), c, d, e, f, g, h).map { ($0.0, $0.1, $1, $2, $3, $4, $5, $6) }
190119
}
191120

192121
public func zip<A, B, C, D, E, F, G, H, Output>(
@@ -214,19 +143,7 @@ public func zip<A, B, C, D, E, F, G, H, I>(
214143
_ h: Decoding<H>,
215144
_ i: Decoding<I>
216145
) -> Decoding<(A, B, C, D, E, F, G, H, I)> {
217-
.init { decoder in
218-
try (
219-
a.decode(decoder),
220-
b.decode(decoder),
221-
c.decode(decoder),
222-
d.decode(decoder),
223-
e.decode(decoder),
224-
f.decode(decoder),
225-
g.decode(decoder),
226-
h.decode(decoder),
227-
i.decode(decoder)
228-
)
229-
}
146+
zip(zip(a, b), c, d, e, f, g, h, i).map { ($0.0, $0.1, $1, $2, $3, $4, $5, $6, $7) }
230147
}
231148

232149
public func zip<A, B, C, D, E, F, G, H, I, Output>(

Sources/Coding/Decoding.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,21 @@ public extension Decoding where Value: Decodable {
341341
}
342342
}
343343

344+
// MARK: - Zip
345+
346+
public func zip<A, B>(
347+
_ a: Decoding<A>,
348+
_ b: Decoding<B>
349+
) -> Decoding<(A, B)> {
350+
.init { decoder in
351+
try (a.decode(decoder), b.decode(decoder))
352+
}
353+
}
354+
355+
public func zip<A, B, Output>(
356+
with f: @escaping (A, B) -> Output) -> (
357+
Decoding<A>,
358+
Decoding<B>
359+
) -> Decoding<Output> {
360+
{ zip($0, $1).map(f) }
361+
}

0 commit comments

Comments
 (0)