Решение на Poor Man's Currying от Ангел Александров

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

Към профила на Ангел Александров

Резултати

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

Код

package main
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
result := initial //just for readability
for _, arg := range args {
result = reducer(result, mapper(arg))
}
return result
}
}
func Repeater(s, sep string) func(int) string {
return func(times int) string {
if times < 0 {
return "Warning message: cannot call function with negative number"
}
var result string
if times > 0 {
result = s
}
for i := 0; i < times-1; i++ {
result += sep + s
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
result := initial
initial = gen(initial)
return result
}
}

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

PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s
PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s
PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s
PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s
PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s
PASS
ok  	_/tmp/d20181024-53-a03r5n	0.002s

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

Ангел обнови решението на 19.10.2018 19:09 (преди 10 месеца)

+package main
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ result := initial //just for readability
+ for _, arg := range args {
+ result = reducer(result, mapper(arg))
+ }
+ return result
+ }
+}
+
+func Repeater(s, sep string) func(int) string {
+ return func(times int) string {
+ if times < 0 {
+ return "Warning message: cannot call function with negative number"
+ }
+ var result string
+ if times > 0 {
+ result = s
+ }
+ for i := 0; i < times-1; i++ {
+ result += sep + s
+ }
+ return result
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ result := initial
+ initial = gen(initial)
+ return result
+ }
+}