Q on Patterns and Streams
Johannes Kuhn
info at j-kuhn.de
Wed Jan 20 00:30:36 UTC 2021
Turns out I sometimes write code that looks like this:
foos.stream().filter(e -> e instanceof Foo).map(e -> (Foo) e)...
or
foos.stream().filter(Foo.class::isInstance).map(Foo.class::cast)...
Nicolas Fränkel has even written a blog post about this[1].
Anyway, this can be expressed with a single stream operation:
foos.stream().flatMap(e -> e instanceof Foo foo ? Stream.of(foo) :
Stream.of())...
I expect that form to work with any pattern in the future.
But it doesn't look great.
Are there any plans around this?
Or is this something patterns shouldn't be used for?
In a more general way - will there be a way to pass a pattern as an
argument to some other function?
- Johannes
[1]: https://blog.frankel.ch/changing-coding-habits-stream-friendly/
More information about the amber-dev
mailing list