Last active
October 19, 2015 01:39
-
-
Save maiah/0ef4f2cc5e8500ddd916 to your computer and use it in GitHub Desktop.
Something like Reduce, Map, and Collect in Go
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" | |
"strconv" | |
) | |
type Power struct { | |
up int | |
} | |
func main() { | |
s := []int{2, 3, 5, 7, 11, 13} | |
fmt.Println("s ==", s) | |
// Filter | |
cs := func (nums []int) []int { | |
dest := make([]int, 0) | |
for _, v := range nums { | |
if v > 3 && v < 13 { | |
dest = append(dest, v) | |
} | |
} | |
return dest | |
}(s[0:]) | |
// Map | |
ms := func (y []int) []Power { | |
dest := make([]Power, 0) | |
for _, v := range y { | |
dest = append(dest, Power{v}) | |
} | |
return dest | |
}(cs[0:]) | |
// Collect | |
res := func (x []Power) string { | |
powers := "The powers: " | |
pow := 0 | |
for _, v := range x { | |
pow += v.up | |
} | |
return powers + strconv.Itoa(pow) | |
}(ms[0:]) | |
fmt.Println(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment