Skip to content

Instantly share code, notes, and snippets.

@asterite
Created December 17, 2015 15:40
Show Gist options
  • Save asterite/6a2fd48c95cfda3f35a4 to your computer and use it in GitHub Desktop.
Save asterite/6a2fd48c95cfda3f35a4 to your computer and use it in GitHub Desktop.
require "spec"
class Counter(B)
include Iterator(Array(B))
def initialize(@size : Int, @range : Range(B, E))
@array = Array.new(@size, @range.begin)
@stop = false
end
def next
return stop if @stop == true
value = @array.dup
i = 0
while true
if @array[i] == @range.end
if i == @array.size - 1
@stop = true
break
end
@array[i] = @range.begin
i += 1
next
end
@array[i] = @array[i].succ
break
end
value
end
def rewind
@array.fill(@range.begin)
@stop = false
self
end
end
describe Counter do
it "counts" do
counter = Counter.new(2, 'a'..'b')
counter.to_a.should eq([['a', 'a'], ['b', 'a'], ['a', 'b'], ['b', 'b']])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment