Skip to content

Instantly share code, notes, and snippets.

@derrandz
Created September 16, 2017 19:21
Show Gist options
  • Save derrandz/9753e3a687551944aca03a9e1cdd51ef to your computer and use it in GitHub Desktop.
Save derrandz/9753e3a687551944aca03a9e1cdd51ef to your computer and use it in GitHub Desktop.
Messing around with go and js style reducers
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