mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-26 15:44:35 +00:00
[added] "canonical" identity function
This commit is contained in:
parent
eba27da544
commit
a85eec80e2
@ -18,6 +18,11 @@ local functional = setmetatable({}, {
|
|||||||
__index = tablex,
|
__index = tablex,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
--the identity function
|
||||||
|
function functional.identity(v)
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
|
||||||
--simple sequential iteration, f is called for all elements of t
|
--simple sequential iteration, f is called for all elements of t
|
||||||
--f can return non-nil to break the loop (and return the value)
|
--f can return non-nil to break the loop (and return the value)
|
||||||
function functional.foreach(t, f)
|
function functional.foreach(t, f)
|
||||||
@ -161,7 +166,7 @@ end
|
|||||||
|
|
||||||
--2d version of the above
|
--2d version of the above
|
||||||
--note: ends up with a 1d table;
|
--note: ends up with a 1d table;
|
||||||
-- if you need a 2d table, nest 1d generate calls
|
-- if you need a 2d table, you should nest 1d generate calls
|
||||||
function functional.generate_2d(width, height, f)
|
function functional.generate_2d(width, height, f)
|
||||||
local r = {}
|
local r = {}
|
||||||
for y = 1, height do
|
for y = 1, height do
|
||||||
|
Loading…
Reference in New Issue
Block a user