@@ -17,12 +17,20 @@ func Map[T any, U any](input []T, callback func(element T, index int) U) []U {
17
17
return output
18
18
}
19
19
20
- func Filter [T any ](input []T , callback func (element T , index int ) bool ) []T {
20
+ // conditions can be callback or a value
21
+ // callback has two parameter one is element and another is index
22
+ // callback must return true or false
23
+ func Filter [T any ](input []T , conditions interface {}) []T {
21
24
var output []T
25
+ conditionType := getType (conditions )
22
26
23
27
for index , value := range input {
24
- if callback ( value , index ) {
28
+ if conditionType == "func" && callCallback ( conditions , value , index ) {
25
29
output = append (output , value )
30
+ continue
31
+ } else if conditionType != "func" && compareValue (conditions , value ) {
32
+ output = append (output , value )
33
+ continue
26
34
}
27
35
}
28
36
@@ -39,10 +47,17 @@ func Reduce[T any, U any](input []T, callback func(pre U, current T, index int)
39
47
return output
40
48
}
41
49
42
- func Find [T any ](input []T , callback func (element T , index int ) bool ) T {
50
+ // conditions can be callback or a value
51
+ // callback has two parameter one is element and another is index
52
+ // callback must return true or false
53
+ func Find [T any ](input []T , conditions interface {}) T {
43
54
var item T
55
+ conditionType := getType (conditions )
44
56
for index , value := range input {
45
- if callback (value , index ) {
57
+ if conditionType == "func" && callCallback (conditions , value , index ) {
58
+ item = value
59
+ break
60
+ } else if conditionType != "func" && compareValue (conditions , value ) {
46
61
item = value
47
62
break
48
63
}
@@ -57,21 +72,31 @@ func Foreach[T any](input []T, callback func(element T, index int)) {
57
72
}
58
73
}
59
74
75
+ func callCallback [T any ](conditions interface {}, value T , index int ) bool {
76
+ res := reflect .ValueOf (conditions ).Call ([]reflect.Value {reflect .ValueOf (value ), reflect .ValueOf (index )})
77
+
78
+ return res [0 ].Bool ()
79
+ }
80
+
81
+ func compareValue [T any ](conditions interface {}, value T ) bool {
82
+ res := reflect .ValueOf (conditions ).String ()
83
+ return res == fmt .Sprintf ("%v" , value )
84
+ }
85
+
86
+ func getType (conditions interface {}) string {
87
+ return reflect .TypeOf (conditions ).Kind ().String ()
88
+ }
89
+
60
90
func calculateEveryElement [T any ](input []T , conditions interface {}) []bool {
61
91
output := []bool {}
62
92
63
- conditionType := reflect .TypeOf (conditions ).Kind ().String ()
64
-
93
+ conditionType := getType (conditions )
65
94
for index , value := range input {
66
95
result := false
67
96
if conditionType == "func" {
68
- res := reflect .ValueOf (conditions ).Call ([]reflect.Value {reflect .ValueOf (value ), reflect .ValueOf (index )})
69
- result = res [0 ].Bool ()
97
+ result = callCallback (conditions , value , index )
70
98
} else {
71
- res := reflect .ValueOf (conditions ).String ()
72
- if res == fmt .Sprintf ("%v" , value ) {
73
- result = true
74
- }
99
+ result = compareValue (conditions , value )
75
100
}
76
101
if result {
77
102
output = append (output , true )
@@ -96,3 +121,14 @@ func Some[T any](input []T, conditions interface{}) any {
96
121
output := calculateEveryElement (input , conditions )
97
122
return len (output ) > 0
98
123
}
124
+
125
+ func Includes [T any ](input []T , conditions interface {}) bool {
126
+ result := Find (input , conditions )
127
+
128
+ if getType (result ) == "struct" && reflect .ValueOf (result ).IsZero () {
129
+ return false
130
+ } else if getType (result ) != "struct" && fmt .Sprintf ("%v" , result ) == "" {
131
+ return false
132
+ }
133
+ return true
134
+ }
0 commit comments