Advertisement
cwchen

[Go] Higher-order function combo.

Nov 15th, 2017
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.43 KB | None | 0 0
  1. package main
  2.  
  3. // filter is declared as above.
  4.  
  5. // apply is declared as above.
  6.  
  7. // reduce is declared as above.
  8.  
  9. func main() {
  10.     in := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  11.  
  12.     temp := filter(in, func(n int) bool { return n%2 != 0 })
  13.     temp = apply(temp, func(n int) int { return n * n })
  14.     out := reduce(temp, func(a int, b int) int { return a + b })
  15.  
  16.     if !(out == (1*1)+(3*3)+(5*5)+(7*7)+(9*9)) {
  17.         log.Fatal("Wrong value")
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement