Repository navigation
Expand file tree
/
Copy pathSeq.java
More file actions
149 lines (130 loc) · 3.57 KB
/
Copy pathSeq.java
File metadata and controls
149 lines (130 loc) · 3.57 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
package generator;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@FunctionalInterface
public interface Seq<T> {
void consumer(Consumer<T> consumer);
/**
* {@link Stream#map(Function)}
*/
default <R> Seq<R> map(Function<T, R> function) {
return c -> consumer(t -> c.accept(function.apply(t)));
}
/**
* {@link Stream#flatMap(Function)}
*/
default <R> Seq<R> flatMap(Function<T, Seq<R>> function) {
return c -> consumer(t -> function.apply(t).consumer(c));
}
/**
* {@link Stream#filter(Predicate)}
*/
default Seq<T> filter(Predicate<T> predicate) {
return c -> consumer(t -> {
if (predicate.test(t)) {
c.accept(t);
}
});
}
default void consumerTillStop(Consumer<T> consumer) {
try {
consumer(consumer);
} catch (StopException ignore) {
}
}
/**
* {@link Stream#limit(long)}
*/
default Seq<T> take(long n) {
return c -> {
AtomicLong i = new AtomicLong();
consumerTillStop(t -> {
if (i.incrementAndGet() > n) {
throw StopException.INSTANCE;
}
c.accept(t);
});
};
}
/**
* {@link Stream#skip(long)}
*/
default Seq<T> drop(long n) {
return c -> {
AtomicLong i = new AtomicLong();
consumerTillStop(t -> {
if (i.incrementAndGet() > n) {
c.accept(t);
}
});
};
}
/**
* {@link Stream#peek(Consumer)}
*/
default Seq<T> onEach(Consumer<T> consumer) {
return c -> consumer(t -> {
consumer.accept(t);
c.accept(t);
});
}
default List<T> toList() {
List<T> list = new ArrayList<>();
consumer(list::add);
return list;
}
default Set<T> toSet() {
Set<T> set = new HashSet<>();
consumer(set::add);
return set;
}
/**
* {@link Stream#distinct()}
*/
default Seq<T> distinct() {
return toSet()::forEach;
}
/**
* {@link Collectors#joining()}
*/
default String joining() {
StringBuilder stringBuilder = new StringBuilder();
consumer(stringBuilder::append);
return stringBuilder.toString();
}
/**
* {@link Stream#collect(Collector)}
*/
default <A, R> R collect(Collector<T, A, R> collector) {
A a = collector.supplier().get();
consumer(t -> collector.accumulator().accept(a, t));
return collector.finisher().apply(a);
}
default Stream<T> toStream() {
Stream.Builder<T> builder = Stream.builder();
consumer(builder);
return builder.build();
}
static <T> Seq<T> fromStream(Stream<T> stream) {
return stream::forEach;
}
default <E, R> Seq<R> zip(Iterable<E> iterable, BiFunction<T, E, R> function) {
return c -> {
Iterator<E> iterator = iterable.iterator();
consumerTillStop(t -> {
if (iterator.hasNext()) {
c.accept(function.apply(t, iterator.next()));
} else {
throw StopException.INSTANCE;
}
});
};
}
}