• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you stop a Stream on a sentinel value?

 
Marshal
Posts: 79151
377
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    IntelliJ IDE Clojure Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the cow whoever it was.
    Did anybody notice the error in my original post?
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 15484
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    https://coderanch.com/t/664547/java/Java-data-directed#3093922
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic