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

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

Към профила на Георги Божинов

Резултати

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

Код

package main
import (
"strings"
)
// Repeater accepts a string and a separator and returns a function
// that repeats the string a given amount of times
func Repeater(s, sep string) func(int) string {
return func(i int) string {
if i < 0 {
return ""
}
return strings.TrimSuffix(strings.Repeat(s+sep, i), sep)
}
}
// Generator take an initial value and generates numbers by
// applying the generator function
func Generator(gen func(int) int, initial int) func() int {
res := initial
return func() int {
prev := res
res = gen(res)
return prev
}
}
// MapReducer implements a composition of map and left fold
// on the args passed to the returned func
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
res := initial
return func(args ...int) int {
for _, v := range args {
res = reducer(res, mapper(v))
}
return res
}
}

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

PASS
ok  	_/tmp/d20181024-53-j8w3tq	0.002s
PASS
ok  	_/tmp/d20181024-53-j8w3tq	0.002s
PASS
ok  	_/tmp/d20181024-53-j8w3tq	0.002s
PASS
ok  	_/tmp/d20181024-53-j8w3tq	0.002s
PASS
ok  	_/tmp/d20181024-53-j8w3tq	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-j8w3tq	0.002s

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

Георги обнови решението на 19.10.2018 21:53 (преди 10 месеца)

+package main
+
+import (
+ "strings"
+)
+
+// Repeater accepts a string and a separator and returns a function
+// that repeats the string a given amount of times
+func Repeater(s, sep string) func(int) string {
+ return func(i int) string {
+ if i < 0 {
+ return ""
+ }
+
+ return strings.Trim(strings.Repeat(s+sep, i), sep)
+ }
+}
+
+// Generator take an initial value and generates numbers by
+// applying the generator function
+func Generator(gen func(int) int, initial int) func() int {
+ res := initial
+ return func() int {
+ prev := res
+ res = gen(res)
+ return prev
+ }
+}
+
+// MapReducer implements a composition of map and left fold
+// on the args passed to the returned func
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ res := initial
+ return func(args ...int) int {
+ for _, v := range args {
+ res = reducer(res, mapper(v))
+ }
+
+ return res
+ }
+}

Георги обнови решението на 20.10.2018 14:15 (преди 10 месеца)

package main
import (
"strings"
)
// Repeater accepts a string and a separator and returns a function
// that repeats the string a given amount of times
func Repeater(s, sep string) func(int) string {
return func(i int) string {
if i < 0 {
return ""
}
- return strings.Trim(strings.Repeat(s+sep, i), sep)
+ return strings.TrimSuffix(strings.Repeat(s+sep, i), sep)
}
}
// Generator take an initial value and generates numbers by
// applying the generator function
func Generator(gen func(int) int, initial int) func() int {
res := initial
return func() int {
prev := res
res = gen(res)
return prev
}
}
// MapReducer implements a composition of map and left fold
// on the args passed to the returned func
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
res := initial
return func(args ...int) int {
for _, v := range args {
res = reducer(res, mapper(v))
}
return res
}
}