mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-24 22:54:35 +00:00
renamed functional.zip to functional.combine; functional.zip and new functional.unzip are now "more traditional" simple sequence operations
This commit is contained in:
parent
ae98047d97
commit
3d03d3238a
@ -209,11 +209,11 @@ function functional.group_by(t, f)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
--zips two sequences together into a new table, based on another function
|
--combines two same-length sequences through a function f
|
||||||
--iteration limited by min(#t1, #t2)
|
-- f receives arguments (t1[i], t2[i], i)
|
||||||
--function receives arguments (t1, t2, i)
|
-- iteration limited by min(#t1, #t2)
|
||||||
--nil results ignored
|
-- ignores nil results
|
||||||
function functional.zip(t1, t2, f)
|
function functional.combine(t1, t2, f)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
local limit = math.min(#t1, #t2)
|
local limit = math.min(#t1, #t2)
|
||||||
for i = 1, limit do
|
for i = 1, limit do
|
||||||
@ -227,6 +227,31 @@ function functional.zip(t1, t2, f)
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--zips two sequences together into a new table, alternating from t1 and t2
|
||||||
|
-- zip({1, 2}, {3, 4}) -> {1, 3, 2, 4}
|
||||||
|
-- iteration limited by min(#t1, #t2)
|
||||||
|
function functional.zip(t1, t2)
|
||||||
|
local ret = {}
|
||||||
|
local limit = math.min(#t1, #t2)
|
||||||
|
for i = 1, limit do
|
||||||
|
table.insert(ret, t1[i])
|
||||||
|
table.insert(ret, t2[i])
|
||||||
|
end
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
--unzips a table into two new tables, alternating elements into each result
|
||||||
|
-- {1, 2, 3, 4} -> {1, 3}, {2, 4}
|
||||||
|
-- gets an extra result in the first result for odd-length tables
|
||||||
|
function functional.unzip(t)
|
||||||
|
local a = {}
|
||||||
|
local b = {}
|
||||||
|
for i, v in ipairs(t) do
|
||||||
|
table.insert(i % 2 == 1 and a or b, v)
|
||||||
|
end
|
||||||
|
return a, b
|
||||||
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------
|
-----------------------------------------------------------
|
||||||
--specialised maps
|
--specialised maps
|
||||||
-- (experimental: let me know if you have better names for these!)
|
-- (experimental: let me know if you have better names for these!)
|
||||||
|
@ -73,6 +73,7 @@ for _, v in ipairs({
|
|||||||
"filter",
|
"filter",
|
||||||
"remove_if",
|
"remove_if",
|
||||||
"zip",
|
"zip",
|
||||||
|
"combine",
|
||||||
"stitch",
|
"stitch",
|
||||||
"map_stitch",
|
"map_stitch",
|
||||||
"cycle",
|
"cycle",
|
||||||
@ -127,4 +128,9 @@ function sequence:partition(f)
|
|||||||
return sequence(a), sequence(b)
|
return sequence(a), sequence(b)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function sequence:unzip(f)
|
||||||
|
local a, b = functional.unzip(self, f)
|
||||||
|
return sequence(a), sequence(b)
|
||||||
|
end
|
||||||
|
|
||||||
return sequence
|
return sequence
|
||||||
|
Loading…
Reference in New Issue
Block a user