Last active
December 19, 2015 18:18
-
-
Save leafo/5997612 to your computer and use it in GitHub Desktop.
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
-- Iterate through each item in each array table passed to iterator | |
-- doesn't have any allocations | |
all_values = do | |
y = coroutine.yield | |
free_workers = setmetatable {}, __mode: "k" | |
process_head = (skip, first, ...) -> | |
y nil if skip | |
return if first == nil | |
for item in *first | |
y item | |
process_head false, ... | |
main = (worker) -> | |
while true | |
process_head true, y nil | |
free_workers[worker] = true | |
get_free = -> | |
if worker = next free_workers | |
free_workers[worker] = nil | |
worker | |
else | |
worker = coroutine.wrap main | |
worker worker | |
worker | |
(...) -> | |
iter = get_free! | |
iter ... | |
iter | |
-- usage: | |
list1 = {1,2,3,4} | |
list2 = {5,6,7,8} | |
list3 = {9,10,11,12} | |
for item in all_values list1, list2, list3 | |
print item | |
-- prints 1,2,3,4,5,6,7,8,9,10,11,12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment