extend("test")
extend("flow")
extend("array")
extend("string")
describe("flow:trans", () => {
it("applies a function to each item of an ink_iterator and returns an ink_iterator", () => {
transform = flow:trans(n => n * 10, ink(1,5))
expect_equal(type(transform), "ink_transform")
})
it("applies a function to each item of an ink_iteration and converts it to an array", () => {
transform = flow:trans(n => n * 10, ink(1,5))
expect_equal(type(transform), "ink_transform")
result = flow:to_array(transform)
expect_equal(type(result), "int_array")
})
/*
it("handles partial usage => supply the transform function now, the InkIterator later", () => {
partial = flow:trans(x => x + 100)
# partial is waiting for the second argument => pass ink(...) next:
final = partial( ink(3,6) )
# ink(3,6) yields [3,4,5].
# So adding 100 => [103,104,105].
out = array:map(x => x, final)
expect_equal(out, [103,104,105])
})
it("supports placeholder => known InkIterator, missing transform function", () => {
partial2 = flow:trans(_, ink(1,4))
# Now partial2 is waiting for a function:
final = partial2(x => x * x)
# That squares each item from [1,2,3], yielding [1,4,9].
out2 = array:map(x => x, final)
expect_equal(out2, [1,4,9])
})
it("should error if second param is not an InkIterator", () => {
test:expect_error(() => {
flow:trans(x => x*2, 123)
})
})
})
describe("flow:compose", () => {
it("chains multiple transforms from left to right => final usage", () => {
# We'll do a pipeline => first 'x => x+1', then 'x => x*3'.
chain = flow:compose(
flow:trans(x => x + 1),
flow:trans(x => x * 3)
)
# chain => partial, waiting for the final data => pass an InkIterator:
finalChain = chain( ink(1,4) )
# ink(1,4) => yields [1,2,3].
# step1 => x+1 => [2,3,4]
# step2 => x*3 => [6,9,12]
out = array:map(x => x, finalChain)
expect_equal(out, [6,9,12])
})
it("handles multiple transforms + placeholders", () => {
# We define a partial chain with underscores, for later additions:
partialChain = flow:compose(_, flow:trans(x => x * 2), _)
# partialChain is waiting for the missing transforms => fill them:
nextChain = partialChain(flow:trans(x => x + 10), flow:trans(x => x - 1))
# So the final chain is [ (x => x+10), (x => x*2), (x => x-1 ) ] in left->right
# Provide ink(5,8) => yields [5,6,7].
# step1 => x => x-1 => [4,5,6]
# step2 => x => x*2 => [8,10,12]
# step3 => x => x+10 => [18,20,22]
final = nextChain( ink(5,8) )
out2 = array:map(x => x, final)
expect_equal(out2, [18,20,22])
})
it("works even if we pass no transforms => returns partial => final => does nothing", () => {
emptyChain = flow:compose()
finalEmpty = emptyChain( ink(2,4) )
# ink(2,4) => [2,3], with no transforms => the data is unchanged.
out3 = array:map(x => x, finalEmpty)
expect_equal(out3, [2,3])
})
*/
})