Skip to content

Commit fe184cf

Browse files
committed
✨ feat: arrutil - enhance the Ints and Strings and SortedList
1 parent 7747e31 commit fe184cf

4 files changed

Lines changed: 112 additions & 55 deletions

File tree

arrutil/list.go

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
)
99

1010
// Ints type
11-
type Ints []int
11+
type Ints[T comdef.Integer] []T
1212

1313
// String to string
14-
func (is Ints) String() string {
14+
func (is Ints[T]) String() string {
1515
return ToString(is)
1616
}
1717

1818
// Has given element
19-
func (is Ints) Has(i int) bool {
19+
func (is Ints[T]) Has(i T) bool {
2020
for _, iv := range is {
2121
if i == iv {
2222
return true
@@ -26,24 +26,47 @@ func (is Ints) Has(i int) bool {
2626
}
2727

2828
// First element value.
29-
func (is Ints) First() int {
29+
func (is Ints[T]) First(defVal ...T) T {
3030
if len(is) > 0 {
3131
return is[0]
3232
}
33-
panic("empty int slice")
33+
34+
if len(defVal) > 0 {
35+
return defVal[0]
36+
}
37+
panic("empty integer slice")
3438
}
3539

3640
// Last element value.
37-
func (is Ints) Last() int {
41+
func (is Ints[T]) Last(defVal ...T) T {
3842
if len(is) > 0 {
3943
return is[len(is)-1]
4044
}
41-
panic("empty int slice")
45+
46+
if len(defVal) > 0 {
47+
return defVal[0]
48+
}
49+
panic("empty integer slice")
4250
}
4351

4452
// Sort the int slice
45-
func (is Ints) Sort() {
46-
sort.Ints(is)
53+
func (is Ints[T]) Sort() {
54+
sort.Sort(is)
55+
}
56+
57+
// Len get length
58+
func (is Ints[T]) Len() int {
59+
return len(is)
60+
}
61+
62+
// Less compare two elements
63+
func (is Ints[T]) Less(i, j int) bool {
64+
return is[i] < is[j]
65+
}
66+
67+
// Swap elements by indexes
68+
func (is Ints[T]) Swap(i, j int) {
69+
is[i], is[j] = is[j], is[i]
4770
}
4871

4972
// Strings type
@@ -75,41 +98,69 @@ func (ss Strings) Contains(sub string) bool {
7598
}
7699

77100
// First element value.
78-
func (ss Strings) First() string {
101+
func (ss Strings) First(defVal ...string) string {
79102
if len(ss) > 0 {
80103
return ss[0]
81104
}
105+
106+
if len(defVal) > 0 {
107+
return defVal[0]
108+
}
82109
panic("empty string list")
83110
}
84111

85112
// Last element value.
86-
func (ss Strings) Last() string {
113+
func (ss Strings) Last(defVal ...string) string {
87114
if len(ss) > 0 {
88115
return ss[len(ss)-1]
89116
}
117+
118+
if len(defVal) > 0 {
119+
return defVal[0]
120+
}
90121
panic("empty string list")
91122
}
92123

93-
// ScalarList definition for any type
94-
type ScalarList[T comdef.ScalarType] []T
124+
// Sort the string slice
125+
func (ss Strings) Sort() {
126+
sort.Strings(ss)
127+
}
128+
129+
// SortedList definition for compared type
130+
type SortedList[T comdef.Compared] []T
131+
132+
// Len get length
133+
func (ls SortedList[T]) Len() int {
134+
return len(ls)
135+
}
136+
137+
// Less compare two elements
138+
func (ls SortedList[T]) Less(i, j int) bool {
139+
return ls[i] < ls[j]
140+
}
141+
142+
// Swap elements by indexes
143+
func (ls SortedList[T]) Swap(i, j int) {
144+
ls[i], ls[j] = ls[j], ls[i]
145+
}
95146

96147
// IsEmpty check
97-
func (ls ScalarList[T]) IsEmpty() bool {
148+
func (ls SortedList[T]) IsEmpty() bool {
98149
return len(ls) == 0
99150
}
100151

101152
// String to string
102-
func (ls ScalarList[T]) String() string {
153+
func (ls SortedList[T]) String() string {
103154
return ToString(ls)
104155
}
105156

106157
// Has given element
107-
func (ls ScalarList[T]) Has(el T) bool {
158+
func (ls SortedList[T]) Has(el T) bool {
108159
return ls.Contains(el)
109160
}
110161

111162
// Contains given element
112-
func (ls ScalarList[T]) Contains(el T) bool {
163+
func (ls SortedList[T]) Contains(el T) bool {
113164
for _, v := range ls {
114165
if v == el {
115166
return true
@@ -119,39 +170,47 @@ func (ls ScalarList[T]) Contains(el T) bool {
119170
}
120171

121172
// First element value.
122-
func (ls ScalarList[T]) First() T {
173+
func (ls SortedList[T]) First(defVal ...T) T {
123174
if len(ls) > 0 {
124175
return ls[0]
125176
}
177+
178+
if len(defVal) > 0 {
179+
return defVal[0]
180+
}
126181
panic("empty list")
127182
}
128183

129184
// Last element value.
130-
func (ls ScalarList[T]) Last() T {
185+
func (ls SortedList[T]) Last(defVal ...T) T {
131186
if ln := len(ls); ln > 0 {
132187
return ls[ln-1]
133188
}
189+
190+
if len(defVal) > 0 {
191+
return defVal[0]
192+
}
134193
panic("empty list")
135194
}
136195

137196
// Remove given element
138-
func (ls ScalarList[T]) Remove(el T) ScalarList[T] {
197+
func (ls SortedList[T]) Remove(el T) SortedList[T] {
139198
return Filter(ls, func(v T) bool {
140199
return v != el
141200
})
142201
}
143202

144203
// Filter the slice, default will filter zero value.
145-
func (ls ScalarList[T]) Filter(filter ...comdef.MatchFunc[T]) ScalarList[T] {
204+
func (ls SortedList[T]) Filter(filter ...comdef.MatchFunc[T]) SortedList[T] {
146205
return Filter(ls, filter...)
147206
}
148207

149208
// Map the slice to new slice. TODO syntax ERROR: Method cannot have type parameters
150-
// func (ls ScalarList[T]) Map[V any](mapFn MapFn[T, V]) ScalarList[V] {
209+
// func (ls SortedList[T]) Map[V any](mapFn MapFn[T, V]) SortedList[V] {
151210
// return Map(ls, mapFn)
152211
// }
153212

154213
// Sort the slice
155-
// func (ls ScalarList[T]) Sort() {
156-
// sort.Sort(ls)
157-
// }
214+
func (ls SortedList[T]) Sort() {
215+
sort.Sort(ls)
216+
}

arrutil/list_test.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,20 @@ import (
88
)
99

1010
func TestInts_methods(t *testing.T) {
11-
tests := []struct {
12-
is arrutil.Ints
13-
val int
14-
want bool
15-
want2 string
16-
}{
17-
{
18-
arrutil.Ints{12, 23},
19-
12,
20-
true,
21-
"[12,23]",
22-
},
23-
}
11+
ints := arrutil.Ints[int]{23, 10, 12}
12+
assert.False(t, ints.Has(999))
13+
assert.Eq(t, "[23,10,12]", ints.String())
2414

25-
for _, tt := range tests {
26-
assert.Eq(t, tt.want, tt.is.Has(tt.val))
27-
assert.False(t, tt.is.Has(999))
28-
assert.Eq(t, tt.want2, tt.is.String())
29-
}
30-
31-
ints := arrutil.Ints{23, 10, 12}
3215
ints.Sort()
3316
assert.Eq(t, "[10,12,23]", ints.String())
3417
assert.Eq(t, 10, ints.First())
3518
assert.Eq(t, 23, ints.Last())
3619

20+
ints = arrutil.Ints[int]{}
21+
assert.Eq(t, 1, ints.First(1))
22+
assert.Eq(t, 2, ints.Last(2))
23+
3724
t.Run("panic", func(t *testing.T) {
38-
ints = arrutil.Ints{}
3925
assert.Panics(t, func() {
4026
ints.First()
4127
})
@@ -71,8 +57,11 @@ func TestStrings_methods(t *testing.T) {
7157
assert.Eq(t, "a", ss.First())
7258
assert.Eq(t, "b", ss.Last())
7359

60+
ss = arrutil.Strings{}
61+
assert.Eq(t, "default1", ss.First("default1"))
62+
assert.Eq(t, "default2", ss.Last("default2"))
63+
7464
t.Run("panic", func(t *testing.T) {
75-
ss = arrutil.Strings{}
7665
assert.Panics(t, func() {
7766
ss.First()
7867
})
@@ -82,18 +71,27 @@ func TestStrings_methods(t *testing.T) {
8271
})
8372
}
8473

85-
func TestScalarList_methods(t *testing.T) {
86-
ls := arrutil.ScalarList[string]{"a", "", "b"}
74+
func TestSortedList_methods(t *testing.T) {
75+
ls := arrutil.SortedList[string]{"a", "", "b"}
8776
assert.Eq(t, "a", ls.First())
8877
assert.Eq(t, "b", ls.Last())
8978
assert.True(t, ls.Has("a"))
9079
assert.False(t, ls.Has("e"))
9180
assert.False(t, ls.IsEmpty())
9281
assert.Eq(t, "[a,b]", ls.Filter().String())
93-
assert.Eq(t, "[a,b]", ls.Remove("").String())
82+
83+
ls = ls.Remove("")
84+
assert.Eq(t, "[a,b]", ls.String())
85+
86+
ls1 := arrutil.SortedList[int]{4, 3}
87+
ls1.Sort()
88+
assert.Eq(t, "[3,4]", ls1.String())
89+
90+
ls = arrutil.SortedList[string]{}
91+
assert.Eq(t, "default1", ls.First("default1"))
92+
assert.Eq(t, "default2", ls.Last("default2"))
9493

9594
t.Run("panic", func(t *testing.T) {
96-
ls = arrutil.ScalarList[string]{}
9795
assert.Panics(t, func() {
9896
ls.First()
9997
})

comdef/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type SimpleType interface {
6161

6262
// ScalarType interface type.
6363
//
64-
// it can be ordered, that supports the operators < <= >= >.
64+
// TIP: has bool type, it cannot be ordered
6565
//
6666
// contains: (x)int, float, ~string, ~bool types
6767
type ScalarType interface {

fsutil/finder/matchers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ var timeNumReg = regexp.MustCompile(`(-?\d+)`)
239239
//
240240
// Usage:
241241
//
242-
// f := EmptyFinder()
243-
// f.AddFilter(HumanModTime(">10m")) // before 10 minutes
244-
// f.AddFilter(HumanModTime("<10m")) // latest 10 minutes, to Now
242+
// f := finder.NewFinder()
243+
// f.Include(HumanModTime(">10m")) // before 10 minutes
244+
// f.Include(HumanModTime("<10m")) // latest 10 minutes, to Now
245245
func HumanModTime(expr string) MatcherFunc {
246246
opt := &timex.ParseRangeOpt{AutoSort: true}
247247
// convert > to <, < to >

0 commit comments

Comments
 (0)