Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function that does something with streams ... but what?
class Foo {
public static <T> Function<T, Stream<T>> foo(Predicate<T> predicate) {
return new Function<>() {
private T previous;
@Override
public Stream<T> apply(T current) {
if (predicate.test(current)) {
if (previous != null) {
T previous = this.previous;
this.previous = null;
return Stream.of(previous, current);
} else {
return Stream.of(current);
}
} else {
previous = current;
return Stream.empty();
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment