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

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

Към профила на Георги Стефанов

Резултати

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

Код

package main
func Repeater(s, sep string) func(int) string {
const EmptyString = ""
return func(times int) string {
if times <= 0 {
return EmptyString
}
result := EmptyString
for i := 0; i < times-1; i++ {
result += s + sep
}
result += s
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
current := initial
firstInvocation := true
return func() int {
if firstInvocation {
firstInvocation = false
return current
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(ints ...int) int {
result := initial
for _, int := range ints {
result = reducer(result, mapper(int))
}
return result
}
}

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

PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s
PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s
PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s
PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s
PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s
PASS
ok  	_/tmp/d20181024-53-1cup70l	0.002s

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

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

+package main
+
+import "strings"
+
+func Repeater(s, sep string) func(int) string {
+ const EmptyString = ""
+
+ return func(times int) string {
+ if times < 0 {
+ return EmptyString
+ }
+
+ wordRepetitions := make([]string, times)
+
+ for i := 0; i < times; i++ {
+ wordRepetitions[i] = s
+ }
+
+ return strings.Join(wordRepetitions, sep)
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ current := initial
+ firstInvocation := true
+
+ return func() int {
+ if firstInvocation {
+ firstInvocation = false
+ return current
+ }
+
+ current = gen(current)
+ return current
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(ints ...int) int {
+ result := initial
+
+ for _, int := range ints {
+ result = reducer(result, mapper(int))
+ }
+
+ return result
+ }
+
+}

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

package main
import "strings"
func Repeater(s, sep string) func(int) string {
const EmptyString = ""
return func(times int) string {
if times < 0 {
return EmptyString
}
wordRepetitions := make([]string, times)
for i := 0; i < times; i++ {
wordRepetitions[i] = s
}
return strings.Join(wordRepetitions, sep)
}
}
func Generator(gen func(int) int, initial int) func() int {
current := initial
firstInvocation := true
return func() int {
if firstInvocation {
firstInvocation = false
return current
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(ints ...int) int {
result := initial
for _, int := range ints {
result = reducer(result, mapper(int))
}
return result
}
}
+

Георги обнови решението на 22.10.2018 18:39 (преди 10 месеца)

package main
-import "strings"
-
func Repeater(s, sep string) func(int) string {
const EmptyString = ""
return func(times int) string {
- if times < 0 {
+ if times <= 0 {
return EmptyString
}
- wordRepetitions := make([]string, times)
+ result := ""
- for i := 0; i < times; i++ {
- wordRepetitions[i] = s
+ for i := 0; i < times-1; i++ {
+ result += s + sep
}
- return strings.Join(wordRepetitions, sep)
+ result += s
+
+ return result
}
}
func Generator(gen func(int) int, initial int) func() int {
current := initial
firstInvocation := true
return func() int {
if firstInvocation {
firstInvocation = false
return current
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(ints ...int) int {
result := initial
for _, int := range ints {
result = reducer(result, mapper(int))
}
return result
}
}
-

Георги обнови решението на 22.10.2018 18:39 (преди 10 месеца)

package main
func Repeater(s, sep string) func(int) string {
const EmptyString = ""
return func(times int) string {
if times <= 0 {
return EmptyString
}
result := ""
for i := 0; i < times-1; i++ {
result += s + sep
}
result += s
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
current := initial
firstInvocation := true
return func() int {
if firstInvocation {
firstInvocation = false
return current
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(ints ...int) int {
result := initial
for _, int := range ints {
result = reducer(result, mapper(int))
}
return result
}
}
+

Георги обнови решението на 22.10.2018 18:43 (преди 10 месеца)

package main
func Repeater(s, sep string) func(int) string {
const EmptyString = ""
return func(times int) string {
if times <= 0 {
return EmptyString
}
- result := ""
+ result := EmptyString
for i := 0; i < times-1; i++ {
result += s + sep
}
result += s
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
current := initial
firstInvocation := true
return func() int {
if firstInvocation {
firstInvocation = false
return current
}
current = gen(current)
return current
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(ints ...int) int {
result := initial
for _, int := range ints {
result = reducer(result, mapper(int))
}
return result
}
}