Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stopping an infinite stream  RSS feed

 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

for some reasom I want to split a digital number into a list of the individual
digits. (it has absolutely nothing to do with converting an octal number to a decimal one,
a topic in another forum )
Now, we all know the java 7 solution:

Three lines at most. But I was looking for a translation of this routine into some java 8
version.

This sprang to mind:

The question is what should these '???' be?
In principal, this is an infinite sequence, so we should stop it somehow.
This doesn't work:

This almost works:

But this looks horrible...
The next question is: what terminating operation is needed? I tried close(), but that didn't work.
My 'temp' list remained empty, indicating that the stream was not executed.
So I resorted to:

And that gave me my list.

Questions:
* anyone with a simpler way to imitate a while() loop?
* here I was lucky to know the number of the loops beforehand. What if I don't know this?

StackOverflow gave some very complicated spliterator and a nice 'takeWhile' as solutions.
Unfortunately, this 'takeWhile' seems to be java 9.

Greetz,
Piet



 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:This almost works:


len isn't always right here. Try calling it with 10. You get len as 1 rather than 2.

There really isn't a correct terminal operation because this isn't really an infinite stream. If I *had* to use an infinite stream here, I'd do:



The idea is to use a sentinel value to tell anyMatch when to stop looking. Obviously, this only works if you know that the stream isn't really infinite (or it will never end.)
 
Piet Souris
Rancher
Posts: 1783
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jeanne,

thank you for your reply.
Jeanne Boyarsky wrote:
len isn't always right here. Try calling it with 10. You get len as 1 rather than 2.

Correct, I noticed it too and used n + .01 instead.


There really isn't a correct terminal operation because this isn't really an infinite stream. If I *had* to use an infinite stream here, I'd do:



The idea is to use a sentinel value to tell anyMatch when to stop looking.

Of course! How simple... can't believe I didn't think of this that myself.
I now have it as:

Obviously, this only works if you know that the stream isn't really infinite (or it will never end.)

Sometimes you don't know upfront, think of the famous '3N+1' problem.

Well, I think I'll stick to the simple 'while()'.

Again, thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like it for the reason that it modifies a list that is outside the direct scope of the lambda. You're forcing functional code to do something that's not referentially transparent. It's not what functional code is designed for.

Yes, takeWhile() has been on my wishlist the day Java 8 came out. You can implement it yourself using StreamSupport.stream() but it's a bit of a pain.

Until then, I think plain old while loop is the way to go.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't like it for the reason that it modifies a list that is outside the direct scope of the lambda. You're forcing functional code to do something that's not referentially transparent. It's not what functional code is designed for.

Agreed! I even wrote "If I *had* to use an infinite stream here". The code is harder to read than the while loop version. It was an interesting exercise though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!