function source(c) for k = 1:100000 put!(c, (1+k)*k÷2) end end function make_pairs(c, src) buffer = false for chunk in src if buffer == false buffer = chunk else put!(c, (buffer, chunk)) buffer = false end end end function split(c, src) for chunk in src put!(c, chunk) put!(c, chunk) end end function multiply(c, src) for (x, y) in src put!(c, x*y) end end function toy_gcd(c, src) function helper(x, y) if x < y helper(y, x) elseif y == 0 put!(c, x) else helper(y, x%y) end end for (x, y) in src helper(x, y) end end function combine(c, src1, src2) for x in src1 put!(c, (x, take!(src2))) end end function divide(c, src) for (x, y) in src put!(c, x÷y) end end function get_digits(c, src) function helper(x) if x != 0 helper(x÷10) put!(c, x%10) end end for x in src helper(x) end end function sink(src) for chunk in src end end function pipe(func, src...) Channel(c -> func(c, src...)) end function pipeline() s = pipe(split, pipe(make_pairs, Channel(source))) sink(pipe(get_digits, pipe(divide, pipe(combine, pipe(multiply, s), pipe(toy_gcd, s))))) end function repeat() for i = 1:100 pipeline() end end println(@elapsed repeat())