Skip to content

Instantly share code, notes, and snippets.

@EricR
Created April 26, 2013 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricR/5468763 to your computer and use it in GitHub Desktop.
Save EricR/5468763 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
nums := []int{1, 3, 4, 8, 9, 12, 15, 20, 25, 31}
result := filterNumbers(nums, func(x int) bool {
return x%2 == 0
})
fmt.Println(result)
}
func filterNumbers(s []int, fn func(int) bool) []int {
result, n := make([]int, cap(s)), 0
for i := 0; i < cap(s); i++ {
if fn(s[i]) {
result[n] = s[i]
n += 1
}
}
return result[0:n] // => [4 8 12 20 0 0 0 0 0 0] => [4 8 12 20]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment