Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "log"
- )
- func partition(arr []int, predicate func(int) bool) ([]int, []int) {
- fit := make([]int, 0)
- nonfit := make([]int, 0)
- for _, e := range arr {
- if predicate(e) {
- fit = append(fit, e)
- } else {
- nonfit = append(nonfit, e)
- }
- }
- return fit, nonfit
- }
- // eq declared as before.
- func main() {
- arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
- even, odd := partition(arr, func(n int) bool { return n%2 == 0 })
- if !eq(even, []int{2, 4, 6, 8, 10}) {
- log.Fatal("Wrong value")
- }
- if !eq(odd, []int{1, 3, 5, 7, 9}) {
- log.Fatal("Wrong value")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement