Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you stop a Stream on a sentinel value?  RSS feed

 
Marshal
Posts: 58828
179
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been wondering about this for a week or two. I don't mean limit(), which stops after a predetermined number of elements. Maybe I might want to run a Stream until one of (numeric) elements equals 0. It is easy enough to do with loops:-
  • 1: Let's imagine that maxCount is initialised to 0x7fff_ffff_ffff_ffffL or MIN_VALUE.
  • 2: Let's imagine you can reassign it without breaching the rules about effectively final.
  • 3: Let's imagine I can write even worse code. That would be quite an achievement, however.
  • The nearest I had got to it was something like this:-Horrible, isn't it? How would you do it?

    I was looking through the Stream documentation an hour or so ago wondering whether they had added a terminateIf() method and, lo and behold, found this method. It's new in Java9 Brilliant. Exactly the sort of thing I was thinking of.I actually found this method first, which appears to be the opposite of takeWhile, so I thought there was bound to be the counterpart, only if it starts with t it is at the bottom of the method summary.
     
    Ranch Hand
    Posts: 115
    11
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oooo I want to play.

    So, assuming that the operation you take in the loop is some sort of aggregation, then effectively we want to implement a short-circuiting reduction on the stream. And since the problem statement allows for dirty hacks, we can have fun with code that should never see the light of day.

    Imagine our aim was to calculate the product of a stream of integers:

    Here we're processing three more numbers than we needed to, and would like to terminate early when we encounter that 0. Clojure actually has a pretty cool function for that called reduced that "forces" the reduction to terminate with the value supplied to that function. What if we tried something similar here with some ugly trickery?
     
    Campbell Ritchie
    Marshal
    Posts: 58828
    179
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the cow whoever it was.
    Did anybody notice the error in my original post?
     
    Master Rancher
    Posts: 2539
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It took me a while to find this old post, but maybe it can help. It is certainly an alternative to the code shown.
    Stephan did show how to implement 'takeWhile' (I couldn't find it, but maybe he can show it again).
     
    Saloon Keeper
    Posts: 8726
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    https://coderanch.com/t/664547/java/Java-data-directed#3093922
     
    Campbell Ritchie
    Marshal
    Posts: 58828
    179
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is another potential takeWhile implementation in the MEAP Java 8-9 in Action 2/e by Urma Fusco and Mycroft, Manning ?2017? pages 143‑4.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!