-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza.go
54 lines (45 loc) · 1.22 KB
/
pizza.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package pizza
import "reflect"
type Slice[T any] []T
// Length returns the length of the slice
func (s Slice[T]) Length() int {
return len(s)
}
// Capacity returns the length of the slice
func (s Slice[T]) Capacity() int {
return cap(s)
}
// Equals checks whether two Slices are deeply equal or not.
func (a Slice[T]) Equals(b Slice[T]) bool {
return reflect.DeepEqual(a, b)
}
// Pop removes the last element from the slice and returns it.
// If the slice is empty, it returns nil and an error.
func (s *Slice[T]) Pop() (*T, error) {
if len(*s) > 0 {
popped := (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
return &popped, nil
}
return nil, ErrEmptySlice
}
// Push appends an element to the slice and returns the updated slice.
func (s *Slice[T]) Push(v T) *Slice[T] {
*s = append(*s, v)
return s
}
// ForEach loops through a Slice and calls it's callback func during each iteration.
func (s Slice[T]) ForEach(callback func(element T, index int)) {
for i, v := range s {
callback(v, i)
}
}
// Some loops through a Slice and returns a boolean based on whether a predicate is met.
func (s Slice[T]) Some(callback func(element T, index int) bool) bool {
for i, v := range s {
if callback(v, i) {
return true
}
}
return false
}