-
Notifications
You must be signed in to change notification settings - Fork 384
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
1 parent
30386a8
commit c6411d6
Showing
62 changed files
with
1,655 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func bar() func() func() int { | ||
x := 1 | ||
|
||
// First level of closure, modifies x | ||
return func() func() int { | ||
//x++ | ||
// Second level of closure, returns x | ||
return func() int { | ||
return x | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
f := bar() // f is the first-level closure | ||
g := f() // g is the second-level closure, x is incremented here | ||
|
||
fmt.Println(g()) // prints the value of x after being modified by the first-level closure | ||
} | ||
|
||
// Output: | ||
// 1 |
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 main | ||
|
||
import "fmt" | ||
|
||
func bar() func() func() int { | ||
x := 1 | ||
|
||
// First level of closure, modifies x | ||
return func() func() int { | ||
x++ | ||
// Second level of closure, returns x | ||
return func() int { | ||
return x | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
f := bar() // f is the first-level closure | ||
g := f() // g is the second-level closure, x is incremented here | ||
|
||
fmt.Println(g()) // prints the value of x after being modified by the first-level closure | ||
} | ||
|
||
// Output: | ||
// 2 |
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,25 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
f := func() int { | ||
if true { | ||
if true { | ||
return x | ||
} | ||
} | ||
return 0 | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 |
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 main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
f := func() int { | ||
if true { | ||
return x | ||
} | ||
return 0 | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 |
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,27 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
y := 0 | ||
f := func() int { | ||
if true { | ||
x += y | ||
if true { | ||
return x | ||
} | ||
} | ||
return 0 | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 |
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,27 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
y := 0 | ||
f := func() int { | ||
if true { | ||
x += y | ||
if true { | ||
return x | ||
} | ||
} | ||
return 0 | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 |
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,22 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
f := func() int { | ||
{ | ||
return x | ||
} | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 |
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 main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
f := func() int { | ||
for i := 0; i < 1; i++ { | ||
x++ | ||
} | ||
return x | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 1 | ||
// 2 |
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,25 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
s := []int{1, 2} | ||
|
||
f := func() int { | ||
for _, v := range s { | ||
x += v | ||
} | ||
return x | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 3 | ||
// 4 |
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,27 @@ | ||
package main | ||
|
||
func main() { | ||
var fns []func() int | ||
|
||
for i := 0; i < 2; i++ { | ||
x := i | ||
y := 1 | ||
f := func() int { | ||
switch y { | ||
case 1: | ||
x += 1 | ||
default: | ||
x += 0 | ||
} | ||
return x | ||
} | ||
fns = append(fns, f) | ||
} | ||
for _, fn := range fns { | ||
println(fn()) | ||
} | ||
} | ||
|
||
// Output: | ||
// 1 | ||
// 2 |
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,27 @@ | ||
package main | ||
|
||
type queueOnePass struct { | ||
sparse []uint32 | ||
dense []uint32 | ||
size, nextIndex uint32 | ||
} | ||
|
||
func newQueue(size int) (q *queueOnePass) { | ||
return &queueOnePass{ | ||
sparse: make([]uint32, size), | ||
dense: make([]uint32, size), | ||
} | ||
} | ||
func main() { | ||
var ( | ||
visitQueue = newQueue(10) | ||
) | ||
f := func() { | ||
println(visitQueue.size) | ||
} | ||
|
||
f() | ||
} | ||
|
||
// Output: | ||
// 0 |
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,20 @@ | ||
package main | ||
|
||
func main() { | ||
s := []int{1, 2} | ||
|
||
f := func() { | ||
for i, v := range s { | ||
println(i) | ||
println(v) | ||
} | ||
} | ||
|
||
f() | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 | ||
// 1 | ||
// 2 |
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,14 @@ | ||
package main | ||
|
||
func main() { | ||
f := func(a int) bool { | ||
println(a) | ||
return true | ||
} | ||
|
||
println(f(5)) | ||
} | ||
|
||
// Output: | ||
// 5 | ||
// true |
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,25 @@ | ||
package main | ||
|
||
func main() { | ||
s := 1 | ||
f := func() { | ||
i := 0 // no capture for i | ||
var j = s // s should be captured, j not | ||
k := s // s should be captured, k not | ||
m, n := s, 0 | ||
println(i) | ||
println(j) | ||
println(k) | ||
println(m) | ||
println(n) | ||
} | ||
|
||
f() | ||
} | ||
|
||
// Output: | ||
// 0 | ||
// 1 | ||
// 1 | ||
// 1 | ||
// 0 |
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 @@ | ||
package main | ||
|
||
func main() { | ||
s := []int{1, 2} | ||
|
||
f := func() { | ||
if len(s) == 2 { | ||
println("ok") | ||
} | ||
} | ||
f() | ||
} | ||
|
||
// Output: | ||
// ok |
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,24 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
var recursiveFunc func(int) int | ||
var recursiveFunc2 func(int) int | ||
|
||
recursiveFunc = func(num int) int { | ||
recursiveFunc2 = recursiveFunc | ||
|
||
if num <= 0 { | ||
return 1 | ||
} | ||
|
||
return num * recursiveFunc2(num-1) | ||
} | ||
|
||
result := recursiveFunc(5) | ||
fmt.Println("Factorial of 5 is:", result) // Output: 120 | ||
} | ||
|
||
// Output: | ||
// Factorial of 5 is: 120 |
Oops, something went wrong.