Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support foldxl etc. #22

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/LazyGroupBy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ if !@isdefined only
using Compat: only
end

const FoldFunction0 = Union{typeof(foldl),typeof(reduce),typeof(dreduce)}
if @isdefined foldxl
const FoldFunction1 = Union{typeof(foldxl),typeof(foldxt),typeof(foldxd)}
else
const FoldFunction1 = Union{}
const foldxl = foldl
const foldxt = reduce
const foldxd = dreduce
end
const FoldFunction = Union{FoldFunction0,FoldFunction1}

# Used only for `KWBroadcastedInner` below:
function air end
struct Aired{T}
Expand Down Expand Up @@ -98,21 +109,13 @@ function _impl_fold(fold, kwargs, rf, xf, group)
return d
end

impl(::typeof(foldl), kwargs, rf, xf::Transducer, group::GroupedBy) =
_impl_fold(foldl, kwargs, rf, xf, group)
impl(::typeof(reduce), kwargs, rf, xf::Transducer, group::GroupedBy) =
_impl_fold(reduce, kwargs, rf, xf, group)
impl(::typeof(dreduce), kwargs, rf, xf::Transducer, group::GroupedBy) =
_impl_fold(dreduce, kwargs, rf, xf, group)
impl(fold::FoldFunction, kwargs, rf, xf::Transducer, group::GroupedBy) =
_impl_fold(fold, kwargs, rf, xf, group)
impl(fold::FoldFunction, kwargs, rf, xs) = impl(fold, kwargs, rf, Map(identity), xs)

impl(::typeof(foldl), kwargs, rf, xs) = impl(foldl, kwargs, rf, Map(identity), xs)
impl(::typeof(mapfoldl), kwargs, f, rf, xs) = impl(foldl, kwargs, rf, Map(f), xs)

impl(::typeof(reduce), kwargs, rf, xs) = impl(reduce, kwargs, rf, Map(identity), xs)
impl(::typeof(mapreduce), kwargs, f, rf, xs) = impl(reduce, kwargs, rf, Map(f), xs)

impl(::typeof(dreduce), kwargs, rf, xs) = impl(dreduce, kwargs, rf, Map(identity), xs)

impl(::typeof(collect), kwargs, xf::Transducer, group::GroupedBy) = foldl(
right,
GroupBy(group.key, opcompose(Map(last), xf, Map(SingletonVector)), append!!),
Expand All @@ -121,15 +124,15 @@ impl(::typeof(collect), kwargs, xf::Transducer, group::GroupedBy) = foldl(
impl(::typeof(collect), kwargs, group::GroupedBy) =
impl(collect, kwargs, IdentityTransducer(), group)

impl(::typeof(tcollect), kwargs, xf::Transducer, group::GroupedBy) = reduce(
impl(::typeof(tcollect), kwargs, xf::Transducer, group::GroupedBy) = foldxt(
right,
GroupBy(group.key, opcompose(Map(last), xf, Map(SingletonVector)), append!!),
group.collection,
)
impl(::typeof(tcollect), kwargs, group::GroupedBy) =
impl(tcollect, kwargs, IdentityTransducer(), group)

impl(::typeof(dcollect), kwargs, xf::Transducer, group::GroupedBy) = dreduce(
impl(::typeof(dcollect), kwargs, xf::Transducer, group::GroupedBy) = foldxd(
right,
GroupBy(group.key, opcompose(Map(last), xf, Map(SingletonVector)), append!!),
group.collection;
Expand Down
17 changes: 13 additions & 4 deletions test/test_foldl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ module TestFoldl

using Test
using LazyGroupBy
using LazyGroupBy: foldxl

@testset "tuple" begin
@test foldl.(tuple, grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5])) ==
Dict(false => ((0, 4), 0), true => ((((((7, 3), 1), 5), 9), 3), 5))
@test foldl.(tuple, grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5]); init = -1) ==
Dict(false => (((-1, 0), 4), 0), true => (((((((-1, 7), 3), 1), 5), 9), 3), 5))
@testset for fold in [foldl, foldxl]
foldl = nothing

@test fold.(tuple, grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5])) ==
Dict(false => ((0, 4), 0), true => ((((((7, 3), 1), 5), 9), 3), 5))

@test fold.(tuple, grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5]); init = -1) ==
Dict(
false => (((-1, 0), 4), 0),
true => (((((((-1, 7), 3), 1), 5), 9), 3), 5),
)
end
end

extrema_rf((min1, max1), (min2, max2)) = (min(min1, min2), max(max1, max2))
Expand Down
12 changes: 12 additions & 0 deletions test/test_reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module TestReduce

using Test
using LazyGroupBy
using LazyGroupBy: foldxl, foldxt
using Transducers: Map

extrema_rf((min1, max1), (min2, max2)) = (min(min1, min2), max(max1, max2))

Expand All @@ -11,6 +13,16 @@ extrema_rf((min1, max1), (min2, max2)) = (min(min1, min2), max(max1, max2))
extrema_rf,
grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5]),
) == Dict(false => (0, 4), true => (1, 9))

@testset for fold in [foldl, foldxl, foldxt, reduce]
foldl = nothing

@test fold.(
extrema_rf,
Map(x -> (x, x)),
grouped(isodd, [0, 7, 3, 1, 5, 9, 4, 3, 0, 5]),
) == Dict(false => (0, 4), true => (1, 9))
end
end

end # module