Решение на Poor Man's Currying от Ивайло Кирязов

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

Към профила на Ивайло Кирязов

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 5 успешни тест(а)
  • 1 неуспешни тест(а)

Код

package main
func Repeater(s, sep string) func (int) string {
return func (nRepeats int) string {
result := ""
word := sep + s
if nRepeats == 1 {
return result
} else {
result = s
}
for i := 1; i < nRepeats; i++ {
result += word
}
return result
}
}
func Generator(gen func (int) int, initial int) func() int {
result := initial
var firstTime bool = true
return func() int {
if firstTime {
firstTime = false
return initial
}
result = gen(result)
return result
}
}
func MapReducer(mapper func (int) int, reducer func (int, int) int, initial int) func (...int) int {
return func (args ...int) int {
result := initial
for _, v := range args {
v = mapper(v)
result = reducer(result, v)
}
return result
}
}

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

PASS
ok  	_/tmp/d20181024-53-iycwzm	0.002s
--- FAIL: TestRepeater (0.00s)
    --- FAIL: TestRepeater/zero_repeats (0.00s)
        solution_test.go:129: Expected `` but got `gopher`
    --- FAIL: TestRepeater/one_repeats (0.00s)
        solution_test.go:129: Expected `gopher` but got ``
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-iycwzm	0.002s
PASS
ok  	_/tmp/d20181024-53-iycwzm	0.002s
PASS
ok  	_/tmp/d20181024-53-iycwzm	0.002s
PASS
ok  	_/tmp/d20181024-53-iycwzm	0.002s
PASS
ok  	_/tmp/d20181024-53-iycwzm	0.002s

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

Ивайло обнови решението на 24.10.2018 02:04 (преди 10 месеца)

+package main
+
+func Repeater(s, sep string) func (int) string {
+ return func (nRepeats int) string {
+ result := ""
+ word := sep + s
+ if nRepeats == 1 {
+ return result
+ } else {
+ result = s
+ }
+ for i := 1; i < nRepeats; i++ {
+ result += word
+ }
+ return result
+ }
+}
+
+func Generator(gen func (int) int, initial int) func() int {
+ result := initial
+ var firstTime bool = true
+ return func() int {
+ if firstTime {
+ firstTime = false
+ return initial
+ }
+ result = gen(result)
+ return result
+ }
+}
+
+func MapReducer(mapper func (int) int, reducer func (int, int) int, initial int) func (...int) int {
+ return func (args ...int) int {
+ result := initial
+ for _, v := range args {
+ v = mapper(v)
+ result = reducer(result, v)
+ }
+ return result
+ }
+}