handwritingでfizzbuzz

(handwritingは自作のパッケージ。まだwip。todo どこかで説明)

昔にpythonで書いた貧者のfizzbuzzを作ってみる。

貧者のfizzbuzzとは(悪い意味で)まじめな人が書いたfizzbuzzのこと。(全ての条件分岐と出力処理を手書きする)

code

package main

import (
    "fmt"
    "log"

    "github.com/podhmo/handwriting"
)

func main() {
    p := handwriting.Must(handwriting.New("./main"))
    f := p.File("main.go")
    f.Import("fmt")
    f.Code(func(f *handwriting.File) error {
        o := f.Out
        o.WithBlock("func main()", func() {
            o.WithBlock("for i := 1; i<=100; i++", func() {
                o.WithBlock("switch i", func() {
                    for i := 0; i <= 100; i++ {
                        o.WithIndent(fmt.Sprintf("case %d:", i), func() {
                            if i%3 == 0 && i%5 == 0 {
                                o.Println(`fmt.Println("fizzbuzz")`)
                            } else if i%3 == 0 {
                                o.Println(`fmt.Println("fizz")`)
                            } else if i%5 == 0 {
                                o.Println(`fmt.Println("buzz")`)
                            } else {
                                o.Println(`fmt.Printf("%d\n", i)`)
                            }
                        })
                    }
                    o.WithIndent("default:", func() {
                        o.Println(`panic("not supported")`)
                    })
                })
            })
        })
        return nil
    })
    if err := p.Emit(); err != nil {
        log.Fatal(err)
    }
}

実行結果

$ go run main.go
2018/04/03 23:55:57 package ./main is not found, creating.
2018/04/03 23:55:57 open "main/main.go"
$ go run main/main.go
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
..
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz

出力されたコード

package main

import (
    "fmt"
)

func main() {
    for i := 1; i<=100; i++ {
        switch i {
            case 0:
                fmt.Println("fizzbuzz")
            case 1:
                fmt.Printf("%d\n", i)
            case 2:
                fmt.Printf("%d\n", i)
            case 3:
                fmt.Println("fizz")
            case 4:
                fmt.Printf("%d\n", i)
            case 5:
                fmt.Println("buzz")
            case 6:
                fmt.Println("fizz")
            case 7:
                fmt.Printf("%d\n", i)
            case 8:
                fmt.Printf("%d\n", i)
            case 9:
                fmt.Println("fizz")
            case 10:
                fmt.Println("buzz")
            case 11:
                fmt.Printf("%d\n", i)
            case 12:
                fmt.Println("fizz")
            case 13:
                fmt.Printf("%d\n", i)
            case 14:
                fmt.Printf("%d\n", i)
            case 15:
                fmt.Println("fizzbuzz")
            case 16:
                fmt.Printf("%d\n", i)
            case 17:
                fmt.Printf("%d\n", i)
            case 18:
                fmt.Println("fizz")
            case 19:
                fmt.Printf("%d\n", i)
            case 20:
                fmt.Println("buzz")
            case 21:
                fmt.Println("fizz")
            case 22:
                fmt.Printf("%d\n", i)
            case 23:
                fmt.Printf("%d\n", i)
            case 24:
                fmt.Println("fizz")
            case 25:
                fmt.Println("buzz")
            case 26:
                fmt.Printf("%d\n", i)
            case 27:
                fmt.Println("fizz")
            case 28:
                fmt.Printf("%d\n", i)
            case 29:
                fmt.Printf("%d\n", i)
            case 30:
                fmt.Println("fizzbuzz")
            case 31:
                fmt.Printf("%d\n", i)
            case 32:
                fmt.Printf("%d\n", i)
            case 33:
                fmt.Println("fizz")
            case 34:
                fmt.Printf("%d\n", i)
            case 35:
                fmt.Println("buzz")
            case 36:
                fmt.Println("fizz")
            case 37:
                fmt.Printf("%d\n", i)
            case 38:
                fmt.Printf("%d\n", i)
            case 39:
                fmt.Println("fizz")
            case 40:
                fmt.Println("buzz")
            case 41:
                fmt.Printf("%d\n", i)
            case 42:
                fmt.Println("fizz")
            case 43:
                fmt.Printf("%d\n", i)
            case 44:
                fmt.Printf("%d\n", i)
            case 45:
                fmt.Println("fizzbuzz")
            case 46:
                fmt.Printf("%d\n", i)
            case 47:
                fmt.Printf("%d\n", i)
            case 48:
                fmt.Println("fizz")
            case 49:
                fmt.Printf("%d\n", i)
            case 50:
                fmt.Println("buzz")
            case 51:
                fmt.Println("fizz")
            case 52:
                fmt.Printf("%d\n", i)
            case 53:
                fmt.Printf("%d\n", i)
            case 54:
                fmt.Println("fizz")
            case 55:
                fmt.Println("buzz")
            case 56:
                fmt.Printf("%d\n", i)
            case 57:
                fmt.Println("fizz")
            case 58:
                fmt.Printf("%d\n", i)
            case 59:
                fmt.Printf("%d\n", i)
            case 60:
                fmt.Println("fizzbuzz")
            case 61:
                fmt.Printf("%d\n", i)
            case 62:
                fmt.Printf("%d\n", i)
            case 63:
                fmt.Println("fizz")
            case 64:
                fmt.Printf("%d\n", i)
            case 65:
                fmt.Println("buzz")
            case 66:
                fmt.Println("fizz")
            case 67:
                fmt.Printf("%d\n", i)
            case 68:
                fmt.Printf("%d\n", i)
            case 69:
                fmt.Println("fizz")
            case 70:
                fmt.Println("buzz")
            case 71:
                fmt.Printf("%d\n", i)
            case 72:
                fmt.Println("fizz")
            case 73:
                fmt.Printf("%d\n", i)
            case 74:
                fmt.Printf("%d\n", i)
            case 75:
                fmt.Println("fizzbuzz")
            case 76:
                fmt.Printf("%d\n", i)
            case 77:
                fmt.Printf("%d\n", i)
            case 78:
                fmt.Println("fizz")
            case 79:
                fmt.Printf("%d\n", i)
            case 80:
                fmt.Println("buzz")
            case 81:
                fmt.Println("fizz")
            case 82:
                fmt.Printf("%d\n", i)
            case 83:
                fmt.Printf("%d\n", i)
            case 84:
                fmt.Println("fizz")
            case 85:
                fmt.Println("buzz")
            case 86:
                fmt.Printf("%d\n", i)
            case 87:
                fmt.Println("fizz")
            case 88:
                fmt.Printf("%d\n", i)
            case 89:
                fmt.Printf("%d\n", i)
            case 90:
                fmt.Println("fizzbuzz")
            case 91:
                fmt.Printf("%d\n", i)
            case 92:
                fmt.Printf("%d\n", i)
            case 93:
                fmt.Println("fizz")
            case 94:
                fmt.Printf("%d\n", i)
            case 95:
                fmt.Println("buzz")
            case 96:
                fmt.Println("fizz")
            case 97:
                fmt.Printf("%d\n", i)
            case 98:
                fmt.Printf("%d\n", i)
            case 99:
                fmt.Println("fizz")
            case 100:
                fmt.Println("buzz")
            default:
                panic("not supported")
        }
    }
}