Created
September 16, 2017 19:21
-
-
Save derrandz/9753e3a687551944aca03a9e1cdd51ef to your computer and use it in GitHub Desktop.
Messing around with go and js style reducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type reducer func(int, string, int, []string) int | |
func ReduceStringArr(arr []string, fn reducer, acc int) int { | |
for i, str := range arr { | |
acc = fn(acc, str, i, arr) | |
} | |
return acc; | |
} | |
func main() { | |
names := []string{ | |
"Jackson", "Brad", "Felany", "Stephanie", | |
} | |
totalNamesLength := ReduceStringArr(names, func(acc int, name string, i int, _names []string) int { | |
return int(acc + len(name)) | |
}, 0); | |
fmt.Println("Total names length: ", totalNamesLength) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment