diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a0c5db3..bf92e1a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ OhMyThreads.jl Changelog ========================= +Version 0.5.2 +------------- +- ![Enhancement][badge-enhancement] For empty input (e.g. `Float64[]` or `11:10`) behavior is now aligned with the serial functions in `Base`. + Version 0.5.1 ------------- - ![Feature][badge-feature] Within a parallel `@tasks` block one can now mark a region with `@one_by_one`. This region will be run by one task at a time ("critical region"). diff --git a/src/implementation.jl b/src/implementation.jl index 217f67d7..88786cbe 100644 --- a/src/implementation.jl +++ b/src/implementation.jl @@ -60,13 +60,9 @@ function tmapreduce(f, op, Arrs...; kwargs...) mapreduce_kwargs = isgiven(init) ? (; init) : (;) _scheduler = _scheduler_from_userinput(scheduler; kwargs...) - if isempty(first(Arrs)) - isempty(mapreduce_kwargs) && reduce_empty_err() - return mapreduce_kwargs.init - end - # @show _scheduler - if _scheduler isa SerialScheduler + if _scheduler isa SerialScheduler || isempty(first(Arrs)) + # empty input collection → align with Base.mapreduce behavior mapreduce(f, op, Arrs...; mapreduce_kwargs...) else @noinline _tmapreduce(f, op, Arrs, outputtype, _scheduler, mapreduce_kwargs) @@ -78,10 +74,6 @@ end throw(ArgumentError("Providing an explicit scheduler as well as direct keyword arguments (e.g. $(kwargstr)) is currently not supported.")) end -@noinline function reduce_empty_err() - throw(ArgumentError("reducing over an empty collection is not allowed; consider supplying `init`")) -end - treducemap(op, f, A...; kwargs...) = tmapreduce(f, op, A...; kwargs...) # DynamicScheduler: AbstractArray/Generic @@ -357,7 +349,8 @@ function tmap(f, end Arrs = (A, _Arrs...) - if _scheduler isa SerialScheduler + if _scheduler isa SerialScheduler || isempty(A) + # empty input collection → align with Base.map behavior map(f, Arrs...; kwargs...) else check_all_have_same_indices(Arrs) diff --git a/test/runtests.jl b/test/runtests.jl index 035dbb65..fcac91b7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -364,14 +364,30 @@ end; end; @testset "empty collections" begin + @static if VERSION < v"1.11.0-" + err = MethodError + else + err = ArgumentError + end for empty_coll in (11:9, Float64[]) for f in (sin, x -> im * x, identity) for op in (+, *, min) - @test_throws ArgumentError tmapreduce(f, op, empty_coll) + # mapreduce for init in (0.0, 0, 0.0 * im, 0.0f0) @test tmapreduce(f, op, empty_coll; init) == init end + # foreach @test tforeach(f, empty_coll) |> isnothing + # reduce + if op != min + @test treduce(op, empty_coll) == reduce(op, empty_coll) + else + @test_throws err treduce(op, empty_coll) + end + # map + @test tmap(f, empty_coll) == map(f, empty_coll) + # collect + @test tcollect(empty_coll) == collect(empty_coll) end end end