Skip to content

Instantly share code, notes, and snippets.

@keizo042
Last active April 13, 2016 17:39
Show Gist options
  • Save keizo042/4c9804a1f814b51dfd8977050defa106 to your computer and use it in GitHub Desktop.
Save keizo042/4c9804a1f814b51dfd8977050defa106 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
)
type N (chan interface{})
type Z int
func Zero() N {
n := make(N, 1)
z := *new(Z)
z = 0
n <- z
return n
}
func (n N) IsZero() bool {
m := <-n
switch m.(type) {
case N:
n <- m
return false
case Z:
n <- m
return true
default:
n <- m
return false
}
}
func (n N) Succ() N {
m := make(N, 1)
m <- n
return m
}
func (n N) Int() int {
i := 0
m := n
for {
l := <-m
switch l.(type) {
case N:
m = l.(N)
i++
case Z:
return i
default:
return -1
}
}
}
func (n N) String() string {
return strconv.Itoa(n.Int())
}
func main() {
n := Zero()
n = n.Succ()
n = n.Succ()
n = n.Succ()
n = n.Succ()
fmt.Println(n.Int())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment