Skip to content
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
28 changes: 28 additions & 0 deletions src/DataAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ struct Cols{T<:Tuple}
Cols(args...) = new{typeof(args)}(args)
end

"""
BroadcastedSelector(selector)

Wrapper type around a `Between`, `All` or `Cols` indicating that
an operation should be applied to each column included by the wrapped selector.

# Examples
```jldoctest
julia> using DataAPI

julia> DataAPI.Between(:a, :e) .=> sin
DataAPI.BroadcastedSelector{DataAPI.Between{Symbol, Symbol}}(DataAPI.Between{Symbol, Symbol}(:a, :e)) => sin

julia> DataAPI.Cols(r"x") .=> [sum, prod]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
julia> DataAPI.Cols(r"x") .=> [sum, prod]
julia> DataAPI.Cols(r"x") .=> [sum prod]

using [sum, prod] is probably not what the user will want (also the output needs to be changed)

Copy link
Member

Choose a reason for hiding this comment

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

ah - I have just checked that this does not look nice in the output, so maybe leave it as is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Contrary to e.g. ["a", "b"] .=> [sin, prod], DataAPI.Cols(r"x") .=> [sum prod] does apply sum and prod to all matching columns, right?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the difference is that ["a", "b"] .=> [sin, prod] broadcasts the same dimension (and we get a vector) while ["a", "b"] .=> [sin prod] different dimensions (and we get a matrix).

DataAPI.Cols(r"x") will be treated as expanding to a vector.

2-element Vector{Pair{DataAPI.BroadcastedSelector{DataAPI.Cols{Tuple{Regex}}}, _A} where _A}:
DataAPI.BroadcastedSelector{DataAPI.Cols{Tuple{Regex}}}(DataAPI.Cols{Tuple{Regex}}((r"x",))) => sum
DataAPI.BroadcastedSelector{DataAPI.Cols{Tuple{Regex}}}(DataAPI.Cols{Tuple{Regex}}((r"x",))) => prod
```
"""
struct BroadcastedSelector{T}
sel::T
BroadcastedSelector(sel) = new{typeof(sel)}(sel)
end

Base.Broadcast.broadcastable(x::Between) = Ref(BroadcastedSelector(x))
Base.Broadcast.broadcastable(x::All) = Ref(BroadcastedSelector(x))
Base.Broadcast.broadcastable(x::Cols) = Ref(BroadcastedSelector(x))

"""
unwrap(x)

Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ end
b = DataAPI.Between(x, y)
@test b.first == (x isa Int ? x : Symbol(x))
@test b.last == (y isa Int ? y : Symbol(y))

@test (b .=> sum) ===
(DataAPI.BroadcastedSelector(DataAPI.Between(x, y)) => sum)
@test (b .=> [sum, float]) ==
(Ref(DataAPI.BroadcastedSelector(DataAPI.Between(x, y))) .=> [sum, float])
end

@test_throws MethodError DataAPI.Between(true, 1)
Expand All @@ -81,6 +86,15 @@ end
@test length(a.cols) == 1
@test a.cols[1] isa v
@test a.cols[1].cols == ()

@test (v() .=> sum) ===
(DataAPI.BroadcastedSelector(v()) => sum)
@test (v(:a) .=> sum) ===
(DataAPI.BroadcastedSelector(v(:a)) => sum)
@test (v((1,2,3), :b) .=> sum) ===
(DataAPI.BroadcastedSelector(v((1,2,3), :b)) => sum)
@test (v() .=> [sum, float]) ==
(Ref(DataAPI.BroadcastedSelector(v())) .=> [sum, float])
end

end
Expand Down