Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to check if an Iterator is complete?

Tags:

crystal-lang

As the title states, I'd like to know the proper way to check if an iterator is complete.

I couldn't find anything in the docs but I found something like this in the source:

iter.next.is_a? Iterator::Stop

Toy example:

a = "a世c"
b = a.each_char

puts b.next # a
puts b.next # 世
puts b.next # c
puts b.next # #<Iterator::Stop:0x8ccbff8>

if b.next.is_a? Iterator::Stop
  puts "DONE" # successfully prints DONE
end

Is this correct and proper or is there a different way I should use.

like image 228
Lye Fish Avatar asked Oct 20 '22 01:10

Lye Fish


1 Answers

Yes, that's the proper way. But most of the time you don't need to deal with next, you just chain iterators and get a result out of them.

like image 153
asterite Avatar answered Dec 19 '22 12:12

asterite