Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "log"
- )
- func filter(arr []int, predicate func(int) bool) []int {
- out := make([]int, 0)
- for _, e := range arr {
- if predicate(e) {
- out = append(out, e)
- }
- }
- return out
- }
- func eq(m []int, n []int) bool {
- if len(m) != len(n) {
- return false
- }
- for i := 0; i < len(m); i++ {
- if m[i] != n[i] {
- return false
- }
- }
- return true
- }
- func main() {
- arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
- out := filter(arr, func(n int) bool { return n%2 == 0 })
- if !eq(out, []int{2, 4, 6, 8, 10}) {
- log.Fatal("Wrong value")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement