Files
lambda/pkg/lifo/lifo.go
2026-01-11 20:18:38 -05:00

36 lines
492 B
Go

package lifo
import "fmt"
type LIFO[T any] []T
func New[T any](items ...T) *LIFO[T] {
l := LIFO[T](items)
return &l
}
func (l *LIFO[T]) Push(item T) {
*l = append(*l, item)
}
func (l *LIFO[T]) Empty() bool {
return len(*l) == 0
}
func (l *LIFO[T]) MustPop() T {
var item T
*l, item = (*l)[:len(*l)-1], (*l)[len(*l)-1]
return item
}
func (l *LIFO[T]) Pop() (T, error) {
var item T
if l.Empty() {
return item, fmt.Errorf("stack is exhausted")
}
return l.MustPop(), nil
}