Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created August 15, 2011 15:10
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 nobeans/1146957 to your computer and use it in GitHub Desktop.
Save nobeans/1146957 to your computer and use it in GitHub Desktop.
// http://d.hatena.ne.jp/fumokmm/20110815/1313405510
def fizzBuzz(List<Integer> nums, Map<Integer, String> dict) {
nums.each { int n ->
println dict.findAll{ n % it.key == 0 }.collect{ dict[it.key] }.join() ?: n
}
}
def convertDict(List dict) {
assert dict.size() % 2 == 0
(0..<dict.size()).step(2).collectEntries{ [dict[it], dict[++it]] }
}
fizzBuzz(1..100, convertDict([3, "Fizz", 5, "Buzz", 7, "Hoge"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment