Upgrade to new |>
of Transducers.jl 0.4.39
Transducers.jl 0.4.39 now interprets |>
differently. Consider the following examples:
using Transducers
collect(Filter(isodd) |> Map(inv), 1:5)
foldl(+, Filter(isodd) |> Map(inv), 1:5)
It is now recommended to write
1:5 |> Filter(isodd) |> Map(inv) |> collect
foldl(+, 1:5 |> Filter(isodd) |> Map(inv))
The last snippet can also be written as
1:5 |> Filter(isodd) |> Map(inv) |> sum
Note that |>
now is compatible with the standard function application definition of |>
(i.e., x |> f == f(x)
):
@assert 1:5 |> Filter(isodd) |> Map(inv) === Map(inv)(Filter(isodd)(1:5))
Julia < 1.3 requires collection |> xf
or eduction(xf, collection)
instead of xf(collection)
.
If there is no input collection; e.g.,
xf = Map(last) |> Filter(isodd)
foldl(right, GroupBy(x -> gcd(x, 6), xf, push!!), 1:10)
use the opposite composition opcompose
instead:
xf = opcompose(Map(last), Filter(isodd))
foldl(right, GroupBy(x -> gcd(x, 6), xf, push!!), 1:10)
In Julia ≥ 1.5, ⨟
can also be used as the infix version of the opposite composition
foldl(right, GroupBy(x -> gcd(x, 6), Map(last) ⨟ Filter(isodd), push!!), 1:10)
Note that collection |> xf
and xf(collection)
create an eduction(xf, collection)
which is iterable. However, it is not recommended to use it with non-specialized functions. For example, even though
ys = Int[]
for x in 1:5 |> Filter(isodd)
push!(ys, x)
end
works as expected, it is recommended to use
ys = Int[]
foreach(1:5 |> Filter(isodd)) do x
push!(ys, x)
end
when the performance is important.
Transducers.jl 0.4.39 also adds xf'(rf)
as the reducing function transformation. This makes composing reducing functions easier. For example, it can be used to compute even minimum and odd maximum in one go:
rf = TeeRF(Filter(iseven)'(min), Filter(isodd)'(max))
foldxt(rf, Map(identity), 1:10)
(2, 9)
More details can be found in the reference entries such as Transducer
, eduction
, adjoint
, reducingfunction
and ∘
. To understand how those interfaces work coherently, see the explanation section on "generalized" transducers.
This page was generated using Literate.jl.