Решение на Poor Man's Currying от Даяна Веселинова

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

Към профила на Даяна Веселинова

Резултати

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

Код

package main
import "strings"
func Repeater(s, sep string) func(int) string {
var result []string
return func(m int) string {
for i := 0; i < m; i++ {
result = append(result, s)
}
return strings.Join(result, sep)
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
defer func() { initial = gen(initial) }()
return initial
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
for _, v := range args {
initial = reducer(initial, mapper(v))
}
return initial
}
}

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

PASS
ok  	_/tmp/d20181024-53-18np311	0.002s
PASS
ok  	_/tmp/d20181024-53-18np311	0.003s
--- FAIL: TestRepeaterMultipleCalls (0.00s)
    solution_test.go:147: Expected `gopher,gopher,gopher` but got `gopher,gopher,gopher,gopher,gopher,gopher` after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-18np311	0.002s
PASS
ok  	_/tmp/d20181024-53-18np311	0.002s
PASS
ok  	_/tmp/d20181024-53-18np311	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-18np311	0.002s

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

Даяна обнови решението на 21.10.2018 16:08 (преди 10 месеца)

+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func main() {
+ fmt.Println(Repeater("foo", ":")(3))
+
+ counter := Generator(func(v int) int { return v + 1 }, 0)
+
+ power := Generator(func(v int) int { return v * v }, 2)
+
+ fmt.Println(counter())
+ fmt.Println(counter()) // 1
+ fmt.Println(power()) // 2
+ fmt.Println(power()) // 4
+ fmt.Println(counter()) // 2
+ fmt.Println(power()) // 16
+ fmt.Println(counter()) // 3
+ fmt.Println(power()) // 256
+
+ 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
+}
+
+func Repeater(s, sep string) func(int) string {
+
+ var result []string
+ return func(m int) string {
+ for i := 0; i < m; i++ {
+ result = append(result, s)
+
+ }
+ return (strings.Join(result, sep))
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+
+ return func() int {
+ defer func() { initial = gen(initial) }()
+
+ return initial
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+
+ return func(args ...int) int {
+ for _, v := range args {
+
+ initial = reducer(initial, mapper(v))
+
+ }
+ return initial
+
+ }
+}

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

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

Даяна обнови решението на 22.10.2018 22:52 (преди 10 месеца)

package main
-import (
- "fmt"
- "strings"
-)
+import "strings"
-func main() {
- fmt.Println(Repeater("foo", ":")(3))
-
- counter := Generator(func(v int) int { return v + 1 }, 0)
-
- power := Generator(func(v int) int { return v * v }, 2)
-
- fmt.Println(counter())
- fmt.Println(counter()) // 1
- fmt.Println(power()) // 2
- fmt.Println(power()) // 4
- fmt.Println(counter()) // 2
- fmt.Println(power()) // 16
- fmt.Println(counter()) // 3
- fmt.Println(power()) // 256
-
- 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
-}
-
func Repeater(s, sep string) func(int) string {
var result []string
return func(m int) string {
for i := 0; i < m; i++ {
result = append(result, s)
}
return (strings.Join(result, sep))
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
defer func() { initial = gen(initial) }()
return initial
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
for _, v := range args {
initial = reducer(initial, mapper(v))
}
return initial
}
-}
+}

Даяна обнови решението на 24.10.2018 09:47 (преди 10 месеца)

package main
import "strings"
func Repeater(s, sep string) func(int) string {
var result []string
return func(m int) string {
for i := 0; i < m; i++ {
result = append(result, s)
}
- return (strings.Join(result, sep))
+ return strings.Join(result, sep)
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
defer func() { initial = gen(initial) }()
return initial
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
for _, v := range args {
initial = reducer(initial, mapper(v))
}
return initial
}
}