Решение на Poor Man's Currying от Исмаил Алиджиков

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

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

Резултати

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

Код

package main
import "strings"
func Repeater(word, separator string) func(int) string {
return func(times int) string {
var words []string
for i := 0; i < times; i++ {
words = append(words, word)
}
return strings.Join(words, separator)
}
}
func Generator(gen func(int) int, initial int) func() int {
isInitialCall := true
current := initial
return func() int {
if isInitialCall {
isInitialCall = false
return initial
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(nums ...int) int {
mapped := make([]int, len(nums))
for i := 0; i < len(nums); i++ {
mapped[i] = mapper(nums[i])
}
reduced := initial
for i := 0; i < len(nums); i++ {
reduced = reducer(reduced, mapped[i])
}
return reduced
}
}

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

PASS
ok  	_/tmp/d20181024-53-11m41v5	0.002s
PASS
ok  	_/tmp/d20181024-53-11m41v5	0.002s
PASS
ok  	_/tmp/d20181024-53-11m41v5	0.002s
PASS
ok  	_/tmp/d20181024-53-11m41v5	0.003s
PASS
ok  	_/tmp/d20181024-53-11m41v5	0.003s
PASS
ok  	_/tmp/d20181024-53-11m41v5	0.002s

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

Исмаил обнови решението на 21.10.2018 01:12 (преди 10 месеца)

+package main
+
+import "strings"
+
+func Repeater(word, separator string) func(int) string {
+ return func(times int) string {
+ var words []string
+ for i := 0; i < times; i++ {
+ words = append(words, word)
+ }
+
+ return strings.Join(words, separator)
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ current := initial
+
+ return func() int {
+ if is_initial_call {
+ is_initial_call = false
+ return initial
+ }
+
+ current = gen(current)
+ return current
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(nums ...int) int {
+ mapped := make([]int, len(nums))
+ for i := 0; i < len(nums); i++ {
+ mapped[i] = mapper(nums[i])
+ }
+
+ reduced := initial
+ for i := 0; i < len(nums); i++ {
+ reduced = reducer(reduced, mapped[i])
+ }
+
+ return reduced
+ }
+}

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

package main
import "strings"
func Repeater(word, separator string) func(int) string {
return func(times int) string {
var words []string
for i := 0; i < times; i++ {
words = append(words, word)
}
return strings.Join(words, separator)
}
}
func Generator(gen func(int) int, initial int) func() int {
- is_initial_call := true
+ isInitialCall := true
current := initial
return func() int {
- if is_initial_call {
- is_initial_call = false
+ if isInitialCall {
+ isInitialCall = false
return initial
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(nums ...int) int {
mapped := make([]int, len(nums))
for i := 0; i < len(nums); i++ {
mapped[i] = mapper(nums[i])
}
reduced := initial
for i := 0; i < len(nums); i++ {
reduced = reducer(reduced, mapped[i])
}
return reduced
}
}