Skip to content

Commit 83cbed6

Browse files
committed
Make chain() rewrite \> map \> reduce into mapreduce
This provides an example of how such pipelines can be fundamentally more efficient than normal piping syntax.
1 parent 3c6e6f9 commit 83cbed6

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/expr.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ fixbutlast(f, args...; kws...) = FixButLast(f, args, kws)
360360
chain(x, f, fs...) = chain(f(x), fs...)
361361
chain(x) = x
362362

363+
# An example of how chain() can be used to rewrite
364+
# `x \> map(f) \> reduce(g)` into `mapreduce(f, g, x)`
365+
function chain(x, f1::FixButLast{typeof(map)}, f2::FixButLast{typeof(reduce)}, fs...)
366+
chain(x, fixbutlast(mapreduce, f1.args..., f2.args...; f1.kwargs..., f2.kwargs...), fs...)
367+
end
368+
363369
struct ComposeChain{Funcs}
364370
fs::Funcs
365371
end

0 commit comments

Comments
 (0)