Skip to content

Commit

Permalink
Merge pull request #4 from AlgebraicJulia/path-fixup
Browse files Browse the repository at this point in the history
Path fixup
  • Loading branch information
olynch authored Aug 22, 2024
2 parents 6094397 + 752bb63 commit 04a17cd
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,37 @@ Base.isempty(p::Path) = isempty(p.segments)

head(p::Path) = p.segments[1]

tail(p::Path) = Subpath(p, 2)
tail(p::Path) =
length(p) > 0 ? Subpath(p, 2) : error("tried to take the tail of an empty path")

function segments(p::Path)
p.segments
end

struct Subpath <: AbstractPath
path::Path
starting_from::Int
end

function Base.length(p::Subpath)
length(p.path) - p.starting_from + 1
end

@tests Tuple{Subpath, length} begin
@test length(tail(Path([:a, :b, :c]))) == 2
end

Base.isempty(p::Subpath) = p.starting_from > length(p.path)

head(p::Subpath) = p.path.segments[p.starting_from]

tail(p::Subpath) = Subpath(p.path, p.starting_from + 1)
tail(p::Subpath) =
length(p) > 0 ? Subpath(p.path, p.starting_from + 1) : error("tried to take the tail of an empty path")

function segments(p::Subpath)
p.path.segments[p.starting_from:end]
end

function Base.:(*)(p1::AbstractPath, p2::AbstractPath)
Path([segments(p1); segments(p2)])
end

0 comments on commit 04a17cd

Please sign in to comment.