Skip to content

Instantly share code, notes, and snippets.

@jxub
Created March 20, 2017 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxub/68edcc6f4d466e03353b2fc2bc241b18 to your computer and use it in GitHub Desktop.
Save jxub/68edcc6f4d466e03353b2fc2bc241b18 to your computer and use it in GitHub Desktop.
// functional is a small FP-like library with no dependencies (yet!)
// open-sourcing bits and pieces here on gist :)
package bitbucket.com/jjanarek/x/functional
// Reduce reduces a slice of ints, uints or runes to a single element
// making the pythonic lambda sadly implicit and opinionated
func Reduce(elements []interface{}) interface{} {
switch elements.(type) {
case int:
var result int
for _, num := range elements {
result += num
}
return result
case uint:
var result uint
for _, num := range elements {
result += num
}
return result
case rune:
var result string
for _, char := range elements {
result += char
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment