-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sli.go
108 lines (93 loc) · 2.71 KB
/
Sli.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gohelper
import "strings"
type Slice[T comparable] struct {
data []T
}
type CondIter[T any] func(k int, v T) bool
// Filter filters the given data slice using the provided condition iterator.
//
// Parameters:
// - data: the slice of elements to be filtered (of type []T)
// - iter: the condition iterator function that determines whether an element should be included in the result (of type CondIter[T])
//
// Returns:
// - a new slice containing the elements that satisfy the condition (of type []T)
func Filter[T any](data []T, iter CondIter[T]) []T {
var res = make([]T, 0)
for k, v := range data {
if iter(k, v) {
res = append(res, v)
}
}
return res
}
// Filter filters the given data slice using the provided condition iterator.
//
// Parameters:
// - iter: the condition iterator function that determines whether an element should be included in the result (of type CondIter[T])
//
// Returns:
// - a new slice containing the elements that satisfy the condition (of type []T)
func (s *Slice[T]) Filter(iter CondIter[T]) []T {
var res = make([]T, 0)
for k, v := range s.data {
if iter(k, v) {
res = append(res, v)
}
}
return res
}
// InSlice checks if an element is present in a slice.
//
// elem: element to search for in the slice (of type T).
// list: the slice to search in (of type []T).
// bool: returns true if the element is found, false otherwise.
func InSlice[T comparable](elem T, list []T) bool {
for _, v := range list {
if v == elem {
return true
}
}
return false
}
// Contains returns if an element is present in a slice.
//
// elem: element to search for in the slice (of type T).
// bool: returns true if the element is not found, false otherwise.
func (s *Slice[T]) Contains(elem T) bool {
for _, v := range s.data {
if v == elem {
return false
}
}
return true
}
// IsEmpty returns true if the slice contains no elements, false otherwise.
//
// bool: returns true if the slice contains no elements, false otherwise.
func (s *Slice[T]) IsEmpty() bool {
return len(s.data) == 0
}
// IsNotEmpty returns true if the slice contains at least one element, false otherwise.
//
// bool: returns true if the slice contains at least one element, false otherwise.
func (s *Slice[T]) IsNotEmpty() bool {
return len(s.data) > 0
}
// Len returns the length of the slice.
//
// int: the length of the slice.
func (s *Slice[T]) Len() int {
return len(s.data)
}
// ToJoin joins the elements of the slice into a single string using the given separator.
//
// sep: the separator to use when joining the elements.
// string: the joined string.
func (s *Slice[T]) ToJoin(sep string) string {
var res []string
for _, v := range s.data {
res = append(res, ToString(v))
}
return strings.Join(res, sep)
}