Skip to content

Commit 5842ccf

Browse files
authored
Fix Folds.dict(array) (#51)
1 parent 1796329 commit 5842ccf

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/collect.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ function copy_default(T::Type{<:AbstractSet}, itr, ex)
5656
return Folds.reduce(union!!, itr |> Map(SingletonVector), ex; init = Empty(T))
5757
end
5858

59+
struct Unpairs{Data}
60+
data::Data
61+
end
62+
63+
Base.pairs(pairs::Unpairs) = pairs.data
64+
5965
function copy_default(T::Type{<:AbstractDict}, itr, ex)
6066
xf, array = extract_transducer(itr)
6167
if xf isa Union{Map{identity},IdentityTransducer}
6268
if array isa PartitionableArray
69+
isempty(array) && return T(array)
6370
basesize =
6471
something(get_basesize(ex), max(1, length(array) ÷ Threads.nthreads()))
6572
return Folds.reduce(
6673
merge!!,
67-
Iterators.partition(array, basesize),
74+
Iterators.partition(array, basesize) |> Map(Unpairs),
6875
set_basesize(ex, 1);
6976
init = emptyshim(T),
7077
)

src/testing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ copy(Dict, (x => x^2 for x in Set(1:10))) # nodist,FIXME
8686
copy(Vector, 1:10)
8787
count(isodd(x) for x in 1:10)
8888
dict(x => x^2 for x in 1:10)
89+
dict([x => x^2 for x in 1:10]) # nodist,FIXME
8990
extrema((x - 5)^2 for x in 1:10)
9091
findall(isodd, 1:10) # nodist,FIXME
9192
findfirst(x -> x > 3, 1:10) # nodist,FIXME

test/test_reduce.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
module TestReduce
22

3-
# import Base as Folds # should work (in Julia 1.7 and later)
43
using Folds
54
using Test
65

76
@testset "findmin & findmax" begin
7+
# Folds = Base # should work (in Julia 1.7 and later)
88
@test Folds.findmin('a':'z') == ('a', 1)
99
@test Folds.findmax('a':'z') == ('z', 26)
1010
@test Folds.findmin(a -> 'z' - a, 'a':'z') == (0, 26)
1111
@test Folds.findmax(a -> 'z' - a, 'a':'z') == (25, 1)
1212
end
1313

1414
@testset "argmin & argmax" begin
15+
# Folds = Base # should work (in Julia 1.7 and later)
1516
@test Folds.argmin('a':'z') == 1
1617
@test Folds.argmax('a':'z') == 26
1718
@test Folds.argmin(a -> 'z' - a, 'a':'z') == 'z'
1819
@test Folds.argmax(a -> 'z' - a, 'a':'z') == 'a'
1920
end
2021

22+
@testset "dict" begin
23+
@test Folds.dict(x => x^2 for x in 1:10) ==
24+
Folds.dict([x => x^2 for x in 1:10]) ==
25+
Dict(x => x^2 for x in 1:10)
26+
end
27+
2128
end # module

0 commit comments

Comments
 (0)