mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added lume.each() function
This commit is contained in:
parent
bfcc23d285
commit
1cedfd9464
10
README.md
10
README.md
@ -70,6 +70,15 @@ Iterates the supplied iterator and returns an array filled with the values.
|
|||||||
lume.array(pairs({a = 1, b = 2})) -- Returns {"a", "b"}
|
lume.array(pairs({a = 1, b = 2})) -- Returns {"a", "b"}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### lume.each(t, fn, ...)
|
||||||
|
Iterates the table `t` and calls the function `fn` on each value followed by
|
||||||
|
the supplied additional arguments; if `fn` is a string the method of that name
|
||||||
|
is called for each value. The function returns `t` unmodified.
|
||||||
|
```lua
|
||||||
|
lume.each({1, 2, 3}, print) -- Prints "1", "2", "3" on separate lines
|
||||||
|
lume.each({a, b, c}, "move", 10, 20) -- Does x:move(10, 20) on each value
|
||||||
|
```
|
||||||
|
|
||||||
### lume.map(t, fn)
|
### lume.map(t, fn)
|
||||||
Applies the function `fn` to each value in table `t` and returns a new table
|
Applies the function `fn` to each value in table `t` and returns a new table
|
||||||
with the resulting values.
|
with the resulting values.
|
||||||
@ -132,7 +141,6 @@ exist in the table.
|
|||||||
lume.find({"a", "b", "c"}, "b") -- Returns 2
|
lume.find({"a", "b", "c"}, "b") -- Returns 2
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### lume.slice(t [, i [, j]])
|
### lume.slice(t [, i [, j]])
|
||||||
Mimics the behaviour of Lua's `string.sub`, but operates on an array rather
|
Mimics the behaviour of Lua's `string.sub`, but operates on an array rather
|
||||||
than a string. Creates and returns a new array of the given slice.
|
than a string. Creates and returns a new array of the given slice.
|
||||||
|
11
lume.lua
11
lume.lua
@ -80,6 +80,16 @@ function lume.array(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.each(t, fn, ...)
|
||||||
|
if type(fn) == "string" then
|
||||||
|
for _, v in pairs(t) do v[fn](v, ...) end
|
||||||
|
else
|
||||||
|
for _, v in pairs(t) do fn(v, ...) end
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.map(t, fn)
|
function lume.map(t, fn)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[k] = fn(v) end
|
for k, v in pairs(t) do rtn[k] = fn(v) end
|
||||||
@ -145,7 +155,6 @@ function lume.find(t, value)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function lume.slice(t, i, j)
|
function lume.slice(t, i, j)
|
||||||
i = i or 1
|
i = i or 1
|
||||||
j = j and (j < 0 and (#t + j) or j) or (#t - i + 1)
|
j = j and (j < 0 and (#t + j) or j) or (#t - i + 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user