mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-29 16:24:35 +00:00
[modified] unnecessary use of cached table length for many functions
This commit is contained in:
parent
715765003d
commit
a810bf4ce5
@ -26,8 +26,7 @@ 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)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local result = f(t[i], i)
|
local result = f(t[i], i)
|
||||||
if result ~= nil then
|
if result ~= nil then
|
||||||
return result
|
return result
|
||||||
@ -35,12 +34,11 @@ function functional.foreach(t, f)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--performs a left to right reduction of t using f, with o as the initial value
|
--performs a left to right reduction of t using f, with seed as the initial value
|
||||||
-- reduce({1, 2, 3}, 0, f) -> f(f(f(0, 1), 2), 3)
|
-- reduce({1, 2, 3}, 0, f) -> f(f(f(0, 1), 2), 3)
|
||||||
-- (but performed iteratively, so no stack smashing)
|
-- (but performed iteratively, so no stack smashing)
|
||||||
function functional.reduce(t, seed, f)
|
function functional.reduce(t, seed, f)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
seed = f(seed, t[i], i)
|
seed = f(seed, t[i], i)
|
||||||
end
|
end
|
||||||
return seed
|
return seed
|
||||||
@ -50,8 +48,7 @@ end
|
|||||||
-- (automatically drops any nils to keep a sequence, so can be used to simultaneously map and filter)
|
-- (automatically drops any nils to keep a sequence, so can be used to simultaneously map and filter)
|
||||||
function functional.map(t, f)
|
function functional.map(t, f)
|
||||||
local result = {}
|
local result = {}
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local v = f(t[i], i)
|
local v = f(t[i], i)
|
||||||
if v ~= nil then
|
if v ~= nil then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
@ -64,7 +61,7 @@ end
|
|||||||
-- (automatically drops any nils, which can be used to simultaneously map and filter)
|
-- (automatically drops any nils, which can be used to simultaneously map and filter)
|
||||||
function functional.map_inplace(t, f)
|
function functional.map_inplace(t, f)
|
||||||
local write_i = 0
|
local write_i = 0
|
||||||
local n = #t
|
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
local v = f(t[i], i)
|
local v = f(t[i], i)
|
||||||
if v ~= nil then
|
if v ~= nil then
|
||||||
@ -85,8 +82,7 @@ functional.remap = functional.map_inplace
|
|||||||
-- returns a table containing items where f(v, i) returns truthy
|
-- returns a table containing items where f(v, i) returns truthy
|
||||||
function functional.filter(t, f)
|
function functional.filter(t, f)
|
||||||
local result = {}
|
local result = {}
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if f(t[i], i) then
|
if f(t[i], i) then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
end
|
end
|
||||||
@ -97,7 +93,7 @@ end
|
|||||||
--filters a sequence in place, modifying it
|
--filters a sequence in place, modifying it
|
||||||
function functional.filter_inplace(t, f)
|
function functional.filter_inplace(t, f)
|
||||||
local write_i = 1
|
local write_i = 1
|
||||||
local n = #t
|
local n = #t --cache, so splitting the sequence doesn't stop iteration
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
local v = t[i]
|
local v = t[i]
|
||||||
if f(v, i) then
|
if f(v, i) then
|
||||||
@ -116,8 +112,7 @@ end
|
|||||||
-- nil results are included so that this is an exact complement of filter; consider using partition if you need both!
|
-- nil results are included so that this is an exact complement of filter; consider using partition if you need both!
|
||||||
function functional.remove_if(t, f)
|
function functional.remove_if(t, f)
|
||||||
local result = {}
|
local result = {}
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if not f(t[i], i) then
|
if not f(t[i], i) then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
end
|
end
|
||||||
@ -130,8 +125,7 @@ end
|
|||||||
function functional.partition(t, f)
|
function functional.partition(t, f)
|
||||||
local a = {}
|
local a = {}
|
||||||
local b = {}
|
local b = {}
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if f(t[i], i) then
|
if f(t[i], i) then
|
||||||
table.insert(a, v)
|
table.insert(a, v)
|
||||||
else
|
else
|
||||||
@ -146,8 +140,7 @@ end
|
|||||||
-- (or use numeric grouping and pre-seed) if you want to avoid pairs!
|
-- (or use numeric grouping and pre-seed) if you want to avoid pairs!
|
||||||
function functional.group_by(t, f)
|
function functional.group_by(t, f)
|
||||||
local result = {}
|
local result = {}
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local group = f(t[i], i)
|
local group = f(t[i], i)
|
||||||
if result[group] == nil then
|
if result[group] == nil then
|
||||||
result[group] = {}
|
result[group] = {}
|
||||||
@ -215,8 +208,7 @@ end
|
|||||||
|
|
||||||
--true if any element of the table matches f
|
--true if any element of the table matches f
|
||||||
function functional.any(t, f)
|
function functional.any(t, f)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if f(t[i], i) then
|
if f(t[i], i) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -226,8 +218,7 @@ end
|
|||||||
|
|
||||||
--true if no element of the table matches f
|
--true if no element of the table matches f
|
||||||
function functional.none(t, f)
|
function functional.none(t, f)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if f(t[i], i) then
|
if f(t[i], i) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -237,8 +228,7 @@ end
|
|||||||
|
|
||||||
--true if all elements of the table match f
|
--true if all elements of the table match f
|
||||||
function functional.all(t, f)
|
function functional.all(t, f)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if not f(t[i], i) then
|
if not f(t[i], i) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -249,8 +239,7 @@ end
|
|||||||
--counts the elements of t that match f
|
--counts the elements of t that match f
|
||||||
function functional.count(t, f)
|
function functional.count(t, f)
|
||||||
local c = 0
|
local c = 0
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if f(t[i], i) then
|
if f(t[i], i) then
|
||||||
c = c + 1
|
c = c + 1
|
||||||
end
|
end
|
||||||
@ -260,8 +249,7 @@ end
|
|||||||
|
|
||||||
--true if the table contains element e
|
--true if the table contains element e
|
||||||
function functional.contains(t, e)
|
function functional.contains(t, e)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
if t[i] == e then
|
if t[i] == e then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -272,8 +260,7 @@ end
|
|||||||
--return the numeric sum of all elements of t
|
--return the numeric sum of all elements of t
|
||||||
function functional.sum(t)
|
function functional.sum(t)
|
||||||
local c = 0
|
local c = 0
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
c = c + t[i]
|
c = c + t[i]
|
||||||
end
|
end
|
||||||
return c
|
return c
|
||||||
@ -323,8 +310,7 @@ end
|
|||||||
function functional.find_min(t, f)
|
function functional.find_min(t, f)
|
||||||
local current = nil
|
local current = nil
|
||||||
local current_min = math.huge
|
local current_min = math.huge
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local e = t[i]
|
local e = t[i]
|
||||||
local v = f(e, i)
|
local v = f(e, i)
|
||||||
if v and v < current_min then
|
if v and v < current_min then
|
||||||
@ -340,8 +326,7 @@ end
|
|||||||
function functional.find_max(t, f)
|
function functional.find_max(t, f)
|
||||||
local current = nil
|
local current = nil
|
||||||
local current_max = -math.huge
|
local current_max = -math.huge
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local e = t[i]
|
local e = t[i]
|
||||||
local v = f(e, i)
|
local v = f(e, i)
|
||||||
if v and v > current_max then
|
if v and v > current_max then
|
||||||
@ -365,8 +350,7 @@ end
|
|||||||
|
|
||||||
--return the first element of the table that results in a true filter
|
--return the first element of the table that results in a true filter
|
||||||
function functional.find_match(t, f)
|
function functional.find_match(t, f)
|
||||||
local n = #t
|
for i = 1, #t do
|
||||||
for i = 1, n do
|
|
||||||
local v = t[i]
|
local v = t[i]
|
||||||
if f(v) then
|
if f(v) then
|
||||||
return v
|
return v
|
||||||
|
Loading…
Reference in New Issue
Block a user