Created
January 20, 2016 20:57
-
-
Save aesteve/5163a7ba661c1620355f to your computer and use it in GitHub Desktop.
Slices (with closure)
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
package main | |
import "golang.org/x/tour/pic" | |
func mult(x, y int) uint8 { | |
return uint8( x * y ) | |
} | |
func mid(x, y int) uint8 { | |
return uint8( (x + y) / 2 ) | |
} | |
func pow(x, y int) uint8 { | |
return uint8( x^y ) | |
} | |
func generateWithFunc(generator func(x, y int) uint8) func(dx int, dy int) [][]uint8 { | |
return func(dx, dy int) [][]uint8 { | |
tab := make([][]uint8, dx) | |
for x := range tab { | |
tab[x] = make([]uint8, dy) | |
for y := range tab[x] { | |
tab[x][y] = generator(x, y) | |
} | |
} | |
return tab | |
} | |
} | |
func main() { | |
pic.Show(generateWithFunc(mid)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment