Александър обнови решението на 21.10.2018 11:39 (преди 10 месеца)
+package main
+
+import "sync"
+
+// Repeater("foo", ":")(3) -> "foo:foo:foo"
+func Repeater(str, sep string) func(int) string {
+ return func(num int) string {
+ result := ""
+
+ for i := 0; i < num; i++ {
+ result = result + str
+
+ if (i + 1) != num {
+ result = result + sep
+ }
+ }
+
+ return result
+ }
+}
+
+// Generator - int generator. Every time it runs, it returns its current `initial` var
+// and applies the `gen` function on it
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ var previous = initial
+
+ initial = gen(previous)
+
+ return previous
+ }
+}
+
+// MapReducer - A dummy implementation of map reduce
+// The mapping is parallelized
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(list ...int) int {
+ var wg sync.WaitGroup
+
+ for i, v := range list {
+ wg.Add(1)
+
+ go func(localI, localV int) {
+ defer wg.Done()
+
+ list[localI] = mapper(localV)
+ }(i, v)
+ }
+ wg.Wait()
+
+ for _, v := range list {
+ initial = reducer(initial, v)
+ }
+
+ return initial
+ }
+}