-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceutils.go
More file actions
123 lines (98 loc) · 2.99 KB
/
sliceutils.go
File metadata and controls
123 lines (98 loc) · 2.99 KB
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
package sliceutils
import (
"fmt"
"sort"
)
type Array[T comparable] []T
// Returns the number of elements in Array
func (arr Array[T]) Length() int {
return len(arr)
}
// Push appends elements to the end of the array.
func (arr *Array[T]) Push(elements ...T) {
*arr = append(*arr, elements...)
}
// Pop removes the last element from an array and returns that element.
// This method changes the length of the array.
func (arr *Array[T]) Pop() T {
lastIdx := len(*arr) - 1
lastEl := (*arr)[lastIdx]
*arr = (*arr)[:lastIdx]
return lastEl
}
// Executes a provided function once for each array element.
func (arr Array[T]) ForEach(iterator func(element T, index int, arr Array[T])) {
for idx, el := range arr {
iterator(el, idx, arr)
}
}
// Creates a new array populated with the results of
// calling a provided function on every element in the calling array.
func (arr Array[T]) Map(iterator func(element T, index int, arr Array[T]) T) Array[T] {
newArr := make([]T, arr.Length())
for idx, el := range arr {
newArr[idx] = iterator(el, idx, arr)
}
return newArr
}
// Filter creates a new array with all elements that pass the test implemented by the provided function.
func (arr Array[T]) Filter(iterator func(element T, index int, arr Array[T]) bool) Array[T] {
var newArr []T
for idx, el := range arr {
if iterator(el, idx, arr) {
newArr = append(newArr, el)
}
}
return newArr
}
// IndexOf returns the first index at which a given element can be found in the array, or -1 if it is not present.
func (arr Array[T]) IndexOf(element T) int {
for idx, el := range arr {
if el == element {
return idx
}
}
return -1
}
// Includes determines whether an array includes a certain element, returning true or false as appropriate.
func (arr Array[T]) Includes(element T) bool {
return arr.IndexOf(element) != -1
}
// Sort
func (arr Array[T]) Sort(comparator func(a, b T) bool) {
sort.Slice(arr, func(i, j int) bool {
return comparator(arr[i], arr[j])
})
}
// Concat: Merges two arrays
func (arr Array[T]) Concat(other Array[T]) Array[T] {
return append(arr, other...)
}
// Every: Tests whether all elements in the array pass the test implemented by the provided function.
func (arr Array[T]) Every(iterator func(element T, index int, arr Array[T]) bool) bool {
for idx, el := range arr {
if !iterator(el, idx, arr) {
return false
}
}
return true
}
// Join: Joins all elements of an array into a string.
func (arr Array[T]) Join(separator string) string {
var out string
for i := 0; i < len(arr); i++ {
out += fmt.Sprintf("%v", arr[i])
if i < len(arr)-1 {
out += separator
}
}
return out
}
// Reduce: Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
func (arr Array[T]) Reduce(iterator func(accumulator T, currentValue T, currentIndex int, arr Array[T]) T, initialValue T) T {
accumulator := initialValue
for idx, el := range arr {
accumulator = iterator(accumulator, el, idx, arr)
}
return accumulator
}