Решение на Poor Man's Currying от Николай Лазаров

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

Към профила на Николай Лазаров

Резултати

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

Код

package main
import (
"strings"
)
func Repeater(s, sep string) func(int) string {
return func(times int) string {
var buffer strings.Builder
for i := 0; i < times; i++ {
if i > 0 {
buffer.WriteString(sep)
}
buffer.WriteString(s)
}
return buffer.String()
}
}
func Generator(gen func(int) int, initial int) func() int {
accumulator := initial
return func() int {
result := accumulator
accumulator = gen(accumulator)
return result
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
accumulator := initial
for _, num := range args {
accumulator = reducer(accumulator, mapper(num))
}
return accumulator
}
}

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

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

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

Николай обнови решението на 17.10.2018 23:44 (преди 10 месеца)

+package main
+
+import (
+ "strings"
+)
+
+func Repeater(s, sep string) func(int) string {
+ return func(times int) string {
+ var buffer strings.Builder
+ for i := 0; i < times; i++ {
+ if i > 0 {
+ buffer.WriteString(sep)
+ }
+
+ buffer.WriteString(s)
+ }
+ return buffer.String()
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ accumulator := initial
+ return func() int {
+ result := accumulator
+ accumulator = gen(accumulator)
+ return result
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ accumulator := initial
+ for _, num := range args {
+ accumulator = reducer(accumulator, mapper(num))
+ }
+ return accumulator
+ }
+}