Created
January 20, 2021 12:32
-
-
Save tomwhoiscontrary/1fcb36d2b9d45ae32eeacfd083c42902 to your computer and use it in GitHub Desktop.
A function that does something with streams ... but what?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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