Skip to content

Instantly share code, notes, and snippets.

@ParthDesai
Last active September 8, 2015 15:51
Show Gist options
  • Save ParthDesai/52cb0e477270600b5383 to your computer and use it in GitHub Desktop.
Save ParthDesai/52cb0e477270600b5383 to your computer and use it in GitHub Desktop.
Functional programming in go
package main
import "fmt"
type predicate func(int) bool
func main() {
r := filter([]int{1, 2, 3, 4}, func(x int) bool {
return x%2 == 0
})
fmt.Println(r)
r = filter([]int{1, 2, 3, 4}, func(x int) bool {
return x%2 != 0
})
fmt.Println(r)
}
func filter(arr []int, f predicate) []int {
newArr := []int{}
for _, value := range arr {
if f(value) {
newArr = append(newArr, value)
}
}
return newArr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment