Created
March 20, 2017 11:58
-
-
Save jxub/68edcc6f4d466e03353b2fc2bc241b18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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