From 6d527b0471252b956c0101820e8ef1a9f42c02a6 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 7 Sep 2022 11:48:24 +0200 Subject: [PATCH] Move func.iterate to iterator.foreach --- func.lua | 9 --------- iterator.lua | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/func.lua b/func.lua index 61e8b0e..70c2178 100644 --- a/func.lua +++ b/func.lua @@ -49,15 +49,6 @@ function memoize(func) }) end --- Equivalent to `for x, y, z in iterator, state, ... do callback(x, y, z) end` -function iterate(callback, iterator, state, ...) - local function loop(...) - if ... == nil then return end - callback(...) - return loop(iterator(state, ...)) - end - return loop(iterator(state, ...)) -end function for_generator(caller, ...) local co = coroutine.create(function(...) diff --git a/iterator.lua b/iterator.lua index eefeb43..fdab8de 100644 --- a/iterator.lua +++ b/iterator.lua @@ -4,6 +4,16 @@ local add = modlib.func.add --+ Otherwise they will be applied to the indices. local iterator = {} +-- Equivalent to `for x, y, z in iterator, state, ... do callback(x, y, z) end` +function iterator.foreach(callback, iterator, state, ...) + local function loop(...) + if ... == nil then return end + callback(...) + return loop(iterator(state, ...)) + end + return loop(iterator(state, ...)) +end + function iterator.range(from, to, step) if not step then if not to then