extend("test")
extend("array")
describe("array:zip", () => {
it("truncates to shorter length for unequal int arrays and returns int2d_array", () => {
z = array:zip([1,2,3], [10,20])
expect_equal(type(z), "int2d_array")
expect_equal(z, [[1,10],[2,20]])
})
it("preserves types for string arrays as pairs", () => {
z = array:zip(["a","b"], ["x","y","z"])
expect_equal(type(z), "mixed_array")
expect_equal(z, [["a","x"], ["b","y"]])
})
it("supports placeholder partial for first argument", () => {
p = array:zip(_, [9,8])
r = p([1,2,3])
expect_equal(type(r), "int2d_array")
expect_equal(r, [[1,9],[2,8]])
})
it("supports placeholder partial for second argument", () => {
p = array:zip([1,2], _)
r = p([7,8,9])
expect_equal(type(r), "int2d_array")
expect_equal(r, [[1,7],[2,8]])
})
it("supports fully partial usage", () => {
p = array:zip()
r = p([5,6], [1,2,3])
expect_equal(type(r), "int2d_array")
expect_equal(r, [[5,1],[6,2]])
})
})
describe("array:zip_with", () => {
it("truncates to shorter length for unequal int arrays and returns mixed_array", () => {
sum = (a,b) => a + b
r = array:zip_with(sum, [1,2,3], [10,20])
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 2)
expect_equal(r[0], 11)
expect_equal(r[1], 22)
})
it("preserves result element types with string join function", () => {
join = (a,b) => a + "-" + b
r = array:zip_with(join, ["a","b"], ["x","y","z"])
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 2)
expect_equal(r[0], "a-x")
expect_equal(r[1], "b-y")
})
it("supports placeholder partial for function position", () => {
p = array:zip_with(_, [1,2,3], [10,20,30])
r = p((a,b) => a - b)
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 3)
expect_equal(r[0], -9)
expect_equal(r[1], -18)
expect_equal(r[2], -27)
})
it("supports placeholder partial for first array position", () => {
sum = (a,b) => a + b
p = array:zip_with(sum, _, [10,20,30])
r = p([1,2])
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 2)
expect_equal(r[0], 11)
expect_equal(r[1], 22)
})
it("supports placeholder partial for second array position", () => {
sum = (a,b) => a + b
p = array:zip_with(sum, [1,2,3], _)
r = p([10,20])
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 2)
expect_equal(r[0], 11)
expect_equal(r[1], 22)
})
it("supports fully partial usage", () => {
p = array:zip_with()
r = p((a,b) => a * b, [2,3], [5,7,9])
expect_equal(type(r), "mixed_array")
expect_equal(array:length(r), 2)
expect_equal(r[0], 10)
expect_equal(r[1], 21)
})
})