Skip to content

Instantly share code, notes, and snippets.

@aesteve
Created January 20, 2016 20:57
Show Gist options
  • Save aesteve/5163a7ba661c1620355f to your computer and use it in GitHub Desktop.
Save aesteve/5163a7ba661c1620355f to your computer and use it in GitHub Desktop.
Slices (with closure)
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