Last active
August 29, 2015 14:14
-
-
Save enricostano/21c0edbb5e2135a16542 to your computer and use it in GitHub Desktop.
lazy iterator
This file contains 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
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