Skip to content

Commit a34ae68

Browse files
Sah, NandeshwarSah, Nandeshwar
authored andcommitted
Added function TakeWhile<TYPE>Err which returns new list and error
1 parent 397de73 commit a34ae68

File tree

11 files changed

+2593
-0
lines changed

11 files changed

+2593
-0
lines changed

fp/takewhileerr.go

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

0 commit comments

Comments
 (0)