Added 'mustPop' function which panics if slice is empty

master
Aadhavan Srinivasan 3 months ago
parent 5ee427230c
commit d1205c781c

@ -10,8 +10,19 @@ func peek[T any](s []T) (T, error) {
return s[len(s)-1], nil
}
func pop[T any](sp *[]T) T {
func mustPop[T any](sp *[]T) T {
val, err := pop(sp)
if err != nil {
panic(err)
}
return val
}
func pop[T any](sp *[]T) (T, error) {
if len(*sp) < 1 {
return *new(T), errors.New("Stack empty")
}
to_return := (*sp)[len(*sp)-1]
*sp = (*sp)[:len(*sp)-1]
return to_return
return to_return, nil
}

Loading…
Cancel
Save