Skip to content

Commit a1c95eb

Browse files
committed
+ any, +all, +compose, +forloop, +repeat; fix take remove() passthrough
1 parent 3399309 commit a1c95eb

File tree

15 files changed

+755
-22
lines changed

15 files changed

+755
-22
lines changed

src/main/java/ix/Ix.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,49 +161,43 @@ public static <T1, T2, T3, T4, R> Ix<R> zip(
161161
return new IxZip4<T1, T2, T3, T4, R>(it1, it2, it3, it4, zipper);
162162
}
163163

164-
public static <T> Ix<T> repeat(T value) {
165-
// TODO implement
166-
throw new UnsupportedOperationException();
164+
public static <T> Ix<T> repeatValue(T value) {
165+
return new IxRepeat<T>(value);
167166
}
168167

169-
public static <T> Ix<T> repeat(T value, long count) {
170-
// TODO implement
171-
throw new UnsupportedOperationException();
168+
public static <T> Ix<T> repeatValue(T value, long count) {
169+
return new IxRepeatCount<T>(value, count);
172170
}
173171

174-
public static <T> Ix<T> repeat(T value, Pred0 stopPredicate) {
175-
// TODO implement
176-
throw new UnsupportedOperationException();
172+
public static <T> Ix<T> repeatValue(T value, Pred0 stopPredicate) {
173+
return new IxRepeatPredicate<T>(value, stopPredicate);
177174
}
178175

179-
public static <T> Ix<T> forloop(T seed, Pred<? super T> condition, Func1<? super T, ? extends T> next) {
180-
// TODO implement
181-
throw new UnsupportedOperationException();
176+
public static <T, R> Ix<R> forloop(T seed, Pred<? super T> condition,
177+
Func1<? super T, ? extends T> next,
178+
Func1<? super T, ? extends R> selector) {
179+
return new IxForloop<T, R>(seed, condition, selector, next);
182180
}
183181

184182
//---------------------------------------------------------------------------------------
185183
// Instance operators
186184
//---------------------------------------------------------------------------------------
187185

188186
public final <R> R as(Func1<? super Ix<T>, R> transformer) {
189-
// TODO implement
190-
throw new UnsupportedOperationException();
187+
return transformer.call(this);
191188
}
192189

193190
public final <R> Ix<R> compose(Func1<? super Ix<T>, ? extends Iterable<? extends R>> transformer) {
194-
// TODO implement
195-
throw new UnsupportedOperationException();
191+
return new IxCompose<T, R>(this, transformer);
196192
}
197193

198194
public final Ix<Boolean> any(Pred<? super T> predicate) {
199-
// TODO implement
200-
throw new UnsupportedOperationException();
195+
return new IxAny<T>(this, predicate);
201196
}
202197

203198

204199
public final Ix<Boolean> all(Pred<? super T> predicate) {
205-
// TODO implement
206-
throw new UnsupportedOperationException();
200+
return new IxAll<T>(this, predicate);
207201
}
208202

209203
public final Ix<Boolean> hasElements() {

src/main/java/ix/IxAll.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
final class IxAll<T> extends IxSource<T, Boolean> {
22+
23+
final Pred<? super T> predicate;
24+
25+
public IxAll(Iterable<T> source, Pred<? super T> predicate) {
26+
super(source);
27+
this.predicate = predicate;
28+
}
29+
30+
@Override
31+
public Iterator<Boolean> iterator() {
32+
return new AllIterator<T>(source.iterator(), predicate);
33+
}
34+
35+
static final class AllIterator<T> extends IxSourceIterator<T, Boolean> {
36+
37+
final Pred<? super T> predicate;
38+
39+
public AllIterator(Iterator<T> it, Pred<? super T> predicate) {
40+
super(it);
41+
this.predicate = predicate;
42+
}
43+
44+
@Override
45+
protected boolean moveNext() {
46+
Iterator<T> it = this.it;
47+
Pred<? super T> pred = predicate;
48+
49+
while (it.hasNext()) {
50+
if (!pred.test(it.next())) {
51+
hasValue = true;
52+
value = false;
53+
done = true;
54+
return true;
55+
}
56+
}
57+
58+
hasValue = true;
59+
value = true;
60+
done = true;
61+
return true;
62+
}
63+
64+
}
65+
}

src/main/java/ix/IxAny.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
final class IxAny<T> extends IxSource<T, Boolean> {
22+
23+
final Pred<? super T> predicate;
24+
25+
public IxAny(Iterable<T> source, Pred<? super T> predicate) {
26+
super(source);
27+
this.predicate = predicate;
28+
}
29+
30+
@Override
31+
public Iterator<Boolean> iterator() {
32+
return new AnyIterator<T>(source.iterator(), predicate);
33+
}
34+
35+
static final class AnyIterator<T> extends IxSourceIterator<T, Boolean> {
36+
37+
final Pred<? super T> predicate;
38+
39+
public AnyIterator(Iterator<T> it, Pred<? super T> predicate) {
40+
super(it);
41+
this.predicate = predicate;
42+
}
43+
44+
@Override
45+
protected boolean moveNext() {
46+
Iterator<T> it = this.it;
47+
Pred<? super T> pred = predicate;
48+
49+
while (it.hasNext()) {
50+
if (pred.test(it.next())) {
51+
hasValue = true;
52+
value = true;
53+
done = true;
54+
return true;
55+
}
56+
}
57+
58+
hasValue = true;
59+
value = false;
60+
done = true;
61+
return true;
62+
}
63+
64+
}
65+
}

src/main/java/ix/IxCompose.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ix;
2+
3+
import java.util.Iterator;
4+
5+
import rx.functions.Func1;
6+
7+
final class IxCompose<T, R> extends IxSource<T, R> {
8+
9+
final Func1<? super Ix<T>, ? extends Iterable<? extends R>> transformer;
10+
11+
public IxCompose(Iterable<T> source, Func1<? super Ix<T>, ? extends Iterable<? extends R>> transformer) {
12+
super(source);
13+
this.transformer = transformer;
14+
}
15+
16+
@SuppressWarnings("unchecked")
17+
@Override
18+
public Iterator<R> iterator() {
19+
return (Iterator<R>)transformer.call(from(source)).iterator();
20+
}
21+
22+
}

src/main/java/ix/IxForloop.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
import rx.functions.Func1;
22+
23+
final class IxForloop<T, R> extends Ix<R> {
24+
25+
final T seed;
26+
27+
final Pred<? super T> condition;
28+
29+
final Func1<? super T, ? extends R> selector;
30+
31+
final Func1<? super T, ? extends T> next;
32+
33+
public IxForloop(T seed, Pred<? super T> condition, Func1<? super T, ? extends R> selector,
34+
Func1<? super T, ? extends T> next) {
35+
this.seed = seed;
36+
this.condition = condition;
37+
this.selector = selector;
38+
this.next = next;
39+
}
40+
41+
@Override
42+
public Iterator<R> iterator() {
43+
return new ForloopIterator<T, R>(seed, condition, selector, next);
44+
}
45+
46+
static final class ForloopIterator<T, R> extends IxBaseIterator<R> {
47+
48+
T index;
49+
50+
final Pred<? super T> condition;
51+
52+
final Func1<? super T, ? extends R> selector;
53+
54+
final Func1<? super T, ? extends T> next;
55+
56+
boolean exceptFirst;
57+
58+
public ForloopIterator(T index, Pred<? super T> condition, Func1<? super T, ? extends R> selector,
59+
Func1<? super T, ? extends T> next) {
60+
this.index = index;
61+
this.condition = condition;
62+
this.selector = selector;
63+
this.next = next;
64+
}
65+
66+
@Override
67+
protected boolean moveNext() {
68+
T i = index;
69+
70+
if (exceptFirst) {
71+
i = next.call(i);
72+
index = i;
73+
} else {
74+
exceptFirst = true;
75+
}
76+
77+
if (!condition.test(i)) {
78+
done = true;
79+
return false;
80+
}
81+
82+
value = selector.call(i);
83+
hasValue = true;
84+
85+
return true;
86+
}
87+
}
88+
}

src/main/java/ix/IxRepeat.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
final class IxRepeat<T> extends Ix<T> {
22+
23+
final T value;
24+
25+
public IxRepeat(T value) {
26+
this.value = value;
27+
}
28+
29+
@Override
30+
public Iterator<T> iterator() {
31+
return new RepeatIterator<T>(value);
32+
}
33+
34+
static final class RepeatIterator<T> implements Iterator<T> {
35+
36+
final T value;
37+
38+
public RepeatIterator(T value) {
39+
this.value = value;
40+
}
41+
42+
@Override
43+
public boolean hasNext() {
44+
return true;
45+
}
46+
47+
@Override
48+
public T next() {
49+
return value;
50+
}
51+
52+
@Override
53+
public void remove() {
54+
throw new UnsupportedOperationException();
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)