Created
December 17, 2015 15:40
-
-
Save asterite/6a2fd48c95cfda3f35a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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