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

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

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

Резултати

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

Код

package main
import "sync"
// Repeater returns a function concatenating
// the first argument n times using the second argument as a separator
// example: Repeater("foo", ":")(3) // "foo:foo:foo"
func Repeater(str, sep string) func(int) string {
return func(num int) string {
result := ""
for i := 0; i < num; i++ {
result = result + str
if (i + 1) != num {
result = result + sep
}
}
return result
}
}
// Generator - int generator. Every time it runs, it returns its current
// `initial` var and applies the `gen` function on it
func Generator(gen func(int) int, initial int) func() int {
return func() int {
var previous = initial
initial = gen(previous)
return previous
}
}
// MapReducer - A dummy implementation of map reduce
// The mapping is parallelized
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(list ...int) int {
var wg sync.WaitGroup
current := initial
for i, v := range list {
wg.Add(1)
go func(localI, localV int) {
defer wg.Done()
list[localI] = mapper(localV)
}(i, v)
}
wg.Wait()
// unfortunately, we have to do the reducing sequentially, because we
// don't know if the reducing operation is commutative and associative
for _, v := range list {
current = reducer(current, v)
}
return current
}
}

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

PASS
ok  	_/tmp/d20181024-53-h933ie	0.002s
PASS
ok  	_/tmp/d20181024-53-h933ie	0.002s
PASS
ok  	_/tmp/d20181024-53-h933ie	0.002s
PASS
ok  	_/tmp/d20181024-53-h933ie	0.002s
PASS
ok  	_/tmp/d20181024-53-h933ie	0.002s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:318: Expected 45 but got 22 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-h933ie	0.003s

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

Александър обнови решението на 21.10.2018 11:39 (преди 10 месеца)

+package main
+
+import "sync"
+
+// Repeater("foo", ":")(3) -> "foo:foo:foo"
+func Repeater(str, sep string) func(int) string {
+ return func(num int) string {
+ result := ""
+
+ for i := 0; i < num; i++ {
+ result = result + str
+
+ if (i + 1) != num {
+ result = result + sep
+ }
+ }
+
+ return result
+ }
+}
+
+// Generator - int generator. Every time it runs, it returns its current `initial` var
+// and applies the `gen` function on it
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ var previous = initial
+
+ initial = gen(previous)
+
+ return previous
+ }
+}
+
+// MapReducer - A dummy implementation of map reduce
+// The mapping is parallelized
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(list ...int) int {
+ var wg sync.WaitGroup
+
+ for i, v := range list {
+ wg.Add(1)
+
+ go func(localI, localV int) {
+ defer wg.Done()
+
+ list[localI] = mapper(localV)
+ }(i, v)
+ }
+ wg.Wait()
+
+ for _, v := range list {
+ initial = reducer(initial, v)
+ }
+
+ return initial
+ }
+}

Александър обнови решението на 21.10.2018 11:56 (преди 10 месеца)

package main
import "sync"
-// Repeater("foo", ":")(3) -> "foo:foo:foo"
+// Repeater returns a function concatenating
+// the first argument n times using the second argument as a separator
+// example: Repeater("foo", ":")(3) // "foo:foo:foo"
func Repeater(str, sep string) func(int) string {
return func(num int) string {
result := ""
for i := 0; i < num; i++ {
result = result + str
if (i + 1) != num {
result = result + sep
}
}
return result
}
}
-// Generator - int generator. Every time it runs, it returns its current `initial` var
-// and applies the `gen` function on it
+// Generator - int generator. Every time it runs, it returns its current
+// `initial` var and applies the `gen` function on it
func Generator(gen func(int) int, initial int) func() int {
return func() int {
var previous = initial
initial = gen(previous)
return previous
}
}
// MapReducer - A dummy implementation of map reduce
// The mapping is parallelized
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(list ...int) int {
var wg sync.WaitGroup
for i, v := range list {
wg.Add(1)
go func(localI, localV int) {
defer wg.Done()
list[localI] = mapper(localV)
}(i, v)
}
wg.Wait()
+ // unfortunately, we have to do the reducing sequentially, because we
+ // don't know if the reducing operation is commutative and associative
for _, v := range list {
initial = reducer(initial, v)
}
return initial
}
}

Александър обнови решението на 24.10.2018 10:17 (преди 10 месеца)

package main
import "sync"
// Repeater returns a function concatenating
// the first argument n times using the second argument as a separator
// example: Repeater("foo", ":")(3) // "foo:foo:foo"
func Repeater(str, sep string) func(int) string {
return func(num int) string {
result := ""
for i := 0; i < num; i++ {
result = result + str
if (i + 1) != num {
result = result + sep
}
}
return result
}
}
// Generator - int generator. Every time it runs, it returns its current
// `initial` var and applies the `gen` function on it
func Generator(gen func(int) int, initial int) func() int {
return func() int {
var previous = initial
initial = gen(previous)
return previous
}
}
// MapReducer - A dummy implementation of map reduce
// The mapping is parallelized
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(list ...int) int {
var wg sync.WaitGroup
+ current := initial
for i, v := range list {
wg.Add(1)
go func(localI, localV int) {
defer wg.Done()
list[localI] = mapper(localV)
}(i, v)
}
wg.Wait()
// unfortunately, we have to do the reducing sequentially, because we
// don't know if the reducing operation is commutative and associative
for _, v := range list {
- initial = reducer(initial, v)
+ current = reducer(current, v)
}
- return initial
+ return current
}
}