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

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

Към профила на Антони Добренов

Резултати

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

Код

package main
func Repeater(s, sep string) func(int) string {
return func(cnt int) string {
finalString := ""
for i := 0; i < cnt; i++ {
finalString += s
if i < cnt-1 {
finalString += sep
}
}
return finalString
}
}
func Generator(gen func(int) int, initial int) func() int {
acc := initial
return func() int {
result := acc
acc = gen(acc)
return result
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
reducedElement := initial
for _, arg := range args {
mappedElement := mapper(arg)
reducedElement = reducer(reducedElement, mappedElement)
}
return reducedElement
}
}

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

PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s
PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s
PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s
PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s
PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s
PASS
ok  	_/tmp/d20181024-53-1yvye4w	0.002s

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

Антони обнови решението на 23.10.2018 01:24 (преди 10 месеца)

+package main
+
+func Repeater(s, sep string) func(int) string {
+ return func(cnt int) string {
+ finalString := ""
+ for i := 0; i < cnt; i++ {
+ finalString += s
+ if i < cnt-1 {
+ finalString += sep
+ }
+ }
+ return finalString
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ result := initial
+ initial = gen(initial)
+ return result
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ for _, arg := range args {
+ mappedElement := mapper(arg)
+ initial = reducer(initial, mappedElement)
+ }
+ return initial
+ }
+}

Антони обнови решението на 23.10.2018 23:08 (преди 10 месеца)

package main
func Repeater(s, sep string) func(int) string {
return func(cnt int) string {
finalString := ""
for i := 0; i < cnt; i++ {
finalString += s
if i < cnt-1 {
finalString += sep
}
}
return finalString
}
}
func Generator(gen func(int) int, initial int) func() int {
+ acc := initial
return func() int {
- result := initial
- initial = gen(initial)
+ result := acc
+ acc = gen(acc)
return result
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
+ reducedElement := initial
for _, arg := range args {
mappedElement := mapper(arg)
- initial = reducer(initial, mappedElement)
+ reducedElement = reducer(reducedElement, mappedElement)
}
- return initial
+ return reducedElement
}
}