Решение на Poor Man's Currying от Мирослав Лалев

Обратно към всички решения

Към профила на Мирослав Лалев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 6 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
import (
"strings"
)
func Repeater(s, sep string) func(int) string {
return func(n int) string {
str := make([]string, 0, n)
for i := 0; i < n; i++ {
str = append(str, s)
}
return strings.Join(str, sep)
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
old := initial
initial = gen(initial)
return old
}
}
func MapReducer(
mapper func(int) int,
reducer func(int, int) int,
initial int) func(...int) int {
return func(numbers ...int) int {
res := initial
for _, num := range numbers {
res = reducer(res, mapper(num))
}
return res
}
}

Лог от изпълнението

PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s
PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s
PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s
PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s
PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s
PASS
ok  	_/tmp/d20181024-53-w5881f	0.002s

История (2 версии и 6 коментара)

Мирослав обнови решението на 18.10.2018 20:02 (преди 10 месеца)

+package main
+
+import (
+ "strings"
+)
+
+func main() {
+}
+
+func Repeater(s, sep string) func(int) string {
+ return func(n int) string {
+ str := make([]string, 0, n)
+ for i := 0; i < n; i++ {
+ str = append(str, s)
+ }
+ return strings.Join(str, sep)
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ initial = gen(initial)
+ return initial
+ }
+}
+
+func MapReducer(
+ mapper func(int) int,
+ reducer func(int, int) int,
+ initial int) func(...int) int {
+
+ return func(numbers ...int) int {
+ res := initial
+ for _, num := range numbers {
+ res = reducer(res, mapper(num))
+ }
+ return res
+ }
+}

Мирослав обнови решението на 23.10.2018 21:46 (преди 10 месеца)

package main
import (
"strings"
)
-func main() {
-}
-
func Repeater(s, sep string) func(int) string {
return func(n int) string {
str := make([]string, 0, n)
for i := 0; i < n; i++ {
str = append(str, s)
}
return strings.Join(str, sep)
}
}
-
func Generator(gen func(int) int, initial int) func() int {
return func() int {
+ old := initial
initial = gen(initial)
- return initial
+ return old
}
}
-
func MapReducer(
mapper func(int) int,
reducer func(int, int) int,
initial int) func(...int) int {
-
return func(numbers ...int) int {
res := initial
for _, num := range numbers {
res = reducer(res, mapper(num))
}
return res
}
}