├── README.md ├── functional.go └── functional_test.go /README.md: -------------------------------------------------------------------------------- 1 | ## Functional Programming Constructs in Golang 2 | 3 | These functions where coded to understand the difficulty of implementing these functions in golang. 4 | I do not recommend you use it. A simple for loop will do and work in most of the cases. 5 | 6 | #### Check out the test file to see the usage of these 7 | 8 | ### FUNCTIONS 9 | 10 | ```go 11 | 12 | // Definition of Filter function 13 | type filterFunc func(interface{}) bool 14 | 15 | func Filter(fn filterFunc, array interface{}) []interface{} 16 | Filter filters the array based on the predicate 17 | ``` 18 | 19 | ```go 20 | 21 | // Definition of Foldl function 22 | type foldlFunc func(interface{}, interface{}) interface{} 23 | 24 | func Foldl(fn foldrFunc, array interface{}, accumulator interface{}) interface{} 25 | Folds left the array values (reduction) based on the function 26 | ``` 27 | 28 | ```go 29 | 30 | // Definition of Map function 31 | type mapFunc func(interface{}) interface{} 32 | 33 | func Map(fn mapFunc, array interface{}) []interface{} 34 | Map maps the function onto the array 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /functional.go: -------------------------------------------------------------------------------- 1 | package functional 2 | 3 | import "reflect" 4 | 5 | // Definition of Map function 6 | type mapFunc func(interface{}) interface{} 7 | 8 | // Map maps the function onto the array 9 | func Map(fn mapFunc, array interface{}) []interface{} { 10 | val := reflect.ValueOf(array) 11 | outputArray := make([]interface{}, val.Len()) 12 | for i := 0; i < val.Len(); i++ { 13 | outputArray[i] = fn(val.Index(i).Interface()) 14 | } 15 | return outputArray 16 | } 17 | 18 | // Definition of Filter function 19 | type filterFunc func(interface{}) bool 20 | 21 | // Filter filters the array based on the predicate 22 | func Filter(fn filterFunc, array interface{}) []interface{} { 23 | val := reflect.ValueOf(array) 24 | var outputArray []interface{} 25 | for i := 0; i < val.Len(); i++ { 26 | if fn(val.Index(i).Interface()) { 27 | outputArray = append(outputArray, val.Index(i).Interface()) 28 | } 29 | } 30 | return outputArray 31 | } 32 | 33 | // Definition of Foldl function 34 | type foldlFunc func(interface{}, interface{}) interface{} 35 | 36 | // Folds left the array values (reduction) based on the function 37 | func Foldl(fn foldlFunc, array interface{}, accumulator interface{}) interface{} { 38 | val := reflect.ValueOf(array) 39 | var result = accumulator 40 | for i := 0; i < val.Len(); i++ { 41 | result = fn(val.Index(i).Interface(), result) 42 | } 43 | return result 44 | } 45 | -------------------------------------------------------------------------------- /functional_test.go: -------------------------------------------------------------------------------- 1 | package functional 2 | 3 | import "testing" 4 | 5 | func TestMap(t *testing.T) { 6 | inputList := []int{1, 2, 3} 7 | 8 | square := func(x interface{}) interface{} { 9 | return x.(int) * x.(int) 10 | } 11 | 12 | expected := []int{1, 4, 9} 13 | actual := Map(square, inputList) 14 | 15 | for i, e := range expected { 16 | if e != actual[i] { 17 | t.Errorf("expected %v != actual %v", expected, actual) 18 | } 19 | } 20 | } 21 | 22 | func TestFilter(t *testing.T) { 23 | inputList := []int{1, 2, 3} 24 | 25 | modulotwo := func(x interface{}) bool { 26 | return x.(int)%2 == 0 27 | } 28 | 29 | expected := []int{2} 30 | actual := Filter(modulotwo, inputList) 31 | 32 | for i, e := range expected { 33 | if e != actual[i] { 34 | t.Errorf("expected %v != actual %v", expected, actual) 35 | } 36 | } 37 | } 38 | 39 | func TestFoldl(t *testing.T) { 40 | inputList := []int{1, 2, 3} 41 | 42 | funcFoldl := func(x interface{}, acc interface{}) interface{} { 43 | return x.(int) + acc.(int) 44 | } 45 | 46 | expected := 6 47 | accumulator := 0 48 | actual := Foldl(funcFoldl, inputList, accumulator) 49 | 50 | if expected != actual { 51 | t.Errorf("expected %v != actual %v", expected, actual) 52 | } 53 | } 54 | --------------------------------------------------------------------------------