Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "log"
- )
- func reduce(arr []int, reducer func(int, int) int) int {
- if len(arr) == 0 {
- return 0
- } else if len(arr) == 1 {
- return arr[0]
- }
- n := arr[0]
- for i := 1; i < len(arr); i++ {
- n = reducer(n, arr[i])
- }
- return n
- }
- func main() {
- arr := []int{1, 2, 3, 4, 5}
- n := reduce(arr, func(a int, b int) int { return a + b })
- if !(n == 15) {
- log.Fatal("Wrong value")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement