Skip to content

Commit

Permalink
fix namedtuple getindex with tuple below 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
chengchingwen committed May 19, 2022
1 parent 8d71424 commit b6df0e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ Base.:(|>)(p1::Pipelines, p2::Pipeline) = Pipelines(p1.pipes..., p2)
Base.:(|>)(p1::Pipeline, p2::Pipelines) = Pipelines(p1, p2.pipes...)
Base.:(|>)(p1::Pipelines, p2::Pipelines) = Pipelines(p1.pipes..., p2.pipes...)

function __getindex__ end

"""
PipeGet{name}()
Expand Down Expand Up @@ -85,12 +83,7 @@ julia> p(0.5)
const PipeGet{name} = Pipeline{name, typeof(__getindex__)}

PipeGet{name}() where name = PipeGet{name}(__getindex__)

@static if VERSION < v"1.7"
(p::PipeGet{name})(_, y) where name = name isa Symbol ? y[name] : NamedTuple{name}(y)
else
(p::PipeGet{name})(_, y) where name = y[name]
end
(p::PipeGet{name})(_, y) where name = __getindex__(y, name)


"""
Expand Down Expand Up @@ -138,7 +131,7 @@ julia> p(2, (a = 3, b = 5))
(a = 3, b = 5, x = 9)
julia> p = Pipeline{(:sinx, :cosx)}(sincos, 1)
Pipeline{(:sinx, :cosx)}(sincos(source))
Pipeline{(sinx, cosx)}(sincos(source))
julia> p(0.5)
(sinx = 0.479425538604203, cosx = 0.8775825618903728)
Expand Down
10 changes: 8 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ end

# misc

@static if VERSION < v"1.7"
@inline __getindex__(nt::NamedTuple, name) = name isa Symbol ? nt[name] : NamedTuple{name}(nt)
else
@inline __getindex__(nt::NamedTuple, name) = nt[name]
end

struct FixRest{F, A<:Tuple} <: Function
f::F
arg::A
Expand Down Expand Up @@ -100,9 +106,9 @@ _syms(::ApplySyms{S}) where S = S
function (f::ApplySyms)(nt::NamedTuple)
s = _syms(f)
if s isa Tuple
f.f(nt[s]...)
f.f(__getindex__(nt, s)...)
else
f.f(nt[s])
f.f(__getindex__(nt,s))
end
end

Expand Down

0 comments on commit b6df0e7

Please sign in to comment.