Решение на Poor Man's Currying от Иван Маргаритов

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

Към профила на Иван Маргаритов

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 4 успешни тест(а)
  • 2 неуспешни тест(а)

Код

package main
func Repeater(s, sep string) func(int) string {
return func(a int) string {
var result string
for i := 0; i < a-1; i++ {
result += s
result += sep
}
result += s
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
var result int = initial
initial = gen(result)
return result
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
var result int = initial
return func(a ...int) int {
for _, num := range a {
result = reducer(result, mapper(num))
}
return result
}
}

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

PASS
ok  	_/tmp/d20181024-53-1kl9e02	0.002s
--- FAIL: TestRepeater (0.00s)
    --- FAIL: TestRepeater/zero_repeats (0.00s)
        solution_test.go:129: Expected `` but got `gopher`
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-1kl9e02	0.002s
PASS
ok  	_/tmp/d20181024-53-1kl9e02	0.002s
PASS
ok  	_/tmp/d20181024-53-1kl9e02	0.002s
PASS
ok  	_/tmp/d20181024-53-1kl9e02	0.002s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:318: Expected 45 but got 81 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-1kl9e02	0.002s

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

Иван обнови решението на 24.10.2018 13:01 (преди 10 месеца)

+package main
+
+func Repeater(s, sep string) func(int) string {
+ return func(a int) string {
+ var result string
+ for i := 0; i < a-1; i++ {
+ result += s
+ result += sep
+ }
+ result += s
+ return result
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ var result int = initial
+ initial = gen(result)
+ return result
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ var result int = initial
+ return func(a ...int) int {
+ for _, num := range a {
+ result = reducer(result, mapper(num))
+ }
+ return result
+ }
+}