-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbarray.go
240 lines (222 loc) · 4.86 KB
/
rbarray.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package rbarray
import (
"fmt"
)
type IntArray []int
type StrArray []string
type Array struct {
IntVals IntArray
StrVals StrArray
}
// instance method Array#pop
// pop() -> object | error
func (a *Array) Pop() (interface{}, error) {
if len(a.IntVals) > 0 {
slice := a.IntVals
last := slice[len(slice)-1]
a.IntVals = slice[:len(slice)-1]
return last, nil
}
if len(a.StrVals) > 0 {
slice := a.StrVals
last := slice[len(slice)-1]
a.StrVals = slice[:len(slice)-1]
return last, nil
}
return nil, fmt.Errorf("Array is empty")
}
// instance method Array#shift
// shift() -> object | error
func (a *Array) Shift() (interface{}, error) {
if len(a.IntVals) > 0 {
slice := a.IntVals
first := slice[0]
a.IntVals = slice[1:]
return first, nil
}
if len(a.StrVals) > 0 {
slice := a.StrVals
first := slice[0]
a.StrVals = slice[1:]
return first, nil
}
return nil, fmt.Errorf("Array is empty")
}
// instance method Array#push
// push(*obj) -> self | error
func (a *Array) Push(obj interface{}) (interface{}, error) {
switch v := obj.(type) {
case int:
a.IntVals = append(a.IntVals, v)
case string:
a.StrVals = append(a.StrVals, v)
default:
return *a, fmt.Errorf("invalid type: %T", obj)
}
return *a, nil
}
// instance method Array#unshift
// unshift(*obj) -> self | error
func (a *Array) Unshift(obj interface{}) (interface{}, error) {
switch v := obj.(type) {
case int:
a.IntVals = append(IntArray{v}, a.IntVals...)
case string:
a.StrVals = append(StrArray{v}, a.StrVals...)
default:
return *a, fmt.Errorf("invalid type: %T", obj)
}
return *a, nil
}
// instance method Array#delete
// delete(val) { ... } -> object | error
func (a *Array) Delete(obj interface{}) (interface{}, error) {
switch v := obj.(type) {
case int:
for i, n := range a.IntVals {
if n == v {
a.IntVals = append(a.IntVals[:i], a.IntVals[i+1:]...)
return n, nil
}
}
case string:
for i, s := range a.StrVals {
if s == v {
a.StrVals = append(a.StrVals[:i], a.StrVals[i+1:]...)
return s, nil
}
}
}
return nil, fmt.Errorf("invalid type: %T", obj)
}
// instance method Array#uniq
// uniq() -> Array
func (a *Array) Uniq() interface{} {
seen := make(map[interface{}]bool)
var uniqInts IntArray
var uniqStrs StrArray
for _, n := range a.IntVals {
if _, ok := seen[n]; !ok {
seen[n] = true
uniqInts = append(uniqInts, n)
}
}
for _, s := range a.StrVals {
if _, ok := seen[s]; !ok {
seen[s] = true
uniqStrs = append(uniqStrs, s)
}
}
a.IntVals = uniqInts
a.StrVals = uniqStrs
return *a
}
// Only works for IntArray
// instance method Enumerable#sum
// sum() -> object | error
func (a *Array) Sum() (interface{}, error) {
if len(a.IntVals) == 0 {
return nil, fmt.Errorf("cannot sum empty IntArray")
}
var sum int
for _, n := range a.IntVals {
sum += n
}
return sum, nil
}
// Only works for IntArray
// instance method Enumerable#max
// max() -> object | error
func (a *Array) Max() (interface{}, error) {
if len(a.IntVals) == 0 {
return nil, fmt.Errorf("cannot max empty IntArray")
}
max := -int(^uint(0)>>1) - 1
for _, n := range a.IntVals {
if n > max {
max = n
}
}
return max, nil
}
// Only works for IntArray
// instance method Enumerable#min
// min() -> object | error
func (a *Array) Min() (interface{}, error) {
if len(a.IntVals) == 0 {
return nil, fmt.Errorf("cannot min empty IntArray")
}
min := int(^uint(0) >> 1)
for _, n := range a.IntVals {
if n < min {
min = n
}
}
return min, nil
}
// instance method Array#size
// size -> Integer(for int) / Integer(for string)
func (a *Array) Size() (int, int) {
return len(a.IntVals), len(a.StrVals)
}
// instance method Array#include
// include(val) -> bool
func (a *Array) Include(obj interface{}) bool {
switch v := obj.(type) {
case int:
for _, n := range a.IntVals {
if n == v {
return true
}
}
case string:
for _, s := range a.StrVals {
if s == v {
return true
}
}
}
return false
}
// instance method Array#first
// first -> object
func (a *Array) First() interface{} {
if len(a.IntVals) > 0 {
return a.IntVals[0]
}
if len(a.StrVals) > 0 {
return a.StrVals[0]
}
return nil
}
// instance method Array#last
// last -> object
func (a *Array) Last() interface{} {
if len(a.IntVals) > 0 {
return a.IntVals[len(a.IntVals)-1]
}
if len(a.StrVals) > 0 {
return a.StrVals[len(a.StrVals)-1]
}
return nil
}
// instance method Array#empty
// empty -> bool
func (a *Array) Empty() bool {
return len(a.IntVals) == 0 && len(a.StrVals) == 0
}
// instance method Array#reverse
// reverse -> Array
func (a *Array) Reverse() interface{} {
var reverseInts IntArray
var reverseStrs StrArray
for i := len(a.IntVals) - 1; i >= 0; i-- {
reverseInts = append(reverseInts, a.IntVals[i])
}
for i := len(a.StrVals) - 1; i >= 0; i-- {
reverseStrs = append(reverseStrs, a.StrVals[i])
}
a.IntVals = reverseInts
a.StrVals = reverseStrs
return *a
}