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

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

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

Резултати

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

Код

package main
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
if mapper == nil || reducer == nil {
return nil
}
return func(args ...int) int {
var result int = initial
for _, v := range args {
result = reducer(result, mapper(reducer(initial, v)))
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
if gen == nil {
return nil
}
var begin bool = false
return func() int {
if !begin {
begin = true
return initial
}
initial = gen(initial)
return initial
}
}
func Repeater(s, sep string) func(int) string {
if s == "" || sep == "" {
return nil
}
return func(repetition int) string {
var result string
for i := 0; i < repetition; i++ {
if i == repetition-1 {
result += s
break
}
result += s + sep
}
return result
}
}

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

PASS
ok  	_/tmp/d20181024-53-l8e5f6	0.002s
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4eed67]

goroutine 8 [running]:
testing.tRunner.func1(0xc0000b0300)
	/usr/local/go/src/testing/testing.go:792 +0x387
panic(0x512740, 0x61bfa0)
	/usr/local/go/src/runtime/panic.go:513 +0x1b9
_/tmp/d20181024-53-l8e5f6.TestRepeater.func1(0xc0000b0300)
	/tmp/d20181024-53-l8e5f6/solution_test.go:126 +0x67
testing.tRunner(0xc0000b0300, 0xc00004a4b0)
	/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-l8e5f6	0.004s
PASS
ok  	_/tmp/d20181024-53-l8e5f6	0.002s
PASS
ok  	_/tmp/d20181024-53-l8e5f6	0.002s
--- FAIL: TestMapReducer (0.00s)
    --- FAIL: TestMapReducer/left_shift_all_the_way (0.00s)
        solution_test.go:291: Expected 88080384 but got 0
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-l8e5f6	0.002s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:314: Expected 45 but got 50 after 1st call
    solution_test.go:318: Expected 45 but got 50 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-l8e5f6	0.002s

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

Иво обнови решението на 20.10.2018 16:40 (преди 10 месеца)

+package main
+
+import "fmt"
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+
+ return func(args ...int) int {
+
+ var result int = 0
+
+ for _, v := range args {
+
+ result += mapper(reducer(initial, v))
+ }
+
+ return result
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+
+ var begin bool = false
+
+ return func() int {
+
+ if !begin {
+
+ begin = true
+ return initial
+ }
+
+ initial = gen(initial)
+
+ return initial
+ }
+}
+
+func Repeater(s, sep string) func(int) string {
+
+ return func(repetition int) string {
+
+ var result string
+
+ for i := 0; i < repetition; i++ {
+
+ if i == repetition-1 {
+
+ result += s
+ break
+ }
+
+ result += s + sep
+ }
+
+ return result
+ }
+}
+
+func main() {
+
+ fmt.Printf("%s", Repeater("foobar", ":")(5))
+
+ counter := Generator(func (v int) int {return v + 1}, 0)
+ power := Generator(func (v int) int {return v * v}, 3)
+ fmt.Printf("Values: %d, %d, %d, %d", counter(), counter(), power(), power())
+
+ powerSum := MapReducer(func(v int) int { return v * v }, func(a, v int) int { return a + v }, 0)
+ fmt.Printf("Value is: %d", powerSum(1, 2, 3, 4))
+}

Няма нужда да предаваш main функция -- моля да я изтриеш цялата.

Лесен начин да си тестваш решението е да я сложиш в друг go файл, след което да ползваш go run . за да стартираш програмата си.

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

package main
-import "fmt"
-
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
var result int = 0
for _, v := range args {
result += mapper(reducer(initial, v))
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
var begin bool = false
return func() int {
if !begin {
begin = true
return initial
}
initial = gen(initial)
return initial
}
}
func Repeater(s, sep string) func(int) string {
return func(repetition int) string {
var result string
for i := 0; i < repetition; i++ {
if i == repetition-1 {
result += s
break
}
result += s + sep
}
return result
}
-}
-
+}
-func main() {
-
- fmt.Printf("%s", Repeater("foobar", ":")(5))
-
- counter := Generator(func (v int) int {return v + 1}, 0)
- power := Generator(func (v int) int {return v * v}, 3)
- fmt.Printf("Values: %d, %d, %d, %d", counter(), counter(), power(), power())
-
- powerSum := MapReducer(func(v int) int { return v * v }, func(a, v int) int { return a + v }, 0)
- fmt.Printf("Value is: %d", powerSum(1, 2, 3, 4))
-}

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

package main
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ if mapper == nil || reducer == nil {
+
+ return nil
+ }
+
return func(args ...int) int {
var result int = 0
for _, v := range args {
result += mapper(reducer(initial, v))
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
+ if gen == nil {
+
+ return nil
+ }
+
var begin bool = false
return func() int {
if !begin {
begin = true
return initial
}
initial = gen(initial)
return initial
}
}
func Repeater(s, sep string) func(int) string {
+ if s == "" || sep == "" {
+
+ return nil
+ }
+
return func(repetition int) string {
var result string
for i := 0; i < repetition; i++ {
if i == repetition-1 {
result += s
break
}
result += s + sep
}
return result
}
-}
+}

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

package main
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
if mapper == nil || reducer == nil {
return nil
}
return func(args ...int) int {
var result int = 0
for _, v := range args {
- result += mapper(reducer(initial, v))
+ result = reducer(result, mapper(reducer(initial, v)))
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
if gen == nil {
return nil
}
var begin bool = false
return func() int {
if !begin {
begin = true
return initial
}
initial = gen(initial)
return initial
}
}
func Repeater(s, sep string) func(int) string {
if s == "" || sep == "" {
return nil
}
return func(repetition int) string {
var result string
for i := 0; i < repetition; i++ {
if i == repetition-1 {
result += s
break
}
result += s + sep
}
return result
}
}

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

package main
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
if mapper == nil || reducer == nil {
return nil
}
return func(args ...int) int {
- var result int = 0
+ var result int = initial
for _, v := range args {
result = reducer(result, mapper(reducer(initial, v)))
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
if gen == nil {
return nil
}
var begin bool = false
return func() int {
if !begin {
begin = true
return initial
}
initial = gen(initial)
return initial
}
}
func Repeater(s, sep string) func(int) string {
if s == "" || sep == "" {
return nil
}
return func(repetition int) string {
var result string
for i := 0; i < repetition; i++ {
if i == repetition-1 {
result += s
break
}
result += s + sep
}
return result
}
-}
+}