Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created June 12, 2011 04:17
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 dbrady/1021248 to your computer and use it in GitHub Desktop.
Save dbrady/1021248 to your computer and use it in GitHub Desktop.
FizzBuzz!
Transcript clear.
(1 to: 100) do: [ :i | (i \\ 15 = 0)
ifTrue: [Transcript show: 'FizzBuzz']
ifFalse: [
(i \\ 3 = 0)
ifTrue: [ Transcript show: 'Fizz']
ifFalse: [
(i \\ 5 = 0)
ifTrue: [ Transcript show: 'Buzz' ]
ifFalse: [Transcript show: i].
].
].
Transcript cr.
].
@timfel
Copy link

timfel commented Jun 12, 2011

How about this?

| mapping |
mapping := { [:i | i \ 15 = 0] -> 'FizzBuzz' .
[:i | i \ 3 = 0] -> 'Fizz' .
[:i | i \ 5 = 0] -> 'Buzz' }.
(1 to: 100) do: [:i |
Transcript show: (mapping
detect: [:assoc | assoc key value: i]
ifNone: [nil -> i]) value; cr].

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