forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV6.java
More file actions
379 lines (336 loc) · 8.91 KB
/
V6.java
File metadata and controls
379 lines (336 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package fj.data.vector;
import fj.F;
import fj.F2;
import fj.P1;
import fj.P2;
import fj.P5;
import fj.P6;
import static fj.Function.curry;
import static fj.P.p2;
import fj.data.Array;
import fj.data.NonEmptyList;
import fj.data.Stream;
import java.util.Iterator;
/**
* A vector-6.
*/
public final class V6<A> implements Iterable<A> {
private final V5<A> tail;
private final P1<A> head;
private V6(final P1<A> head, final V5<A> tail) {
this.head = head;
this.tail = tail;
}
/**
* Creates a vector-6 from a homogeneous product-6.
*
* @param p The product-6 from which to create a vector.
* @return A new vector-6.
*/
public static <A> V6<A> p(final P6<A, A, A, A, A, A> p) {
return new V6<A>(new P1<A>() {
public A _1() {
return p._1();
}
}, V5.p(new P5<A, A, A, A, A>() {
public A _1() {
return p._2();
}
public A _2() {
return p._3();
}
public A _3() {
return p._4();
}
public A _4() {
return p._5();
}
public A _5() {
return p._6();
}
}));
}
/**
* Creates a vector-6 from a head and a tail.
*
* @param head The value to put as the first element of the vector.
* @param tail The vector representing all but the first element of the new vector.
* @return The new vector.
*/
public static <A> V6<A> cons(final P1<A> head, final V5<A> tail) {
return new V6<A>(head, tail);
}
/**
* Returns the first element of this vector.
*
* @return the first element of this vector.
*/
public A _1() {
return head._1();
}
/**
* Returns the second element of this vector.
*
* @return the second element of this vector.
*/
public A _2() {
return tail._1();
}
/**
* Returns the third element of this vector.
*
* @return the third element of this vector.
*/
public A _3() {
return tail._2();
}
/**
* Returns the fourth element of this vector.
*
* @return the fourth element of this vector.
*/
public A _4() {
return tail._3();
}
/**
* Returns the fifth element of this vector.
*
* @return the fifth element of this vector.
*/
public A _5() {
return tail._4();
}
/**
* Returns the sixth element of this vector.
*
* @return the sixth element of this vector.
*/
public A _6() {
return tail._5();
}
/**
* Returns all but the first element of this vector, as a vector-5.
*
* @return all but the first element of this vector, as a vector-5.
*/
public V5<A> tail() {
return tail;
}
/**
* Returns the first element of this vector, as a product-1.
*
* @return the first element of this vector, as a product-1.
*/
public P1<A> head() {
return head;
}
/**
* Returns an iterator for the elements of this vector.
*
* @return an iterator for the elements of this vector.
*/
public Iterator<A> iterator() {
return toStream().iterator();
}
/**
* Returns a homogeneous product-6 equivalent to this vector.
*
* @return a homogeneous product-6 equivalent to this vector.
*/
public P6<A, A, A, A, A, A> p() {
return new P6<A, A, A, A, A, A>() {
public A _1() {
return V6.this._1();
}
public A _2() {
return V6.this._2();
}
public A _3() {
return V6.this._3();
}
public A _4() {
return V6.this._4();
}
public A _5() {
return V6.this._5();
}
public A _6() {
return V6.this._6();
}
};
}
/**
* Returns a nonempty list with the elements of this vector.
*
* @return a nonempty list with the elements of this vector.
*/
public NonEmptyList<A> toNonEmptyList() {
return NonEmptyList.nel(_1(), tail.toNonEmptyList().toList());
}
/**
* Returns a stream of the elements of this vector.
*
* @return a stream of the elements of this vector.
*/
public Stream<A> toStream() {
return Stream.cons(head._1(), new P1<Stream<A>>() {
public Stream<A> _1() {
return tail.toStream();
}
});
}
/**
* Returns an array with the elements of this vector.
*
* @return an array with the elements of this vector.
*/
@SuppressWarnings("unchecked")
public Array<A> toArray() {
return Array.array(_1(), _2(), _3(), _4(), _5(), _6());
}
/**
* Maps the given function across this vector.
*
* @param f The function to map across this vector.
* @return A new vector after the given function has been applied to each element.
*/
public <B> V6<B> map(final F<A, B> f) {
return new V6<B>(head.map(f), tail.map(f));
}
/**
* Performs function application within a vector (applicative functor pattern).
*
* @param vf The vector of functions to apply.
* @return A new vector after zipping the given vector of functions over this vector.
*/
public <B> V6<B> apply(final V6<F<A, B>> vf) {
return new V6<B>(P1.<A, B>apply(head, vf.head()), tail.apply(vf.tail()));
}
/**
* Zips this vector with the given vector using the given function to produce a new vector.
*
* @param bs The vector to zip this vector with.
* @param f The function to zip this vector and the given vector with.
* @return A new vector with the results of the function.
*/
public <B, C> V6<C> zipWith(final F<A, F<B, C>> f, final V6<B> bs) {
return bs.apply(map(f));
}
/**
* Zips this vector with the given vector to produce a vector of pairs.
*
* @param bs The vector to zip this vector with.
* @return A new vector with a length the same as the shortest of this vector and the given
* vector.
*/
public <B> V6<P2<A, B>> zip(final V6<B> bs) {
final F<A, F<B, P2<A, B>>> __2 = p2();
return zipWith(__2, bs);
}
/**
* Zips this vector with the given vector to produce a vector of vectors.
*
* @param bs The vector to zip this vector with.
* @return A new vector of vectors.
*/
public V6<V2<A>> vzip(final V6<A> bs) {
final F2<A, A, V2<A>> __2 = V.v2();
return zipWith(curry(__2), bs);
}
/**
* Returns a function that transforms a vector-6 to a stream of its elements.
*
* @return a function that transforms a vector-6 to a stream of its elements.
*/
public static <A> F<V6<A>, Stream<A>> toStream_() {
return new F<V6<A>, Stream<A>>() {
public Stream<A> f(final V6<A> v) {
return v.toStream();
}
};
}
/**
* Returns a function that transforms a vector-6 to the equivalent product-6.
*
* @return a function that transforms a vector-6 to the equivalent product-6.
*/
public static <A> F<V6<A>, P6<A, A, A, A, A, A>> p_() {
return new F<V6<A>, P6<A, A, A, A, A, A>>() {
public P6<A, A, A, A, A, A> f(final V6<A> v) {
return v.p();
}
};
}
/**
* A first-class function to get the first element of a vector.
*
* @return a function that gets the first element of a given vector.
*/
public static <A> F<V6<A>, A> __1() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._1();
}
};
}
/**
* A first-class function to get the second element of a vector.
*
* @return a function that gets the second element of a given vector.
*/
public static <A> F<V6<A>, A> __2() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._2();
}
};
}
/**
* A first-class function to get the third element of a vector.
*
* @return a function that gets the third element of a given vector.
*/
public static <A> F<V6<A>, A> __3() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._3();
}
};
}
/**
* A first-class function to get the fourth element of a vector.
*
* @return a function that gets the fourth element of a given vector.
*/
public static <A> F<V6<A>, A> __4() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._4();
}
};
}
/**
* A first-class function to get the fifth element of a vector.
*
* @return a function that gets the fifth element of a given vector.
*/
public static <A> F<V6<A>, A> __5() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._5();
}
};
}
/**
* A first-class function to get the sixth element of a vector.
*
* @return a function that gets the sixth element of a given vector.
*/
public static <A> F<V6<A>, A> __6() {
return new F<V6<A>, A>() {
public A f(final V6<A> v) {
return v._6();
}
};
}
}