-
Notifications
You must be signed in to change notification settings - Fork 0
/
slices.go
136 lines (99 loc) · 3.3 KB
/
slices.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"strings"
"github.com/enetx/g"
"github.com/enetx/g/cmp"
"github.com/enetx/g/f"
)
func main() {
ws := g.SliceOf[g.String]("d", "b", "c", "a")
// ws.SortBy(g.String.Cmp)
// or
ws.SortBy(cmp.Cmp)
ws.Print()
wsl := g.SliceOf[g.String](
"aa a",
"b b",
"o o oo oooo",
"aa a a aa a",
"aaa aaaa",
"a a a aaaa",
"aaa aaa",
"three three",
"one",
"four",
)
// wsl.SortBy(func(a, b g.String) cmp.Ordering { return a.Cmp(b) }) // or
// wsl.SortBy(g.String.Cmp) // or
wsl.SortBy(cmp.Cmp)
wsl.Print() // Slice[a a a aaaa, aa a, aa a a aa a, aaa aaa, aaa aaaa, b b, four, o o oo oooo, one, three three]
wsl.SortBy(func(a, b g.String) cmp.Ordering { return a.Cmp(b).Reverse() })
wsl.Print() // Slice[three three, one, o o oo oooo, four, b b, aaa aaaa, aaa aaa, aa a a aa a, aa a, a a a aaaa]
wsl.SortBy(func(a, b g.String) cmp.Ordering {
return a.Fields().Collect().Len().Cmp(b.Fields().Collect().Len()).
Then(a.Len().Cmp(b.Len()))
})
wsl.Print() // Slice[one, four, b b, aa a, aaa aaa, aaa aaaa, three three, a a a aaaa, o o oo oooo, aa a a aa a]
slice := g.Slice[int]{1, 2, 3, 4}
slice.Iter().Filter(f.Gt(2)).Collect().Print() // Slice[3, 4]
slice.Iter().Filter(f.Eq(2)).Collect().Print() // Slice[2]
slice.Iter().Filter(f.Ne(2)).Collect().Print() // Slice[1, 3, 4]
fmt.Println(slice.Iter().All(func(i int) bool { return i != 5 }))
fmt.Println(slice.Iter().Any(func(i int) bool { return i == 5 }))
slice.Iter().Range(func(val int) bool {
fmt.Println(val)
return val != 3
})
result := g.Slice[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
result.Delete(1).Print() // Slice[1, 3, 4, 5, 6, 7, 8, 9, 10]
result.Delete(-9).Print() // Slice[1, 3, 4, 5, 6, 7, 8, 9, 10]
result.SubSlice(1, -3).Print() // Slice[2, 3, 4, 5, 6, 7]
result.SubSlice(-3, result.Len()).Print() // Slice[8, 9, 10]
result.SubSlice(-3, -1).Print() // Slice[8, 9]
result.SubSlice(-1, result.Len()).Print() // Slice[10]
result = result.RandomSample(5)
result.Clone().Append(999).Print()
result.Print()
fmt.Printf("%#v\n", result.Std())
filled := g.NewSlice[int](10)
filled.Fill(88)
filled.Print()
slice = g.Slice[int]{1, 2, 3, 4, 5}.Print()
slice.Cut(1, 3).Print() // Slice[1, 4, 5]
slice.Cut(-4, 3).Print() // Slice[1, 4, 5]
slice.Cut(-3, 4).Print() // Slice[1, 2, 5]
// InPlace Methods
sipl := g.NewSlice[int]()
sipl.AppendInPlace(1)
sipl.AppendInPlace(2)
sipl.AppendInPlace(3)
sipl.DeleteInPlace(1)
sipl.Fill(999999)
sipl.InsertInPlace(0, 22, 33, 44)
sipl.AddUniqueInPlace(22, 22, 22, 33, 44, 55)
sipl.Print()
slicea := g.Slice[string]{"a", "b", "c", "d"}
slicea.InsertInPlace(2, "e", "f")
slicea.Print()
slice = g.Slice[int]{1, 2, 0, 4, 0, 3, 0, 0, 0, 0}
slice = slice.Iter().Exclude(f.Zero).Collect()
slice.DeleteInPlace(0)
slice.Print()
sll := g.NewSlice[int](0, 100000)
sll = sll.Append(1).Clip()
fmt.Println(sll.Cap())
g.SliceMap([]string{"AAA", "BBB"}, g.NewString).Iter().Map(g.String.Lower).Collect().Print()
g.SliceOf([]string{"AAA", "BBB"}...).Iter().Map(strings.ToLower).Collect().Print()
g.SliceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11).Iter().
Filter(isPrime).
ForEach(func(n int) { fmt.Printf("%d is a prime number\n", n) })
}
func isPrime(n int) bool {
for i := 2; i < n/2; i++ {
if n%i == 0 {
return false
}
}
return true
}