Skip to content

Commit

Permalink
Merge pull request #41 from invenia/gm/simply_refactor
Browse files Browse the repository at this point in the history
Simple refactor
  • Loading branch information
Glenn Moynihan authored Mar 4, 2021
2 parents 7aef385 + 362ff3e commit 081a9af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ struct Power <: Transform
exponent::Real
end

function _apply(x::AbstractArray{T}, P::Power; kwargs...) where T <: Real
return x .^ P.exponent
end
_apply(x, P::Power; kwargs...) = x .^ P.exponent
18 changes: 9 additions & 9 deletions src/transformers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ function apply(table, t::Transform; cols=nothing, kwargs...)
# Extract a columns iterator that we should be able to use to mutate the data.
# NOTE: Mutation is not guaranteed for all table types, but it avoid copying the data
columntable = Tables.columns(table)

cnames = cols === nothing ? propertynames(columntable) : cols
return _apply(columntable, t, cnames; kwargs...)
end

if cnames isa Union{Tuple, AbstractArray}
return [
_apply(getproperty(columntable, cname), t; name=cname, kwargs...)
for cname in cnames
]
else # return unwrapped single column
return _apply(getproperty(columntable, cnames), t; name=cnames, kwargs...)
end
# 3-arg forms are simply to dispatch on whether cols is a Symbol or a collection
function _apply(table, t::Transform, col; kwargs...)
return _apply(getproperty(table, col), t; name=col, kwargs...)
end

function _apply(table, t::Transform, cols::Union{Tuple, AbstractArray}; kwargs...)
return [_apply(table, t, col; kwargs...) for col in cols]
end

"""
Expand Down
3 changes: 1 addition & 2 deletions test/periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@
end

@testset "Matrix" begin
x = collect(0.:5.)
M = reshape(x, (3, 2))
M = reshape(0.:5., (3, 2))
M_expected = reshape(expected, (3, 2))

@testset "dims = $d" for d in (Colon(), 1, 2)
Expand Down

0 comments on commit 081a9af

Please sign in to comment.