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

Add support for subtype matching in patterns. #47

Merged
merged 1 commit into from
Apr 16, 2021
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AxisSets"
uuid = "a1a1544e-ba16-4f6d-8861-e833517b754e"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.4"
version = "0.1.5"

[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
Expand Down
8 changes: 6 additions & 2 deletions src/patterns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ function Base.in(item::Tuple, pattern::Pattern)
item_val, item_st = item_iter
pat_val, pat_st = pat_iter

# Iterate as normal if the pattern value matches or it's :_
if item_val == pat_val || pat_val === :_
# Iterate as normal if the pattern value matches, is a subtype or it's :_
if (
(item_val isa Type && pat_val isa Type && item_val <: pat_val) ||
item_val == pat_val ||
pat_val === :_
)
pat_iter = iterate(pattern.segments, pat_st)
item_iter = iterate(item, item_st)
# Look ahead when we see a multi-value wildcard to see if the next value matches
Expand Down
49 changes: 49 additions & 0 deletions test/patterns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,54 @@
(t1, 1, "temperature", :time),
(t1, 1, "temperature", :id),
]

@testset "Subtype matching" begin
t1 = Float64
t2 = Int
items = [
(t1, 1, "prices", :time),
(t1, 1, "prices", :id),
(t1, 1, "prices", :lag),
(t1, 1, "load", :time),
(t1, 1, "load", :id),
(t1, 1, "temperature", :time),
(t1, 1, "temperature", :id),
(t1, 2, "prices", :time),
(t1, 2, "prices", :id),
(t2, 1, "prices", :time),
(t2, 1, "prices", :id),
(t2, 1, "prices", :lag),
(t2, 1, "load", :time),
(t2, 1, "load", :id),
(t2, 1, "temperature", :time),
(t2, 1, "temperature", :id),
(t2, 2, "prices", :time),
(t2, 2, "prices", :id),
]

pattern = Pattern(AbstractFloat, 1, :__)
@test filter(in(pattern), items) == [
(t1, 1, "prices", :time),
(t1, 1, "prices", :id),
(t1, 1, "prices", :lag),
(t1, 1, "load", :time),
(t1, 1, "load", :id),
(t1, 1, "temperature", :time),
(t1, 1, "temperature", :id),
]

pattern = Pattern(Integer, 1, :__)
@test filter(in(pattern), items) == [
(t2, 1, "prices", :time),
(t2, 1, "prices", :id),
(t2, 1, "prices", :lag),
(t2, 1, "load", :time),
(t2, 1, "load", :id),
(t2, 1, "temperature", :time),
(t2, 1, "temperature", :id),
]

@test filter(in(Pattern(Real, :__)), items) == items
end
end
end