# Lambda Extensible FizzBuzz 2.0 With LINQ

Many of you have probably heard of the FizzBuzz challenge:

Jeff Atwood blogged about it here

I think it started from here

Anyway, if you don't want to click those links and you're not familiar with it, it is a small programming problem designed to screen someone who can actually write code from someone who pretends to write code. The problem is:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”

The point is this is a simple problem. Just simple enough to be interesting. I actually got asked to solve this problem this week. I solved it after bumbling through it way more than I should have (I was trying to think of an elegant solution on the spot.) But it kept bugging me. I knew there was a solution that I wanted to give it that would make me happy with feelings of maps, function pointers and linq. Pretty much 3 of my favorite things. So I thought about it for a bit and here is the solution I came up with (3rd cut.)

That's my crack at it. If you're not familiar with all the C# magic going on here, basically it works like this:

You have a map which maps one lambda expression which defines the rule we are checking for to a second lambda expression which is what string to return if that rule matches.

Then we have a LINQ query that does a Cartesian join on the set of numbers from 1 to 100 and the 4 rules.

Finally, we take the output and for each string we write it out.

The 4th rule in the set makes sure that after evaluating the set of rules for each number a newline gets put in.

It is kind of interesting how thinking about different ways to solve a fairly simple problem can help you think about the tools you have in new ways.