Skip to content

Commit

Permalink
new method implementations for Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Baumgold committed Jun 10, 2022
1 parent 1c6c2ad commit eee0d59
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ Get the top item from the stack. Sometimes called peek.
Base.first(s::Stack) = last(s.store)
Base.last(s::Stack) = first(s.store)

function Base.push!(s::Stack, x)
push!(s.store, x)
return s
end
Base.push!(s::Stack, x) = (push!(s.store, x); s)
Base.pushfirst!(s::Stack, x) = (pushfirst!(s.store, x); s)

Base.pop!(s::Stack) = pop!(s.store)
Base.popfirst!(s::Stack) = pop!first(s.store)

Base.empty!(s::Stack) = (empty!(s.store); s)

Base.collect(s::Stack) = collect(s.store)

Base.iterate(st::Stack, s...) = iterate(Iterators.reverse(st.store), s...)

Iterators.reverse(s::Stack{T}) where {T} = DequeIterator{T}(s.store)
Expand Down

0 comments on commit eee0d59

Please sign in to comment.