-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package day188 | ||
|
||
// MakeFunctionsOriginal is the problem version. | ||
func MakeFunctionsOriginal() []func() int { | ||
var funcList []func() int | ||
for _, i := range []int{1, 2, 3} { | ||
funcList = append(funcList, func() int { | ||
return i | ||
}) | ||
} | ||
return funcList | ||
} | ||
|
||
// MakeFunctionsCorrect is the corrected version. | ||
func MakeFunctionsCorrect() []func() int { | ||
var funcList []func() int | ||
for _, i := range []int{1, 2, 3} { | ||
wrapped := func(i int) func() int { | ||
return func() int { | ||
return i | ||
} | ||
} | ||
funcList = append(funcList, wrapped(i)) | ||
} | ||
return funcList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def make_functions(): | ||
flist = [] | ||
|
||
for i in [1, 2, 3]: | ||
def print_i(): | ||
print(i) | ||
flist.append(print_i) | ||
|
||
return flist | ||
|
||
functions = make_functions() | ||
for f in functions: | ||
f() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def make_functions(): | ||
flist = [] | ||
|
||
for i in [1, 2, 3]: | ||
def build_print_i(i): | ||
def print_i(): | ||
print(i) | ||
return print_i | ||
flist.append(build_print_i(i)) | ||
|
||
return flist | ||
|
||
functions = make_functions() | ||
for f in functions: | ||
f() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package day188 | ||
|
||
import "testing" | ||
|
||
func TestMakeFunctionsOriginal(t *testing.T) { | ||
t.Parallel() | ||
funcs := MakeFunctionsOriginal() | ||
for _, f := range funcs { | ||
if result := f(); result != 3 { | ||
t.Errorf("Expected every response to be 3. Got %v", result) | ||
} | ||
} | ||
} | ||
|
||
func TestMakeFunctionsCorrect(t *testing.T) { | ||
t.Parallel() | ||
funcs := MakeFunctionsCorrect() | ||
for i, f := range funcs { | ||
if result := f(); result != i+1 { | ||
t.Errorf("Expected %v, got %v", i+1, result) | ||
} | ||
} | ||
} |