Skip to content

Instantly share code, notes, and snippets.

@jxub
Created Mar 20, 2017
Embed
What would you like to do?
// 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