├── map.go └── slice.go /map.go: -------------------------------------------------------------------------------- 1 | package beeku 2 | 3 | import ( 4 | "sort" 5 | ) 6 | 7 | type MapSorter struct { 8 | Keys []string 9 | Vals []string 10 | } 11 | 12 | func NewMapSorter(m map[string]string) *MapSorter { 13 | ms := &MapSorter{ 14 | Keys: make([]string, 0, len(m)), 15 | Vals: make([]string, 0, len(m)), 16 | } 17 | for k, v := range m { 18 | ms.Keys = append(ms.Keys, k) 19 | ms.Vals = append(ms.Vals, v) 20 | } 21 | return ms 22 | } 23 | 24 | func (ms *MapSorter) Sort() { 25 | sort.Sort(ms) 26 | } 27 | 28 | func (ms *MapSorter) Len() int { return len(ms.Keys) } 29 | func (ms *MapSorter) Less(i, j int) bool { return ms.Keys[i] < ms.Keys[j] } 30 | func (ms *MapSorter) Swap(i, j int) { 31 | ms.Vals[i], ms.Vals[j] = ms.Vals[j], ms.Vals[i] 32 | ms.Keys[i], ms.Keys[j] = ms.Keys[j], ms.Keys[i] 33 | } 34 | -------------------------------------------------------------------------------- /slice.go: -------------------------------------------------------------------------------- 1 | package beeku 2 | 3 | import ( 4 | "math/rand" 5 | "time" 6 | ) 7 | 8 | type reducetype func(interface{}) interface{} 9 | type filtertype func(interface{}) bool 10 | 11 | func Slice_randList(min, max int) []int { 12 | if max < min { 13 | min, max = max, min 14 | } 15 | length := max - min + 1 16 | t0 := time.Now() 17 | rand.Seed(int64(t0.Nanosecond())) 18 | list := rand.Perm(length) 19 | for index, _ := range list { 20 | list[index] += min 21 | } 22 | return list 23 | } 24 | 25 | func Slice_merge(slice1, slice2 []interface{}) (c []interface{}) { 26 | c = append(slice1, slice2...) 27 | return 28 | } 29 | 30 | func In_slice(val interface{}, slice []interface{}) bool { 31 | for _, v := range slice { 32 | if v == val { 33 | return true 34 | } 35 | } 36 | return false 37 | } 38 | 39 | func Slice_reduce(slice []interface{}, a reducetype) (dslice []interface{}) { 40 | for _, v := range slice { 41 | dslice = append(dslice, a(v)) 42 | } 43 | return 44 | } 45 | 46 | func Slice_rand(a []interface{}) (b interface{}) { 47 | randnum := rand.Intn(len(a)) 48 | b = a[randnum] 49 | return 50 | } 51 | 52 | func Slice_sum(intslice []int64) (sum int64) { 53 | for _, v := range intslice { 54 | sum += v 55 | } 56 | return 57 | } 58 | 59 | func Slice_filter(slice []interface{}, a filtertype) (ftslice []interface{}) { 60 | for _, v := range slice { 61 | if a(v) { 62 | ftslice = append(ftslice, v) 63 | } 64 | } 65 | return 66 | } 67 | 68 | func Slice_diff(slice1, slice2 []interface{}) (diffslice []interface{}) { 69 | for _, v := range slice1 { 70 | if !In_slice(v, slice2) { 71 | diffslice = append(diffslice, v) 72 | } 73 | } 74 | return 75 | } 76 | 77 | func Slice_intersect(slice1, slice2 []interface{}) (diffslice []interface{}) { 78 | for _, v := range slice1 { 79 | if !In_slice(v, slice2) { 80 | diffslice = append(diffslice, v) 81 | } 82 | } 83 | return 84 | } 85 | 86 | func Slice_chunk(slice []interface{}, size int) (chunkslice [][]interface{}) { 87 | if size >= len(slice) { 88 | chunkslice = append(chunkslice, slice) 89 | return 90 | } 91 | end := size 92 | for i := 0; i <= (len(slice) - size); i += size { 93 | chunkslice = append(chunkslice, slice[i:end]) 94 | end += size 95 | } 96 | return 97 | } 98 | 99 | func Slice_range(start, end, step int64) (intslice []int64) { 100 | for i := start; i <= end; i += step { 101 | intslice = append(intslice, i) 102 | } 103 | return 104 | } 105 | 106 | func Slice_pad(slice []interface{}, size int, val interface{}) []interface{} { 107 | if size <= len(slice) { 108 | return slice 109 | } 110 | for i := 0; i < (size - len(slice)); i++ { 111 | slice = append(slice, val) 112 | } 113 | return slice 114 | } 115 | 116 | func Slice_unique(slice []interface{}) (uniqueslice []interface{}) { 117 | for _, v := range slice { 118 | if !In_slice(v, uniqueslice) { 119 | uniqueslice = append(uniqueslice, v) 120 | } 121 | } 122 | return 123 | } 124 | 125 | func Slice_shuffle(slice []interface{}) []interface{} { 126 | for i := 0; i < len(slice); i++ { 127 | a := rand.Intn(len(slice)) 128 | b := rand.Intn(len(slice)) 129 | slice[a], slice[b] = slice[b], slice[a] 130 | } 131 | return slice 132 | } 133 | --------------------------------------------------------------------------------