Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created August 22, 2018 11:09
Show Gist options
  • Save codecakes/86e55b1941c23fba79691da7230ba1e7 to your computer and use it in GitHub Desktop.
Save codecakes/86e55b1941c23fba79691da7230ba1e7 to your computer and use it in GitHub Desktop.
a golang exercise to render a blue pixel image of dimensions dx, dy
package main
import "golang.org/x/tour/pic"
type funcIntType func(int)
func DivideFn(lo, hi int, fn funcIntType) {
if mid := int((hi - lo)/2 + lo); lo < hi {
DivideFn(lo, mid, fn)
DivideFn(mid+1, hi, fn)
} else {
fn(lo)
}
}
func RecurseDn(dx, dy int, array [][]uint8) funcIntType {
return func (idy int) {
uint8_array := make([]uint8, dx, dx)
for j := 0; j < dx; j++ {
uint8_array[j] = uint8(dy^dx) << uint8(j)
}
array[idy] = uint8_array
}
}
func Pic(dx, dy int) [][]uint8 {
var array = make([][]uint8, dy, dy)
DivideFn(0, dy-1, RecurseDn(dx, dy, array))
return array
}
func main() {
pic.Show(Pic)
}
@codecakes
Copy link
Author

so a loop at L:28 but using a divide and conquer where a generic function needs to be used, I enforced a type. a dynamic function in a scripted language would be easy. enforcing a new typedef like function construct was explicit. Good!
learnt:

  • Closures
  • defining Types
  • Slices and Arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment