From 14e86618d053f72ba678db87649e62c202af1948 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 20 Sep 2022 20:02:32 +0200 Subject: [PATCH] Add iterator table / list / set comprehensions --- iterator.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/iterator.lua b/iterator.lua index dde4b53..0ce040c 100644 --- a/iterator.lua +++ b/iterator.lua @@ -109,4 +109,33 @@ function iterator.standard_deviation(...) return (sum / count)^.5 end +-- Comprehensions ("collectors") + +-- Shorthand for `for k, v in ... do t[k] = v end` +function iterator.to_table(...) + local t = {} + for k, v in ... do + t[k] = v + end + return t +end + +-- Shorthand for `for k in ... do t[#t + 1] = k end` +function iterator.to_list(...) + local t = {} + for k in ... do + t[#t + 1] = k + end + return t +end + +-- Shorthand for `for k in ... do t[k] = true end` +function iterator.to_set(...) + local t = {} + for k in ... do + t[k] = true + end + return t +end + return iterator \ No newline at end of file