Skip to content

Commit f848d85

Browse files
author
Sah, Nandeshwar
committed
Added method filter in methodchain
1 parent 4acaf51 commit f848d85

File tree

15 files changed

+2357
-178
lines changed

15 files changed

+2357
-178
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ jobs: # basic units of work in a run
33
build: # runs not using Workflows must have a `build` job as entry point
44
docker: # run the steps with Docker
55
# CircleCI Go images available at: https://hub.docker.com/r/circleci/golang/
6-
- image: circleci/golang:1.11 #
6+
- image: circleci/golang:1.15 #
77

88
# directory where steps are run. Path must conform to the Go Workspace requirements
99
working_directory: /go/src/github.com/logic-building/functional-go/

fp/filter.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,22 @@ func FilterStr(f func(string) bool, list []string) []string {
376376
}
377377
return newList
378378
}
379+
380+
// FilterBool takes two arguments
381+
// 1. Function: takes 1 argument of type bool and returns bool
382+
// 2. slice of type []bool
383+
//
384+
// Returns:
385+
// new filtered list
386+
func FilterBool(f func(bool) bool, list []bool) []bool {
387+
if f == nil {
388+
return []bool{}
389+
}
390+
var newList []bool
391+
for _, v := range list {
392+
if f(v) {
393+
newList = append(newList, v)
394+
}
395+
}
396+
return newList
397+
}

fp/filter_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,22 @@ func TestFilterStr(t *testing.T) {
365365
func isStringLengthLessThan10(str string) bool {
366366
return len(str) < 10
367367
}
368+
369+
func TestFilterBool(t *testing.T) {
370+
var vt bool = true
371+
372+
expectedSumList := []bool{vt}
373+
374+
newList := FilterBool(trueBool, []bool{vt})
375+
if newList[0] != expectedSumList[0] {
376+
t.Errorf("FilterBoolPtr failed")
377+
}
378+
379+
if len(FilterBoolPtr(nil, nil)) > 0 {
380+
t.Errorf("MapBoolPtr failed.")
381+
}
382+
}
383+
384+
func trueBool(num1 bool) bool {
385+
return true
386+
}

fp/map.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,23 @@ func MapStr(f func(string) string, list []string) []string {
346346
}
347347
return newList
348348
}
349+
350+
// MapBool applies the function(1st argument) on each item of the list and returns new list
351+
//
352+
// Takes 2 inputs
353+
// 1. Function - takes 1 input
354+
// 2. List
355+
//
356+
// Returns
357+
// New List.
358+
// Empty list if all arguments are nil or either one is nil
359+
func MapBool(f func(bool) bool, list []bool) []bool {
360+
if f == nil {
361+
return []bool{}
362+
}
363+
newList := make([]bool, len(list))
364+
for i, v := range list {
365+
newList[i] = f(v)
366+
}
367+
return newList
368+
}

0 commit comments

Comments
 (0)