Return error if stack is empty

master
Aadhavan Srinivasan 2 months ago
parent 273a03e3cf
commit d0e812a730

@ -1,8 +1,13 @@
package main
import "errors"
// Helper functions for slices, to make them behave more like stacks
func peek[T any](s []T) T {
return s[len(s)-1]
func peek[T any](s []T) (T, error) {
if len(s) < 1 {
return *new(T), errors.New("Stack empty")
}
return s[len(s)-1], nil
}
func pop[T any](sp *[]T) T {

Loading…
Cancel
Save