Ангел обнови решението на 19.10.2018 19:09 (преди 10 месеца)
+package main
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ result := initial //just for readability
+ for _, arg := range args {
+ result = reducer(result, mapper(arg))
+ }
+ return result
+ }
+}
+
+func Repeater(s, sep string) func(int) string {
+ return func(times int) string {
+ if times < 0 {
+ return "Warning message: cannot call function with negative number"
+ }
+ var result string
+ if times > 0 {
+ result = s
+ }
+ for i := 0; i < times-1; i++ {
+ result += sep + s
+ }
+ return result
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ result := initial
+ initial = gen(initial)
+ return result
+ }
+}