3

Say I have something like

something.stream()
         .filter(filterMethod) // Same
         .map(mapMethod)       //
         .map(somethingElse)
         .filter(filterMethod) // Same
         .map(mapMethod)       //
         .filter(otherFilter)
         .filter(filterMethod) // Same
         .map(mapMethod)       // 

Could I create custom function on Stream and convert .filter().map() to one method? Implementing own Stream seems to overkill. It would be nice to have some short lambda function or method like

Stream<T> fooFiterMap(Stream<T> stream){
    return stream.filter(filterMethod).map(mapMethod);
}

and then reduce my something stream into

something.stream()
         .fooFilterMap()     // New
         .map(somethingElse)
         .fooFilterMap()     // New
         .filter(otherFilter)
         .fooFilterMap()     // New
Dawid Laszuk
  • 1,773
  • 21
  • 39
  • 1
    You mean like `something.stream().myFilterAndMap().myFilterAndMap().myFilterAndMap()`? – D. Kovács Oct 10 '17 at 18:00
  • That seems pretty hard to do. My guess is you would have to write your own Stream interface (extends Stream) to begin with. – Paul Lemarchand Oct 10 '17 at 18:15
  • @PaulLemarchand: _if_ that is the question, then indeed it is, and then it's a duplicate of https://stackoverflow.com/questions/30685623/how-to-implement-a-java-stream :) – D. Kovács Oct 10 '17 at 18:21
  • Possible duplicate of [How to implement a Java stream?](https://stackoverflow.com/questions/30685623/how-to-implement-a-java-stream) – D. Kovács Oct 10 '17 at 18:21
  • Writing own interface seems like an overkill. Isn't there something like: Stream myFilterAndMap(Stream stream){ return stream.filter(filterMethod).map(mapMethod); } – Dawid Laszuk Oct 10 '17 at 18:24
  • what would you gain with this? one more level of indirection? – Eugene Oct 10 '17 at 19:28
  • @Eugene It would reduce number of steps many-fold and, using proper naming convention, we could earn some clarity. – Dawid Laszuk Oct 10 '17 at 19:41
  • `number of steps many-fold`? I wish I could say I understood what you just said – Eugene Oct 10 '17 at 19:42
  • @Eugene Seeing your answer I understand what you thought of. My use case is as simple as: stream.myFilterMap().map(somethingElse).myFilterMap().filter(otherFilter); and that .myFilterMap() is repeated many times in many methods. – Dawid Laszuk Oct 10 '17 at 19:45
  • 2
    yeah... you can't do that obviously without extending `Stream` in the first place (`StreamEx` library has done it for example); but looks like a big overkill here – Eugene Oct 10 '17 at 19:53
  • @Eugene when you say "obviously" it's really not obvious to me why. Any chance you could add this explanation to your answer? – Dawid Laszuk Oct 11 '17 at 17:13

1 Answers1

2

You can obviously write your own one:

<T> Stream<T> fooFiterMap(Stream<T> stream, Predicate<T> predicate, UnaryOperator<T> function) {
    return stream.filter(predicate).map(function);
}

But the real question is why? It's too verbose? If so, than I'll argue - I like the chaining of filter and map more - bit it's subjective I guess. If you think about multiple objects created, than just think about the fact that these are probably stateless lambdas used on the same call-site, thus a single instance of Predicate and Function.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • 2
    The OP doesn’t want a method for combining an arbitrary `Predicate` and an arbitrary `Function`/`UnaryOperator`. The OP wants to repeatedly apply the *same* filter and map function, so that not specifying the functions again would truly shorten the code, though, I admittedly can’t imagine real life scenarios for that… – Holger Oct 11 '17 at 09:19
  • @Holger I do: your own snippet of `filter(T.class::isInstance).map(T.class::cast)` – Post Self Jan 13 '19 at 10:39
  • @PostSelf how would `T.class` work? I don't get your example here – Eugene Jan 13 '19 at 12:59
  • Probably not, I don't get this whole Java syntax, but you should get the meaning (filtering and downcasting). – Post Self Jan 13 '19 at 20:31