-
Notifications
You must be signed in to change notification settings - Fork 1
/
without.go
37 lines (29 loc) · 987 Bytes
/
without.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
package array
import (
"reflect"
"github.com/warriors-vn/go-dash/constants"
)
// Without returns a new Slice that excludes specified values from the input array.
// It takes an array-like data structure and a variable number of values to be excluded.
// The function returns the new Slice Without the specified values and an error if any occurs.
func Without(array interface{}, values ...interface{}) (interface{}, error) {
arrValue := reflect.ValueOf(array)
if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array {
return nil, constants.ErrNotSlice
}
if values == nil || arrValue.Len() == 0 {
return array, nil
}
mapValue := make(map[interface{}]bool)
for _, v := range values {
mapValue[v] = true
}
result := reflect.MakeSlice(arrValue.Type(), 0, 0)
for i := 0; i < arrValue.Len(); i++ {
element := arrValue.Index(i)
if !mapValue[element.Interface()] {
result = reflect.Append(result, element)
}
}
return result.Interface(), nil
}