daily pastebin goal
60%
SHARE
TWEET

[Go] Higher-order function combo.

cwchen Nov 15th, 2017 386 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top