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

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

Към профила на Иван Ангелов

Резултати

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

Код

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

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

PASS
ok  	_/tmp/d20181024-53-k96tgy	0.002s
PASS
ok  	_/tmp/d20181024-53-k96tgy	0.002s
PASS
ok  	_/tmp/d20181024-53-k96tgy	0.002s
PASS
ok  	_/tmp/d20181024-53-k96tgy	0.002s
panic: runtime error: integer divide by zero [recovered]
	panic: runtime error: integer divide by zero

goroutine 11 [running]:
testing.tRunner.func1(0xc0000b2600)
	/usr/local/go/src/testing/testing.go:792 +0x387
panic(0x512720, 0x61bfb0)
	/usr/local/go/src/runtime/panic.go:513 +0x1b9
_/tmp/d20181024-53-k96tgy.TestMapReducer.func10(0x0, 0x4, 0xc00000a260)
	/tmp/d20181024-53-k96tgy/solution_test.go:278 +0x40
_/tmp/d20181024-53-k96tgy.MapReducer.func1(0xc000016150, 0x6, 0x6, 0xc00000a260)
	/tmp/d20181024-53-k96tgy/solution.go:27 +0x6b
_/tmp/d20181024-53-k96tgy.TestMapReducer.func11(0xc0000b2600)
	/tmp/d20181024-53-k96tgy/solution_test.go:289 +0x72
testing.tRunner(0xc0000b2600, 0xc00004a5f0)
	/usr/local/go/src/testing/testing.go:827 +0xbf
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:878 +0x353
exit status 2
FAIL	_/tmp/d20181024-53-k96tgy	0.005s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:314: Expected 45 but got 36 after 1st call
    solution_test.go:318: Expected 45 but got 36 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-k96tgy	0.002s

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

Иван обнови решението на 22.10.2018 21:11 (преди 10 месеца)

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

Иван обнови решението на 22.10.2018 21:14 (преди 10 месеца)

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

За нещастие не съм видял на време, че си писал :( Вие получавате email-и за това, че има коментари по решението ви. Но ние не получаваме email-и когато някой е отговорил нещо на тези коментари.