Created
November 16, 2011 08:01
-
-
Save kana/1369536 to your computer and use it in GitHub Desktop.
Sleep sort with Reactive Extensions
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.Linq; | |
using System.Reactive.Linq; | |
namespace RxHello | |
{ | |
public static class EnumerableExtensions | |
{ | |
public static IObservable<T> OrderBySleepSort<T>(this IEnumerable<T> xs, Func<T, int> keySelector) | |
{ | |
return xs | |
.ToObservable() | |
.SelectMany(x => Observable.Timer(TimeSpan.FromMilliseconds(keySelector(x))).Select(_ => x)) | |
; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Press Enter key to quit..."); | |
var words = "Reactive Extensions give a great power to programmers who deal with asynchronous operations.".Split(); | |
words.OrderBySleepSort(w => w.ToLower()[0] * 10).Subscribe(Console.WriteLine); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment