Решение на Poor Man's Currying от Кристиан Желязков

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

Към профила на Кристиан Желязков

Резултати

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

Код

package main
import (
"strings"
)
func Repeater(s, sep string) func(int) string {
return func(timesForRepeat int) string {
var repeatedString string
if timesForRepeat > 0 {
repeatedString += s
repeatedString += strings.Repeat(sep+s, timesForRepeat-1)
}
return repeatedString
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
generatedNum := initial
initial = gen(initial)
return generatedNum
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
result := initial
for _, currentArg := range args {
mappedArg := mapper(currentArg)
currentResult := reducer(result, mappedArg)
result = currentResult
}
return result
}
}

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

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

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

Кристиан обнови решението на 21.10.2018 10:03 (преди 10 месеца)

+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func Repeater(s, sep string) func(int) string {
+ return func(timesForRepeat int) string {
+ repeatedString := strings.Repeat(s+sep, timesForRepeat-1)
+
+ if timesForRepeat != 0 {
+ repeatedString += s
+ }
+
+ return repeatedString
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ generatedNum := initial
+ initial = gen(initial)
+ return generatedNum
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(args ...int) int {
+ result := initial
+ for _, currentArg := range args {
+ mappedArg := mapper(currentArg)
+ currentResult := reducer(result, mappedArg)
+ result = currentResult
+ }
+
+ return result
+ }
+}
+
+func main() {
+ // Task 1
+ fmt.Println(Repeater("foo", ":")(3))
+
+ //Task 2
+ 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())
+ fmt.Println(power())
+ fmt.Println(power())
+ fmt.Println(counter())
+ fmt.Println(power())
+ fmt.Println(counter())
+ fmt.Println(power())
+
+ //Task 3
+
+ 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))
+}

Кристиан обнови решението на 21.10.2018 10:48 (преди 10 месеца)

package main
import (
- "fmt"
"strings"
)
func Repeater(s, sep string) func(int) string {
return func(timesForRepeat int) string {
repeatedString := strings.Repeat(s+sep, timesForRepeat-1)
if timesForRepeat != 0 {
repeatedString += s
}
return repeatedString
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
generatedNum := initial
initial = gen(initial)
+
return generatedNum
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
result := initial
for _, currentArg := range args {
mappedArg := mapper(currentArg)
currentResult := reducer(result, mappedArg)
result = currentResult
}
return result
}
}
-
-func main() {
- // Task 1
- fmt.Println(Repeater("foo", ":")(3))
-
- //Task 2
- 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())
- fmt.Println(power())
- fmt.Println(power())
- fmt.Println(counter())
- fmt.Println(power())
- fmt.Println(counter())
- fmt.Println(power())
-
- //Task 3
-
- 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))
-}

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

package main
import (
"strings"
)
func Repeater(s, sep string) func(int) string {
return func(timesForRepeat int) string {
- repeatedString := strings.Repeat(s+sep, timesForRepeat-1)
+ var repeatedString string
- if timesForRepeat != 0 {
+ if timesForRepeat > 0 {
repeatedString += s
+ repeatedString += strings.Repeat(sep+s, timesForRepeat-1)
}
return repeatedString
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
generatedNum := initial
initial = gen(initial)
return generatedNum
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(args ...int) int {
result := initial
for _, currentArg := range args {
mappedArg := mapper(currentArg)
currentResult := reducer(result, mappedArg)
result = currentResult
}
return result
}
-}
+}