forked from Masterminds/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
109 lines (95 loc) · 1.87 KB
/
list.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
package sprig
import (
"reflect"
"sort"
)
func list(v ...interface{}) []interface{} {
return v
}
func push(list []interface{}, v interface{}) []interface{} {
return append(list, v)
}
func prepend(list []interface{}, v interface{}) []interface{} {
return append([]interface{}{v}, list...)
}
func last(list []interface{}) interface{} {
l := len(list)
if l == 0 {
return nil
}
return list[l-1]
}
func first(list []interface{}) interface{} {
if len(list) == 0 {
return nil
}
return list[0]
}
func rest(list []interface{}) []interface{} {
if len(list) == 0 {
return list
}
return list[1:]
}
func initial(list []interface{}) []interface{} {
l := len(list)
if l == 0 {
return list
}
return list[:l-1]
}
func sortAlpha(list interface{}) []string {
k := reflect.Indirect(reflect.ValueOf(list)).Kind()
switch k {
case reflect.Slice, reflect.Array:
a := strslice(list)
s := sort.StringSlice(a)
s.Sort()
return s
}
return []string{strval(list)}
}
func reverse(v []interface{}) []interface{} {
// We do not sort in place because the incomming array should not be altered.
l := len(v)
c := make([]interface{}, l)
for i := 0; i < l; i++ {
c[l-i-1] = v[i]
}
return c
}
func compact(list []interface{}) []interface{} {
res := []interface{}{}
for _, item := range list {
if !empty(item) {
res = append(res, item)
}
}
return res
}
func uniq(list []interface{}) []interface{} {
dest := []interface{}{}
for _, item := range list {
if !inList(dest, item) {
dest = append(dest, item)
}
}
return dest
}
func inList(haystack []interface{}, needle interface{}) bool {
for _, h := range haystack {
if reflect.DeepEqual(needle, h) {
return true
}
}
return false
}
func without(list []interface{}, omit ...interface{}) []interface{} {
res := []interface{}{}
for _, i := range list {
if !inList(omit, i) {
res = append(res, i)
}
}
return res
}