Траян обнови решението на 21.10.2018 13:00 (преди 10 месеца)
+package main
+
+func Repeater(s, sep string) func(int) string {
+ return func(num int) string {
+ if num <= 0 {
+ return ""
+ } else {
+ result := s
+ for cnt := 0; cnt < num-1; cnt++ {
+ result = result + sep + s
+ }
+ return result
+ }
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ defer func() {
+ initial = gen(initial)
+ }()
+ return initial
+ }
+}
+
+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 {
+ arg = mapper(arg)
+ result = reducer(result, arg)
+ }
+ return result
+ }
+}
Много ми харесва как е използван defer
в Generator
:)