Last active
September 8, 2015 15:51
-
-
Save ParthDesai/52cb0e477270600b5383 to your computer and use it in GitHub Desktop.
Functional programming 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" | |
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