Skip to content

Instantly share code, notes, and snippets.

@vslinko
Created October 28, 2013 23:32
Show Gist options
  • Save vslinko/7206675 to your computer and use it in GitHub Desktop.
Save vslinko/7206675 to your computer and use it in GitHub Desktop.
matrix iterator written for collegue
class MatrixIterator
positions: []
constructor: (@arrays) ->
@reset()
reset: ->
@hasNext = true
for i in [0...@arrays.length]
@positions[i] = 0
next: ->
unless @hasNext
return null
result = []
for i in [0...@arrays.length]
result.push @arrays[i][@positions[i]]
@hasNext = false
for i in [0...@arrays.length]
@positions[i] += 1
if @positions[i] < @arrays[i].length
@hasNext = true
break
else
@positions[i] = 0
result
it = new MatrixIterator [
[11000254, 11000255, 11000256]
[11000257, 11000258]
[11000259, 11000260]
]
while combination = it.next()
console.log combination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment