Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6. sum := Sum(1, 2, 3, 4)
  7. fmt.Println(sum)
  8. }
  9.  
  10. type any interface{}
  11.  
  12. func Map(operation func(p any) any, is ...any) []any {
  13. r := make([]any, len(is))
  14. for index, value := range is {
  15. r[index] = operation(value)
  16. }
  17. return r
  18. }
  19.  
  20. func Reduce(operation func(a any, b any) any, is ...any) any {
  21. var r any
  22. for index, value := range is {
  23. if index == 0 {
  24. r = value
  25. } else {
  26. r = operation(r, value)
  27. }
  28. }
  29. return r
  30. }
  31.  
  32. func Add(a, b any) any {
  33. return (a).(int) + (b).(int)
  34. }
  35.  
  36. func Sum(i ...any) any {
  37. return Reduce(Add, i...)
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement