Last active
August 29, 2015 14:07
-
-
Save zaky/0591ffc44b44d21ddf11 to your computer and use it in GitHub Desktop.
FizzBuzz closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func setCheck(data map[int]string) func(nidle int) (ret string) { | |
| f := func(nidle int) (ret string) { | |
| for key, val := range data { | |
| if nidle%key == 0 { | |
| ret = ret + val | |
| } | |
| } | |
| return | |
| } | |
| return f | |
| } | |
| func main() { | |
| mp := map[int]string{3: "fizz", 5: "buzz", 6: "go"} | |
| f := setCheck(mp) | |
| for i := 1; i <= 100; i++ { | |
| h := f(i) | |
| if len(h) == 0 { | |
| fmt.Println(i) | |
| } else { | |
| fmt.Println(h) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://play.golang.org/p/SMzbHC2-xs