diff --git a/about_anonymous_functions.go b/about_anonymous_functions.go new file mode 100644 index 0000000..06d4566 --- /dev/null +++ b/about_anonymous_functions.go @@ -0,0 +1,29 @@ +package go_koans + +func testAnonymousFunctions() { + { + i := 1 + increment := func() { + i++ + } + increment() + + assert(i == __int__) // closures function in an obvious way + } + + { + i := 1 + increment := func(x int) { + x++ + } + increment(i) + + assert(i == __int__) // although anonymous functions need not always be closures + } + + { + double := func(x int) int { return x * 2 } + + assert(double(3) == __int__) // they can do anything our hearts desire + } +} diff --git a/setup_koans_test.go b/setup_koans_test.go index d00f40f..03f9b10 100644 --- a/setup_koans_test.go +++ b/setup_koans_test.go @@ -22,12 +22,12 @@ func TestKoans(t *testing.T) { //testArrays() //testSlices() //testControlFlow() - testEnumeration() + //testEnumeration() + testAnonymousFunctions() // TODO: ie, gameplan //testFiles() //testVariadicFunctions() - //testAnonymousFunctions() //testInterfaces() //testMaps() //testStructs()