Skip to content

Instantly share code, notes, and snippets.

@sohjsolwin
Created September 11, 2015 19:48
Show Gist options
  • Save sohjsolwin/c7a3f2c73392607d1bf4 to your computer and use it in GitHub Desktop.
Save sohjsolwin/c7a3f2c73392607d1bf4 to your computer and use it in GitHub Desktop.
GoLang Tour Slice code solution
package main
import "golang.org/x/tour/pic"
func MakePic(val (func(int, int) uint8)) (func(int, int) [][]uint8) {
var pic = func(dx, dy int) ([][]uint8) {
var dyslice = make([][]uint8, dy)
for i := range dyslice {
dyslice[i] = make([]uint8, dx)
}
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
dyslice[x][y] = val(x, y)
}
}
return dyslice
}
return pic
}
func power(x, y int) uint8 {
return (uint8)(x^y)
}
func calc(x, y int) uint8 {
return (uint8)((x+y)/2)
}
func mult(x, y int) uint8 {
return (uint8)(x*y)
}
func main() {
//pic.Show(MakePic(power))
pic.Show(MakePic(calc))
//pic.Show(MakePic(mult))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment