Skip to content

Commit fe1e2bd

Browse files
author
Sah, Nandeshwar
committed
Added function-dropWhile in methodchain functionality
1 parent 6b004a7 commit fe1e2bd

File tree

12 files changed

+2361
-7
lines changed

12 files changed

+2361
-7
lines changed

fp/dropwhile.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,33 @@ func DropWhileStr(f func(string) bool, list []string) []string {
480480
}
481481
return newList
482482
}
483+
484+
// DropWhileBool drops the items from the list as long as condition satisfies.
485+
//
486+
// Takes two inputs
487+
// 1. Function: takes one input and returns boolean
488+
// 2. list
489+
//
490+
// Returns:
491+
// New List.
492+
// Empty list if either one of arguments or both of them are nil
493+
func DropWhileBool(f func(bool) bool, list []bool) []bool {
494+
if f == nil {
495+
return []bool{}
496+
}
497+
var newList []bool
498+
for i, v := range list {
499+
if !f(v) {
500+
listLen := len(list)
501+
newList = make([]bool, listLen-i)
502+
j := 0
503+
for i < listLen {
504+
newList[j] = list[i]
505+
i++
506+
j++
507+
}
508+
return newList
509+
}
510+
}
511+
return newList
512+
}

fp/dropwhile_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,18 @@ func TestDropWhileStr(t *testing.T) {
377377
t.Errorf("DropWhileStr failed.")
378378
}
379379
}
380+
381+
func TestDropWhileBool(t *testing.T) {
382+
var vt bool = true
383+
var vf bool = false
384+
385+
isTrueBool := func(num bool) bool {
386+
return num == true
387+
}
388+
389+
expectedNewList := []bool{vf, vt}
390+
NewList := DropWhileBool(isTrueBool, []bool{vt, vf, vt})
391+
if NewList[0] != expectedNewList[0] || NewList[1] != expectedNewList[1] {
392+
t.Errorf("DropWhileBool failed. Expected New list=%v, actual list=%v", expectedNewList, NewList)
393+
}
394+
}

0 commit comments

Comments
 (0)