Skip to content

Commit cc897a0

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 612b0c5 commit cc897a0

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
@@ -381,6 +381,12 @@ fixbutlast(f, args...; kws...) = FixButLast(f, args, kws)
381381
chain(x, f, fs...) = chain(f(x), fs...)
382382
chain(x) = x
383383

384+
# An example of how chain() can be used to rewrite
385+
# `x \> map(f) \> reduce(g)` into `mapreduce(f, g, x)`
386+
function chain(x, f1::FixButLast{typeof(map)}, f2::FixButLast{typeof(reduce)}, fs...)
387+
chain(x, fixbutlast(mapreduce, f1.args..., f2.args...; f1.kwargs..., f2.kwargs...), fs...)
388+
end
389+
384390
struct ComposeChain{Funcs}
385391
fs::Funcs
386392
end

0 commit comments

Comments
 (0)