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

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

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

Резултати

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

Код

// You can edit this code!
// Click here and start typing.
package main
import "fmt"
func Repeater(s, sep string) func(int) string {
return func(times int) string {
result := ""
for i := 0; i < times; i++ {
result += s
if i < times-1 {
result += sep
}
}
return result
}
}
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(nums ...int) int {
for i := 0; i < len(nums); i++ {
nums[i] = mapper(nums[i])
}
result := reducer(initial, nums[0])
for i := 1; i < len(nums); i++ {
result = reducer(result, nums[i])
}
return result
}
}
func main() {
//1
fmt.Println(Repeater("foo", ":")(3))
//2
counter := Generator(
func(v int) int { return v + 1 },
0,
)
power := Generator(
func(v int) int { return v * v },
2,
)
counter()
counter()
counter()
power()
power()
power()
fmt.Println(counter())
fmt.Println(power())
/////3
powerSum := MapReducer(
func(v int) int { return v * v },
func(a, v int) int { return a + v },
0,
)
fmt.Println(powerSum(1, 2, 3, 4)) // 30
}

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

PASS
ok  	_/tmp/d20181024-53-9ddg2q	0.002s
PASS
ok  	_/tmp/d20181024-53-9ddg2q	0.002s
PASS
ok  	_/tmp/d20181024-53-9ddg2q	0.002s
PASS
ok  	_/tmp/d20181024-53-9ddg2q	0.002s
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 10 [running]:
testing.tRunner.func1(0xc0000b2500)
	/usr/local/go/src/testing/testing.go:792 +0x387
panic(0x512720, 0x61bfe0)
	/usr/local/go/src/runtime/panic.go:513 +0x1b9
_/tmp/d20181024-53-9ddg2q.MapReducer.func1(0x63fbb0, 0x0, 0x0, 0xc00000a1e0)
	/tmp/d20181024-53-9ddg2q/solution.go:38 +0x101
_/tmp/d20181024-53-9ddg2q.TestMapReducer.func11(0xc0000b2500)
	/tmp/d20181024-53-9ddg2q/solution_test.go:289 +0x72
testing.tRunner(0xc0000b2500, 0xc00004a540)
	/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-9ddg2q	0.004s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:318: Expected 45 but got 22 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-9ddg2q	0.002s

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

Мартин обнови решението на 19.10.2018 18:02 (преди 10 месеца)

+// You can edit this code!
+// Click here and start typing.
+package main
+
+import "fmt"
+
+func Repeater(s, sep string) func(int) string {
+ return func(times int) string {
+ result := ""
+
+ for i := 0; i < times; i++ {
+ result += s
+ if i < times-1 {
+ result += sep
+ }
+ }
+ return result
+
+ }
+}
+
+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(nums ...int) int {
+
+ for i := 0; i < len(nums); i++ {
+ nums[i] = mapper(nums[i])
+ }
+
+ result := reducer(initial, nums[0])
+
+ for i := 1; i < len(nums); i++ {
+ result = reducer(result, nums[i])
+ }
+
+ return result
+ }
+
+}
+
+func main() {
+ //1
+ fmt.Println(Repeater("foo", ":")(3))
+
+ //2
+ counter := Generator(
+ func(v int) int { return v + 1 },
+ 0,
+ )
+
+ power := Generator(
+ func(v int) int { return v * v },
+ 2,
+ )
+
+ counter()
+ counter()
+ counter()
+ power()
+ power()
+ power()
+
+ fmt.Println(counter())
+ fmt.Println(power())
+
+ /////3
+
+ powerSum := MapReducer(
+ func(v int) int { return v * v },
+ func(a, v int) int { return a + v },
+ 0,
+ )
+
+ fmt.Println(powerSum(1, 2, 3, 4)) // 30
+}

Нямах конкретен подход за поправка в предвид. Просто мисля ще е полезно да се направи по две причини. Кода ще стане по - коректен тъй като в момента не е верен и това двойно споменаване е свързано с причината. И ще стане по - ясен за четене.