Skip to content

Commit

Permalink
CLEANUP: Use meaningful element type in tabular sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Nov 3, 2021
1 parent 7a9ec9e commit 79b65d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/categorical_algebra/FinSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ end

""" Finite set whose elements are rows of a table.
The underlying table should be compliant with Tables.jl.
The underlying table should be compliant with Tables.jl. For the sake of
uniformity, the rows are provided as named tuples, which assumes that the table
is not "extremely wide". This should not be a major limitation in practice but
see the Tables.jl documentation for further discussion.
"""
@auto_hash_equals struct TabularSet{Table} <: FinSet{Table,Any}
@auto_hash_equals struct TabularSet{Table,Row} <: FinSet{Table,Row}
table::Table

function TabularSet(table::Table) where Table
schema = Tables.schema(table)
new{Table,NamedTuple{schema.names,Tuple{schema.types...}}}(table)
end
end

FinSet(nt::NamedTuple) = TabularSet(nt)

Base.iterate(set::TabularSet, args...) = iterate(Tables.rows(set.table), args...)
Base.iterate(set::TabularSet, args...) =
iterate(Tables.namedtupleiterator(set.table), args...)
Base.length(set::TabularSet) = Tables.rowcount(set.table)
Base.collect(set::TabularSet) = Tables.rowtable(set.table)

function Base.show(io::IO, set::TabularSet)
print(io, "TabularSet(")
Expand Down
3 changes: 2 additions & 1 deletion test/categorical_algebra/FinSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ set = FinSet(Set(1:2:5))

# Tables as sets.
set = FinSet((x=[1,3,5], y=["a","b","c"]))
@test eltype(set) == NamedTuple{(:x,:y),Tuple{Int,String}}
@test length(set) == 3
@test map(NamedTuple, set) == [(x=1, y="a"), (x=3, y="b"), (x=5, y="c")]
@test collect(set) == [(x=1, y="a"), (x=3, y="b"), (x=5, y="c")]
@test startswith(sshow(set), "TabularSet(")
@test startswith(sshow(MIME("text/plain"), set), "3-element TabularSet")
@test startswith(sshow(MIME("text/html"), set), "<div")
Expand Down

0 comments on commit 79b65d0

Please sign in to comment.