Skip to content

Commit

Permalink
Merge #18
Browse files Browse the repository at this point in the history
18: Support iterate, add show method r=charleskawczynski a=charleskawczynski

This PR adds support for `Base.iterate` and `Base.show`.

Not sure if this is the best `show` format, but it's better than nothing. Will go with this for now and improve later if needed.

Co-authored-by: Charles Kawczynski <[email protected]>
  • Loading branch information
bors[bot] and charleskawczynski authored Mar 17, 2021
2 parents b7b8290 + 2c509e3 commit 22dae1d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DispatchedTuples"
uuid = "508c55e1-51b4-41fd-a5ca-7eb0327d070d"
authors = ["Charles Kawczynski <[email protected]>"]
version = "0.1.4"
version = "0.1.5"

[compat]
julia = "1.5"
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ julia> struct Bar end;
julia> struct Baz end;

julia> dtup = DispatchedTuple((
Pair(Foo(), 1),
Pair(Foo(), 2),
Pair(Bar(), 3),
));
Pair(Foo(), 1),
Pair(Foo(), 2),
Pair(Bar(), 3),
))
DispatchedTuple{Tuple{Tuple{Foo,Int64},Tuple{Foo,Int64},Tuple{Bar,Int64}},DispatchedTuples.NoDefaults} with 3 entries:
Foo() => 1
Foo() => 2
Bar() => 3
default => ()


julia> dispatch(dtup, Foo())
(1, 2)
Expand Down
3 changes: 2 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ dtup = DispatchedTuple((
Pair(Foo(), 1),
Pair(Foo(), 2),
Pair(Bar(), 3),
));
))
dispatch(dtup, Foo()) # returns (1, 2)
dispatch(dtup, Bar()) # returns (3,)
dispatch(dtup, Baz()) # returns ()
nothing
```

For convenience, `DispatchedTuple` can alternatively take a `Tuple` of two-element `Tuple`s.
19 changes: 19 additions & 0 deletions src/DispatchedTuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ Base.map(f, dt::AbstractDispatchedTuple) = Base.map(f, dt.tup)
Base.keys(dt::AbstractDispatchedTuple) = map(x->x[1], dt)
Base.values(dt::AbstractDispatchedTuple) = map(x->x[2], dt)
Base.getindex(dt::AbstractDispatchedTuple, e) = dispatch(dt, e)
Base.iterate(dt::AbstractDispatchedTuple, state = 1) = Base.iterate(dt.tup, state)

# Note: this is not GPU-friendly
show_default(io::IO, dt::AbstractDispatchedTuple, any) = println(io, " default => $(dt.default)")

show_default(io::IO, dt::DispatchedTuple, ::NoDefaults) = println(io, " default => ()")
show_default(io::IO, dt::DispatchedSet, ::NoDefaults) = println(io, " default => error")

function Base.show(io::IO, dt::AbstractDispatchedTuple)
show(io, typeof(dt))
print(io, " with $(length(dt)) entries:")
println(io)
foreach(dt) do tup
print(io, " ")
show(io, Pair(tup...))
println(io)
end
show_default(io, dt, dt.default)
end

# Helper
first_eltype(::Type{Tuple{T, V}}) where {T,V} = T
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ struct FooBar end
@test dispatch(dt, FooBar()) == (dt.default,)
end

@testset "DispatchedTuples - base behavior - show" begin
dt = DispatchedTuple(Pair(Foo(), 1), Pair(Bar(), 2); default = 0)
sprint(show, dt)
dt = DispatchedTuple(Pair(Foo(), 1), Pair(Bar(), 2))
sprint(show, dt)
dt = DispatchedSet(Pair(Foo(), 1), Pair(Bar(), 2); default = 0)
sprint(show, dt)
dt = DispatchedSet(Pair(Foo(), 1), Pair(Bar(), 2))
sprint(show, dt)
end

@testset "DispatchedTuples - base behavior - Pair interface" begin
dt = DispatchedTuple((Pair(Foo(), 1), Pair(Bar(), 2)))
@test dispatch(dt, Foo()) == (1,)
Expand Down Expand Up @@ -58,6 +69,8 @@ end
@test length(dt) == length(tup1)
@test isempty(dt) == isempty(tup1)
@test map(x->x, dt) == map(x->x, dt.tup)
@test map(x->x, dt.tup) == Tuple([x for x in dt])
@test iterate(dt.tup) == iterate(dt)
@test values(dt) == map(x->x[2], dt.tup)
@test keys(dt) == map(x->x[1], dt.tup)
@test dt[Foo()] == (1, )
Expand All @@ -71,6 +84,8 @@ end
@test length(dt) == length(tup1)
@test isempty(dt) == isempty(tup1)
@test map(x->x, dt) == map(x->x, dt.tup)
@test map(x->x, dt.tup) == Tuple([x for x in dt])
@test iterate(dt.tup) == iterate(dt)
@test values(dt) == map(x->x[2], dt.tup)
@test keys(dt) == map(x->x[1], dt.tup)
@test dt[Foo()] == 1
Expand Down

2 comments on commit 22dae1d

@charleskawczynski
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/32215

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 22dae1da3a5dae2a36d684e9c269e0b7e2a363ec
git push origin v0.1.5

Please sign in to comment.