Skip to content

Instantly share code, notes, and snippets.

@maiah
Last active October 19, 2015 01:39
Show Gist options
  • Save maiah/0ef4f2cc5e8500ddd916 to your computer and use it in GitHub Desktop.
Save maiah/0ef4f2cc5e8500ddd916 to your computer and use it in GitHub Desktop.
Something like Reduce, Map, and Collect in Go
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