Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Created January 20, 2021 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomwhoiscontrary/1fcb36d2b9d45ae32eeacfd083c42902 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/1fcb36d2b9d45ae32eeacfd083c42902 to your computer and use it in GitHub Desktop.
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