Skip to content

Commit 8130ca0

Browse files
Merge pull request #97 from logic-building/Remove<TYPE>Err
Added function Remove<TYPE>Err which returns new list and error
2 parents a5c1bda + a278508 commit 8130ca0

File tree

11 files changed

+2602
-0
lines changed

11 files changed

+2602
-0
lines changed

fp/removeerr.go

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
package fp
2+
3+
// RemoveIntErr removes the items from the given list based on supplied function and returns new list
4+
//
5+
// Takes 2 inputs:
6+
// 1. Function: input type int and return types(bool, error)
7+
// 2. List of type: []int
8+
//
9+
// Returns:
10+
// New list and error: ([]int, error)
11+
// Empty list if both of arguments are nil or either one is nil.
12+
func RemoveIntErr(f func(int) (bool, error), list []int) ([]int, error) {
13+
if f == nil {
14+
return []int{}, nil
15+
}
16+
var newList []int
17+
for _, v := range list {
18+
r, err := f(v)
19+
if err != nil {
20+
return nil, err
21+
}
22+
if !r {
23+
newList = append(newList, v)
24+
}
25+
}
26+
return newList, nil
27+
}
28+
29+
// RemoveInt64Err removes the items from the given list based on supplied function and returns new list
30+
//
31+
// Takes 2 inputs:
32+
// 1. Function: input type int64 and return types(bool, error)
33+
// 2. List of type: []int64
34+
//
35+
// Returns:
36+
// New list and error: ([]int64, error)
37+
// Empty list if both of arguments are nil or either one is nil.
38+
func RemoveInt64Err(f func(int64) (bool, error), list []int64) ([]int64, error) {
39+
if f == nil {
40+
return []int64{}, nil
41+
}
42+
var newList []int64
43+
for _, v := range list {
44+
r, err := f(v)
45+
if err != nil {
46+
return nil, err
47+
}
48+
if !r {
49+
newList = append(newList, v)
50+
}
51+
}
52+
return newList, nil
53+
}
54+
55+
// RemoveInt32Err removes the items from the given list based on supplied function and returns new list
56+
//
57+
// Takes 2 inputs:
58+
// 1. Function: input type int32 and return types(bool, error)
59+
// 2. List of type: []int32
60+
//
61+
// Returns:
62+
// New list and error: ([]int32, error)
63+
// Empty list if both of arguments are nil or either one is nil.
64+
func RemoveInt32Err(f func(int32) (bool, error), list []int32) ([]int32, error) {
65+
if f == nil {
66+
return []int32{}, nil
67+
}
68+
var newList []int32
69+
for _, v := range list {
70+
r, err := f(v)
71+
if err != nil {
72+
return nil, err
73+
}
74+
if !r {
75+
newList = append(newList, v)
76+
}
77+
}
78+
return newList, nil
79+
}
80+
81+
// RemoveInt16Err removes the items from the given list based on supplied function and returns new list
82+
//
83+
// Takes 2 inputs:
84+
// 1. Function: input type int16 and return types(bool, error)
85+
// 2. List of type: []int16
86+
//
87+
// Returns:
88+
// New list and error: ([]int16, error)
89+
// Empty list if both of arguments are nil or either one is nil.
90+
func RemoveInt16Err(f func(int16) (bool, error), list []int16) ([]int16, error) {
91+
if f == nil {
92+
return []int16{}, nil
93+
}
94+
var newList []int16
95+
for _, v := range list {
96+
r, err := f(v)
97+
if err != nil {
98+
return nil, err
99+
}
100+
if !r {
101+
newList = append(newList, v)
102+
}
103+
}
104+
return newList, nil
105+
}
106+
107+
// RemoveInt8Err removes the items from the given list based on supplied function and returns new list
108+
//
109+
// Takes 2 inputs:
110+
// 1. Function: input type int8 and return types(bool, error)
111+
// 2. List of type: []int8
112+
//
113+
// Returns:
114+
// New list and error: ([]int8, error)
115+
// Empty list if both of arguments are nil or either one is nil.
116+
func RemoveInt8Err(f func(int8) (bool, error), list []int8) ([]int8, error) {
117+
if f == nil {
118+
return []int8{}, nil
119+
}
120+
var newList []int8
121+
for _, v := range list {
122+
r, err := f(v)
123+
if err != nil {
124+
return nil, err
125+
}
126+
if !r {
127+
newList = append(newList, v)
128+
}
129+
}
130+
return newList, nil
131+
}
132+
133+
// RemoveUintErr removes the items from the given list based on supplied function and returns new list
134+
//
135+
// Takes 2 inputs:
136+
// 1. Function: input type uint and return types(bool, error)
137+
// 2. List of type: []uint
138+
//
139+
// Returns:
140+
// New list and error: ([]uint, error)
141+
// Empty list if both of arguments are nil or either one is nil.
142+
func RemoveUintErr(f func(uint) (bool, error), list []uint) ([]uint, error) {
143+
if f == nil {
144+
return []uint{}, nil
145+
}
146+
var newList []uint
147+
for _, v := range list {
148+
r, err := f(v)
149+
if err != nil {
150+
return nil, err
151+
}
152+
if !r {
153+
newList = append(newList, v)
154+
}
155+
}
156+
return newList, nil
157+
}
158+
159+
// RemoveUint64Err removes the items from the given list based on supplied function and returns new list
160+
//
161+
// Takes 2 inputs:
162+
// 1. Function: input type uint64 and return types(bool, error)
163+
// 2. List of type: []uint64
164+
//
165+
// Returns:
166+
// New list and error: ([]uint64, error)
167+
// Empty list if both of arguments are nil or either one is nil.
168+
func RemoveUint64Err(f func(uint64) (bool, error), list []uint64) ([]uint64, error) {
169+
if f == nil {
170+
return []uint64{}, nil
171+
}
172+
var newList []uint64
173+
for _, v := range list {
174+
r, err := f(v)
175+
if err != nil {
176+
return nil, err
177+
}
178+
if !r {
179+
newList = append(newList, v)
180+
}
181+
}
182+
return newList, nil
183+
}
184+
185+
// RemoveUint32Err removes the items from the given list based on supplied function and returns new list
186+
//
187+
// Takes 2 inputs:
188+
// 1. Function: input type uint32 and return types(bool, error)
189+
// 2. List of type: []uint32
190+
//
191+
// Returns:
192+
// New list and error: ([]uint32, error)
193+
// Empty list if both of arguments are nil or either one is nil.
194+
func RemoveUint32Err(f func(uint32) (bool, error), list []uint32) ([]uint32, error) {
195+
if f == nil {
196+
return []uint32{}, nil
197+
}
198+
var newList []uint32
199+
for _, v := range list {
200+
r, err := f(v)
201+
if err != nil {
202+
return nil, err
203+
}
204+
if !r {
205+
newList = append(newList, v)
206+
}
207+
}
208+
return newList, nil
209+
}
210+
211+
// RemoveUint16Err removes the items from the given list based on supplied function and returns new list
212+
//
213+
// Takes 2 inputs:
214+
// 1. Function: input type uint16 and return types(bool, error)
215+
// 2. List of type: []uint16
216+
//
217+
// Returns:
218+
// New list and error: ([]uint16, error)
219+
// Empty list if both of arguments are nil or either one is nil.
220+
func RemoveUint16Err(f func(uint16) (bool, error), list []uint16) ([]uint16, error) {
221+
if f == nil {
222+
return []uint16{}, nil
223+
}
224+
var newList []uint16
225+
for _, v := range list {
226+
r, err := f(v)
227+
if err != nil {
228+
return nil, err
229+
}
230+
if !r {
231+
newList = append(newList, v)
232+
}
233+
}
234+
return newList, nil
235+
}
236+
237+
// RemoveUint8Err removes the items from the given list based on supplied function and returns new list
238+
//
239+
// Takes 2 inputs:
240+
// 1. Function: input type uint8 and return types(bool, error)
241+
// 2. List of type: []uint8
242+
//
243+
// Returns:
244+
// New list and error: ([]uint8, error)
245+
// Empty list if both of arguments are nil or either one is nil.
246+
func RemoveUint8Err(f func(uint8) (bool, error), list []uint8) ([]uint8, error) {
247+
if f == nil {
248+
return []uint8{}, nil
249+
}
250+
var newList []uint8
251+
for _, v := range list {
252+
r, err := f(v)
253+
if err != nil {
254+
return nil, err
255+
}
256+
if !r {
257+
newList = append(newList, v)
258+
}
259+
}
260+
return newList, nil
261+
}
262+
263+
// RemoveStrErr removes the items from the given list based on supplied function and returns new list
264+
//
265+
// Takes 2 inputs:
266+
// 1. Function: input type string and return types(bool, error)
267+
// 2. List of type: []string
268+
//
269+
// Returns:
270+
// New list and error: ([]string, error)
271+
// Empty list if both of arguments are nil or either one is nil.
272+
func RemoveStrErr(f func(string) (bool, error), list []string) ([]string, error) {
273+
if f == nil {
274+
return []string{}, nil
275+
}
276+
var newList []string
277+
for _, v := range list {
278+
r, err := f(v)
279+
if err != nil {
280+
return nil, err
281+
}
282+
if !r {
283+
newList = append(newList, v)
284+
}
285+
}
286+
return newList, nil
287+
}
288+
289+
// RemoveBoolErr removes the items from the given list based on supplied function and returns new list
290+
//
291+
// Takes 2 inputs:
292+
// 1. Function: input type bool and return types(bool, error)
293+
// 2. List of type: []bool
294+
//
295+
// Returns:
296+
// New list and error: ([]bool, error)
297+
// Empty list if both of arguments are nil or either one is nil.
298+
func RemoveBoolErr(f func(bool) (bool, error), list []bool) ([]bool, error) {
299+
if f == nil {
300+
return []bool{}, nil
301+
}
302+
var newList []bool
303+
for _, v := range list {
304+
r, err := f(v)
305+
if err != nil {
306+
return nil, err
307+
}
308+
if !r {
309+
newList = append(newList, v)
310+
}
311+
}
312+
return newList, nil
313+
}
314+
315+
// RemoveFloat32Err removes the items from the given list based on supplied function and returns new list
316+
//
317+
// Takes 2 inputs:
318+
// 1. Function: input type float32 and return types(bool, error)
319+
// 2. List of type: []float32
320+
//
321+
// Returns:
322+
// New list and error: ([]float32, error)
323+
// Empty list if both of arguments are nil or either one is nil.
324+
func RemoveFloat32Err(f func(float32) (bool, error), list []float32) ([]float32, error) {
325+
if f == nil {
326+
return []float32{}, nil
327+
}
328+
var newList []float32
329+
for _, v := range list {
330+
r, err := f(v)
331+
if err != nil {
332+
return nil, err
333+
}
334+
if !r {
335+
newList = append(newList, v)
336+
}
337+
}
338+
return newList, nil
339+
}
340+
341+
// RemoveFloat64Err removes the items from the given list based on supplied function and returns new list
342+
//
343+
// Takes 2 inputs:
344+
// 1. Function: input type float64 and return types(bool, error)
345+
// 2. List of type: []float64
346+
//
347+
// Returns:
348+
// New list and error: ([]float64, error)
349+
// Empty list if both of arguments are nil or either one is nil.
350+
func RemoveFloat64Err(f func(float64) (bool, error), list []float64) ([]float64, error) {
351+
if f == nil {
352+
return []float64{}, nil
353+
}
354+
var newList []float64
355+
for _, v := range list {
356+
r, err := f(v)
357+
if err != nil {
358+
return nil, err
359+
}
360+
if !r {
361+
newList = append(newList, v)
362+
}
363+
}
364+
return newList, nil
365+
}

0 commit comments

Comments
 (0)