Добромир обнови решението на 23.10.2018 23:54 (преди 10 месеца)
+package main
+
+import (
+ "strings"
+)
+
+func Repeater(s, sep string) func(int) string {
+ return func(repeats int) string {
+ if repeats <= 0 {
+ return ""
+ }
+
+ var sb strings.Builder
+ sb.Grow((repeats * len(s)) + ((repeats - 1) * len(sep)))
+ sb.WriteString(s)
+ for i := 0; i < repeats-1; i++ {
+ sb.WriteString(sep + s)
+ }
+ return sb.String()
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ prev := initial
+ initial = gen(initial)
+ return prev
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ result := initial
+ for _, arg := range args {
+ result = reducer(result, mapper(arg))
+ }
+ return result
+ }
+}