mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
[added] functional.generate and generate_2d
This commit is contained in:
parent
eb428faa6e
commit
8a62f3b19f
@ -141,6 +141,40 @@ function functional.zip(t1, t2, f)
|
||||
return ret
|
||||
end
|
||||
|
||||
-----------------------------------------------------------
|
||||
--generating data
|
||||
-----------------------------------------------------------
|
||||
|
||||
--generate data into a table
|
||||
--basically a map on numeric values from 1 to count
|
||||
--nil values are omitted in the result, as for map
|
||||
function functional.generate(count, f)
|
||||
local r = {}
|
||||
for i = 1, count do
|
||||
local v = f(i)
|
||||
if v ~= nil then
|
||||
table.insert(r, v)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
--2d version of the above
|
||||
--note: ends up with a 1d table;
|
||||
-- if you need a 2d table, nest 1d generate calls
|
||||
function functional.generate_2d(width, height, f)
|
||||
local r = {}
|
||||
for y = 1, height do
|
||||
for x = 1, width do
|
||||
local v = f(x, y)
|
||||
if v ~= nil then
|
||||
table.insert(r, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
-----------------------------------------------------------
|
||||
--common queries and reductions
|
||||
-----------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user