Skip to content

Instantly share code, notes, and snippets.

@enricostano
Last active August 29, 2015 14:14
Show Gist options
  • Save enricostano/21c0edbb5e2135a16542 to your computer and use it in GitHub Desktop.
Save enricostano/21c0edbb5e2135a16542 to your computer and use it in GitHub Desktop.
lazy iterator
arr = [1, 2, 3, 4, 5, 6, 7, 8]
[
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7],
[4, 5, 6, 7, 8],
[5, 6, 7, 8],
[6, 7, 8],
[7, 8],
[8]
]
def chunk_by(array, chunk_size)
r = array.dup
i = 0
while i < r.length do
i += yield(r[i , chunk_size])
end
end
chunk_by(arr, 3) do |chunk|
p chunk
offset = 1
if chunk == [3, 4, 5]
puts 'Hola'
offset += chunk.size
end
offset
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment