Quartz Composer FizzBuzz
At the PSIG meeting a few weeks ago, Jason Foreman demonstrated FizzBuzz first using Erlang, and then Quartz Composer. I didn't see how he implemented the QC version, but I was intrigued and decided to have a crack at it myself. Here's a QuickTime result, and the qtz file:
There's a few nested macros there, but the core macro is the "FizzBuzz" macro:
This macro takes a number as an input, and returns the proper string, "Fizz", "Buzz", "FizzBuzz", or just the number. The "Fizz?" macro returns true if the number is divisible by 3, and the "Buzz?" patch returns true if the number is divisible by 5. These are fed into a Math patch that calculates a number from 0 to 3 for all 4 possible conditions of "Fizz?" and "Buzz?". This number is fed into a multiplexer which outputs the correct string. Here's the inspector for the multiplexer, which should make it a bit more clear:
And that's really about it. The Iterator patch causes the "FizzBuzz" patch to be called 100 times in parallel to generate 100 strings. There's a macro that calculates the Y position of each string, which ensures that each number is on it's own line, and also does the vertical scrolling. It turns out this composition puts quite a load on my Core 2 Duo iMac at ~95% with 30FPS, so this is hardly the most efficient FizzBuzz implementation on the planet.
This essentially boils down to the following Ruby code:
(1..100).each do |i| fizz = (i % 3) == 0? 1 : 0 buzz = (i % 5) == 0? 1 : 0 index = (buzz << 1) | fizz output = [i, "Fizz", "Buzz", "FizzBuzz"] puts output[index] end
What's your implementation?