Skip to content

Commit

Permalink
daveD & dan - converted tap() to return the sequenc instead of void.
Browse files Browse the repository at this point in the history
  • Loading branch information
david denton committed Feb 20, 2015
1 parent 7978280 commit e2889f5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/com/googlecode/totallylazy/Iterators.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.googlecode.totallylazy.iterators.PartitionIterator;
import com.googlecode.totallylazy.iterators.PeekingIterator;
import com.googlecode.totallylazy.iterators.RangerIterator;
import com.googlecode.totallylazy.iterators.ReadOnlyIterator;
import com.googlecode.totallylazy.iterators.RepeatIterator;
import com.googlecode.totallylazy.iterators.TakeWhileIterator;
import com.googlecode.totallylazy.iterators.UnfoldRightIterator;
Expand Down Expand Up @@ -469,6 +470,22 @@ public static <T> int indexOf(Iterator<? extends T> iterator, T instance) {
return -1;
}

public static <T> Iterator<T> tap(final Iterator<? extends T> iterator, final Callable1<? super T, ?> callable) {
return new ReadOnlyIterator<T>(){
@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public T next() {
T next = iterator.next();
call(callable, next);
return next;
}
};
}

public static class functions {
public static <T> Function2<Iterable<? extends T>, Iterable<? extends T>, Iterable<T>> join() {
return new Function2<Iterable<? extends T>, Iterable<? extends T>, Iterable<T>>() {
Expand Down
2 changes: 1 addition & 1 deletion src/com/googlecode/totallylazy/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void forEach(final Callable1<? super T, ?> runnable) {
Sequences.forEach(this, runnable);
}

public void tap(final Block<? super T> block) { Sequences.tap(this, block); }
public Sequence<T> tap(final Callable1<? super T, ?> callable) { return Sequences.tap(this, callable); }

public <S> Sequence<S> mapConcurrently(final Callable1<? super T, S> callable) {
return Sequences.mapConcurrently(this, callable);
Expand Down
9 changes: 7 additions & 2 deletions src/com/googlecode/totallylazy/Sequences.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,13 @@ public static <T> void forEachConcurrently(final Iterable<? extends T> iterable,
mapConcurrently(iterable, runnable, executor).realise();
}

public static <T> void tap(final Iterable<? extends T> iterable, final Block<? super T> block) {
Iterators.forEach(iterable.iterator(), block);
public static <T> Sequence<T> tap(final Iterable<? extends T> iterable, final Callable1<? super T, ?> callable) {
return new Sequence<T>() {
@Override
public Iterator<T> iterator() {
return Iterators.tap(iterable.iterator(), callable);
}
};
}

public static <T> T first(final Iterable<? extends T> iterable) {
Expand Down
5 changes: 3 additions & 2 deletions test/com/googlecode/totallylazy/SequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,12 @@ public void execute(Integer value) throws InterruptedException {
@Test
public void supportsTap() throws Exception {
final int[] sum = {0};
sequence(1, 2).tap(new Block<Integer>() {
Sequence<Integer> result = sequence(1, 2).tap(new Block<Integer>() {
public void execute(Integer value) {
sum[0] += value;
}
});
}).realise();
assertThat(result, hasExactly(1, 2));
assertThat(sum[0], is(3));
}

Expand Down

0 comments on commit e2889f5

Please sign in to comment.