Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active October 19, 2015 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/1763112f673901f49370 to your computer and use it in GitHub Desktop.
Save caelifer/1763112f673901f49370 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Rule func(int) string
func filter(div int, tag string) Rule {
return func(n int) string {
if n%div == 0 {
return tag
}
return ""
}
}
func apply(rules []Rule, n int) interface{} {
res := ""
for _, rule := range rules {
res += rule(n)
}
if len(res) > 0 {
return res
}
return n
}
func main() {
var rules = []Rule{
filter(3, "Fizz"),
filter(5, "Buzz"),
filter(9, "Boom"),
}
for i := 1; i <= 45; i++ {
fmt.Println(apply(rules, i))
}
}
@caelifer
Copy link
Author

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