Skip to content

Instantly share code, notes, and snippets.

@kana
Created November 16, 2011 08:01
Show Gist options
  • Save kana/1369536 to your computer and use it in GitHub Desktop.
Save kana/1369536 to your computer and use it in GitHub Desktop.
Sleep sort with Reactive Extensions
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