Skip to content

Commit

Permalink
Add StepRange support
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Sep 26, 2024
1 parent 3fe838c commit fbfb306
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ end
# -----------------------------------------

index_to_dict(i::Integer) = Dict(:type => "integer", :value => i)
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :first => first(r), :last => last(r))
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :start => r.start, :stop => r.stop)
index_to_dict(r::StepRange) = Dict(:type => "steprange", :start => r.start, :stop => r.stop, :step => r.step)
index_to_dict(::Colon) = Dict(:type => "colon")
index_to_dict(s::ConcretizedSlice{T,Base.OneTo{I}}) where {T,I} = Dict(:type => "concretized_slice", :oneto => s.range.stop)
index_to_dict(::ConcretizedSlice{T,R}) where {T,R} = error("ConcretizedSlice with range type $(R) not supported")
Expand All @@ -880,7 +881,9 @@ function dict_to_index(dict)
if dict[:type] == "integer"
return dict[:value]
elseif dict[:type] == "unitrange"
return dict[:first]:dict[:last]
return dict[:start]:dict[:stop]
elseif dict[:type] == "steprange"
return dict[:start]:dict[:step]:dict[:stop]
elseif dict[:type] == "colon"
return Colon()
elseif dict[:type] == "concretized_slice"
Expand All @@ -892,7 +895,6 @@ function dict_to_index(dict)
end
end


optic_to_dict(::typeof(identity)) = Dict(:type => "identity")
optic_to_dict(::PropertyLens{sym}) where {sym} = Dict(:type => "property", :field => String(sym))
optic_to_dict(i::IndexLens) = Dict(:type => "index", :indices => index_to_dict(i.indices))
Expand Down
1 change: 1 addition & 0 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ end
@varname(x[1]),
@varname(var"x[1]"),
@varname(x[1:10]),
@varname(x[1:3:10]),
@varname(x[1, 2]),
@varname(x[1, 2:5]),
@varname(x[:]),
Expand Down

0 comments on commit fbfb306

Please sign in to comment.