Решение на Poor Man's Currying от Добромир Иванов

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

Към профила на Добромир Иванов

Резултати

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

Код

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
}
}

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

PASS
ok  	_/tmp/d20181024-53-18y7942	0.003s
PASS
ok  	_/tmp/d20181024-53-18y7942	0.002s
PASS
ok  	_/tmp/d20181024-53-18y7942	0.002s
PASS
ok  	_/tmp/d20181024-53-18y7942	0.002s
PASS
ok  	_/tmp/d20181024-53-18y7942	0.002s
PASS
ok  	_/tmp/d20181024-53-18y7942	0.002s

История (1 версия и 0 коментара)

Добромир обнови решението на 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
+ }
+}