Created
September 8, 2010 16:48
-
-
Save MikeBild/570401 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication4 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var source = Observable.Range(1, 100); | |
var consoleObserver = Observer.Create<string>(Console.WriteLine); | |
var debugObserver = Observer.Create<string>(n=>Debug.WriteLine(n)); | |
var connected = source.FizzBuzzLogic().Publish(); | |
connected.Subscribe(consoleObserver); | |
connected.Subscribe(debugObserver); | |
using(connected.Connect()) | |
{ | |
Console.ReadLine(); | |
} | |
} | |
} | |
public static class FizzBuzzExtentions | |
{ | |
public static IObservable<string> FizzBuzzLogic(this IObservable<int> source) | |
{ | |
return source.Select(number => | |
{ | |
if (IsFizzBuzz(number)) | |
return "FizzBuzz"; | |
else if (IsFizz(number)) | |
return "Fizz"; | |
else if (IsBuzz(number)) | |
return "Buzz"; | |
return number.ToString(); | |
}); | |
} | |
public static bool IsBuzz(int number) | |
{ | |
return (number % 5 == 0); | |
} | |
public static bool IsFizz(int number) | |
{ | |
return (number % 3 == 0); | |
} | |
public static bool IsFizzBuzz(int number) | |
{ | |
return ((number % 5 == 0) && (number % 3 == 0)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment