I have a function defined as a C binding that provides a generic for iterator:
for el in doc:each() do ... end
I want to write an iterator in Lua that iterates using this function, but return a modification of each result. How can I do this?
Edit: I'm sure my iterator has to start something like this, but I'm getting lost in the body of the function.
function myiterator()
local f, c, v = doc:each()
return (function(c2, v2)
-- ??
end), ??, ??
end
function myiterator()
local generator, state, prev_x = doc:each()
local function my_generator()
local x, y = generator(state, prev_x)
if x ~= nil then
prev_x = x
-- modify x, y
local modified_x = x + 100
local modified_y = "("..y..")"
-- modified_x must be non-nil
return modified_x, modified_y
end
end
return my_generator
end
Before:
local doc = {each = function() return ipairs{"aa", "bb", "cc"} end}
for x, y in doc:each() do
print(x, y)
end
Output:
1 aa
2 bb
3 cc
After:
local doc = {each = function() return ipairs{"aa", "bb", "cc"} end}
-- insert myiterator definition here
for x, y in myiterator() do
print(x, y) -- now x and y are modified
end
Output:
101 (aa)
102 (bb)
103 (cc)
I don't know how your C function works, but here is an iterator function that should do what you are looking for. It calls an iterator triplet repeatedly, takes the first return value, and calls a function on it to return a new value:
function map(transformer, f, c, v)
return function()
v = f(c, v)
if v ~= nil then
return transformer(v)
end
end
end
For instance, this takes the keys in the table { 'a', 'b', 'c' } and squares them. The second return value (the values corresponding to the keys) is ignored:
for v in map(function (x) return x * x end, pairs { 'a', 'b', 'c' }) do
print(v)
end
You can do map(function (elem) return do_something_to(elem) end, doc:each()).
It's easier to read the map function when it's written using a coroutine:
function map(transformer, f, c, v)
return coroutine.wrap(function ()
for val in f, c, v do
coroutine.yield(transformer(val))
end
end)
end
For completeness, either of these functions would allow you to use two return values from the original iterator triplet:
function map2(transformer, f, c, v)
return function()
local v2
v, v2 = f(c, v)
if v ~= nil then
return transformer(v, v2)
end
end
end
function map(transformer, f, c, v)
return coroutine.wrap(function ()
for v, v2 in f, c, v do
coroutine.yield(transformer(v, v2))
end
end)
end
for v in map2(function (a, b) return a .. b end, pairs { 'a', 'b', 'c' }) do
print(v)
end
-- This prints out:
-- 1a
-- 2b
-- 3c
You could wrap the iterator into a coroutine, see also https://www.lua.org/pil/9.3.html
-- dummy object
local doc = {
each = function()
return pairs{ 11, 22, 33 }
end
}
local myiterator = coroutine.wrap(function()
local f, c, v = doc:each()
return f, c, v
end)
for f, c, v in myiterator() do
print(f, c, v)
end
$ lua test.lua
1 11 nil
2 22 nil
3 33 nil
Thanks to those that helped. I learned that my iterator function does not HAVE to return 2 or 3 values. This is a valid list-table iterator:
function every(list)
local i=0
return function() -- no arguments used!
i = i+1
return list[i]
end
end
for word in every{'foo', 'bar', 'jim'} do print(word) end
--> foo
--> bar
--> jim
Because every() does not return a second value (the "invariant" value), a nil is passed as the first parameter to the anonymous function on each iteration. The second value passed to that anonymous function is the value returned from the function on a previous pass (or the third return value from every() on the very first pass)...but if that does not help us iterate, we don't need to use it.
Because none of the answers so far exactly answered what I needed, here is the solution I ended up with:
function myiterator()
local f, c, v = doc:each()
return function()
v = f(c,v)
if v then
-- NOTE: Do NOT change the `v` variable here, because
-- f() may expect it to be exactly what it returned
-- in order to iterator properly on the next invocation.
local myvalue = mutate(v)
return myvalue
end
end
end
Related
I can usually work out how to code the iterators I want in LUA.
But recursive iterators have me beat.
For example, here's a Python recursive iterator that returns all items in a nested list:
def items(x):
if isinstance(x,(list,tuple)):
for y in x:
for z in items(y): yield z
else:
yield x
for x in items([10,20,[30,[40,50],60],[70,80]]): print(x)
This prints
10
20
30
40
50
60
70
80
But I can't get it going in Lua. I think it is because I don't know how to carry around the state of the recursive traversal from one step in the iteration to the next.
Suggestions?
FP style
local function items(tbl, outer_iter)
local index = 0
local function iter()
index = index + 1
return tbl[index]
end
return
function ()
while iter do
local v = iter()
if v == nil then
iter, outer_iter = outer_iter
elseif type(v) == "table" then
iter = items(v, iter)
else
return v
end
end
end
end
Coroutine style
local function items(tbl)
return coroutine.wrap(
function()
for _, v in ipairs(tbl) do
if type(v) == "table" then
local iter = items(v)
local v = iter()
while v ~= nil do
coroutine.yield(v)
v = iter()
end
else
coroutine.yield(v)
end
end
end
)
end
Usage example:
for x in items{10,20,{30,{},{40,50},60},{70,80}} do
print(x)
end
How can i transform this following string:
string = "1,2,3,4"
into a table:
table = {1,2,3,4}
thanks for any help ;)
Let Lua do the hard work:
s="1,2,3,4"
t=load("return {"..s.."}")()
for k,v in ipairs(t) do print(k,v) end
Below is the code adapted from the Scribunto extension to MediaWiki. It allows to split strings on patterns that can be longer than one character.
-- Iterator factory
function string:gsplit (pattern, plain)
local s, l = 1, self:len()
return function ()
if s then
local e, n = self:find (pattern, s, plain)
local ret
if not e then
ret = self:sub (s)
s = nil
elseif n < e then
-- Empty separator!
ret = self:sub (s, e)
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and self:sub (s, e - 1) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
-- Split function that returns a table:
function string:split (pattern, plain)
local ret = {}
for m in self:gsplit (pattern, plain) do
ret [#ret + 1] = m
end
return ret
end
-- Test:
local str = '1,2, 3,4'
print ('table {' .. table.concat (str:split '%s*,%s*', '; ') .. '}')
You can use gmatch and the pattern (%d+) to create an iterator and then populate the table.
local input = "1,2,3,4"
local output = {}
for v in input:gmatch("(%d+)") do
table.insert(output, tonumber(v))
end
for _,v in pairs(output) do
print(v)
end
The pattern (%d+) will capture any number of digits(0-9).
This is a narrow solution, it does not handle blank entries such as
input = "1,2,,4"
output = {1,2,4}
It also does not care what the delimitator is, or if it is even consistent for each entry.
input = "1,2 3,4"
output = {1,2,3,4}
Reference:
Lua 5.3 Manual, Section 6.4 – String Manipulation: string.gmatch
FHUG: Understanding Lua Patterns
local sometable = {a = "ag", b = "fa"}
for k, v in ipairs(sometable) do
print(k, v)
end
The code above is my effort, so how do i print a, b in that table?!
You are using the wrong iterator. ipairs is for sequences. For hash-like tables, use pairs instead:
for k, v in pairs(sometable) do
ipairs only traversers the array part of a table. What you can do is simply writing
print(sometable.a, sometable.b)
or you can cycle through both the dictionary and array parts of the table by using
for key, value in pairs(sometable)
You could also define your own iterator to only cycle through the dictionary part of the table. In my mind it would look like
function cycle(dict)
local contentarray = {}
for k, v in pairs(dict) do
contentarray[#contentarray + 1] = {k, v}
end
local n = 0
return function()
n = n + 1
if not contentarray[n] then
return
else
while type(contentarray[n][1]) ~= "string" do
n = n + 1
end
return contentarray[n][1], contentarray[n][2]
end
end
end
But that would be higly inefficient.
Is it possible to iterate through multiple Lua tables with the same loop?
For looping through indexed tables I can do something like this:
local t1 = {"a", "b", "c"}
local t2 = {"d", "e", "f"}
local num = #t1+#t2
for i=1, num, do
local j
local val
if i <= #t1 then
j = i
val = t1[j]
else
j = i-#t1
val = t2[j]
end
-- Do stuff
end
but how about key-value tables?
E.g. something like this:
local t1 = {a="a", b="b", c="c"}
local t2 = {d="d", e="e", f="f"}
for key, val in pairs(t1) or pairs(t2) do
print(key..": '"..val.."'")
end
should result in this:
a: 'a'
b: 'b'
c: 'c'
d: 'd'
e: 'e'
f: 'f'
function pairs(t, ...)
local i, a, k, v = 1, {...}
return
function()
repeat
k, v = next(t, k)
if k == nil then
i, t = i + 1, a[i]
end
until k ~= nil or not t
return k, v
end
end
local t1 = {a="a", b="b", c="c"}
local t2 = {d="d", e="e", f="f"}
for key, val in pairs(t1, t2) do
print(key..": '"..val.."'")
end
Note: this implementation does not respect __pairs metamethod.
For the given example, I think it is much more concise and clear to just wrap the loop in an outer loop that iterates the tables.
I am assuming the primary reason OP was looking for a solution other than two loops was to avoid having to write the inner logic twice. This is a good solution to that problem and only adds two lines of code:
local t1 = {a="a", b="b", c="c"}
local t2 = {d="d", e="e", f="f"}
for _, tbl in ipairs({ t1, t2 }) do
for key, val in pairs(tbl) do
print(key..": '"..val.."'")
end
end
While it's always nice to have an iterator like Egor's, a more efficient solution would simply be
local t1 = {a="a", b="b", c="c"}
local t2 = {d="d", e="e", f="f"}
for key, val in pairs(t1) do
print(key..": "..val)
end
for key, val in pairs(t2) do
print(key..": '"..val)
end
It's simple, concise, and easily understandable.
While reading Programming in Lua, I tried this example given in the book for operator overloading
Set = {}
mt = {}
mt.__add = Set.union
--create a new set with the values of the given list
function Set.new (l)
local set = {}
setmetatable (set, mt)
for _, v in ipairs (l) do
set [v] = true
end
return set
end
function Set.union (a, b)
local result = Set.new {}
for k in pairs (a) do result [k] = true end
for k in pairs (b) do result [k] = true end
return result
end
function Set.intersection (a, b)
local result = Set.new {}
for k in pairs (a) do result [k] = b[k] end
return result
end
function Set.tostring (set)
local l = {}
for e in pairs (set) do
l[#l + 1] = e
end
return "{" .. table.concat (l, ", ") .. "}"
end
function Set.print (s)
print (Set.tostring (s))
end
s1 = Set.new {10, 20, 30, 50}
s2 = Set.new {30, 1}
Set.print (s1)
Set.print (s2)
s3 = s1 + s2
Set.print (s3)
But with the latest lua for windows I am getting the following error
lua: C:\meta.lua:47: attempt to perform arithmetic on global 's1' (a table value)
stack traceback:
C:\meta.lua:47: in main chunk
[C]: ?
{30, 10, 20, 50}
{1, 30}
You are making this assignment too early:
mt.__add = Set.union
because Set.union is not initialized yet.
Move this below Set.union and it will work.
For the same reason, if you assign mt.__mul, this should be below Set.intersection
You need to define mt as a suitable metatable:
mt = { __add = Set.union, __mul = Set.intersection, __tostring = Set.tostring }