Skip to content

Instantly share code, notes, and snippets.

@rossmurray
rossmurray / disposablewrapper.cs
Created November 21, 2014 16:21
Return an IDisposable instance of T and control what happens on Dispose, without making a whole class for it.
public static class DisposableWrapper
{
public static DisposableWrapper<T> Wrap<T>(T wrapped, Action onDispose)
{
return new DisposableWrapper<T>(wrapped, onDispose);
}
}
public class DisposableWrapper<T> : IDisposable
{
@rossmurray
rossmurray / FizzBuzz.cs
Last active August 29, 2015 14:04
FizzBuzz
public IList<string> Solve(int n)
{
return Enumerable.Range(1, n).Select(x =>
x % 15 == 0 ? "FizzBuzz"
: x % 5 == 0 ? "Buzz"
: x % 3 == 0 ? "Fizz"
: x.ToString()
).ToArray();
}
@rossmurray
rossmurray / TableExtraction.cs
Created April 30, 2014 21:55
Grouping absolutely-positioned elements into columns (table extraction).
public IList<IEnumerable<Rectangle>> GetColumns(IList<Rectangle> elements)
{
if(elements == null) throw new ArgumentNullException("elements");
if(!elements.Any()) return new List<IEnumerable<Rectangle>>();
var length = elements.Count();
if(length == 1) return new[]{elements.ToList()};
var sorted = elements.OrderBy(x => x.X).ToArray();
var firstElement = sorted.First();
var columns = new List<IEnumerable<Rectangle>>();
@rossmurray
rossmurray / dictionaryComparer.cs
Created April 25, 2014 22:59
Dictionary comparer, great for grouping dictionaries with LINQ. GetHashCode implementation could use work.
public class KeyEqualityComparer<T, U> : IEqualityComparer<KeyValuePair<T, U>>
{
public bool Equals(KeyValuePair<T, U> x, KeyValuePair<T, U> y)
{
return (x.Key.Equals(y.Key) && x.Value.Equals(y.Value));
}
public int GetHashCode(KeyValuePair<T, U> obj)
{
return obj.Key.GetHashCode();
@rossmurray
rossmurray / usingMethod.cs
Created April 25, 2014 21:16
Using method, much more composable than a using statement.
public static class DisposableExtensions
{
public static R Using<U,R>(this U disposable, Func<U,R> body) where U : IDisposable
{
try
{
return body(disposable);
}
finally
{
public static IEnumerable<IEnumerable<T>> Window<T>(this IEnumerable<T> source, int size)
{
if(size < 2) return source.Select(x => new[]{x});
return source.Zip(source.Skip(1).Window(size - 1), (a,b) => new[]{a}.Concat(b));
}
@rossmurray
rossmurray / while_stream.cs
Created February 16, 2014 18:12
Generating a stream with while loop
void Main()
{
var records = GetRecordStream();
foreach(var record in records)
{
SaveRecord(record);
}
}
private IEnumerable<Record> GetRecordStream()
@rossmurray
rossmurray / while_loop.cs
Last active August 29, 2015 13:56
traditional while loop
void Main()
{
ReadAndSaveRecords();
}
private void ReadAndSaveRecords()
{
bool moreRecords = true;
while(moreRecords)
{
@rossmurray
rossmurray / infinity.cs
Last active August 29, 2015 13:56
Infinity() and TakeWhile() as a stream, instead of a while loop.
void Main()
{
var records = GetRecordStream();
foreach (var record in records)
{
SaveRecord(record);
}
}
private IEnumerable<string> GetRecordStream()
@rossmurray
rossmurray / merge_insert.cs
Created February 9, 2014 05:47
merge and insertion for an assignment
void Main()
{
var r = new Random(100);
var list = Enumerable.Range(1,1000000).OrderBy(x => r.Next()).ToList();
var sorted = MergeSort(list);
var correct = (sorted.SequenceEqual(list.OrderBy(x => x))); //true
}
public IList<int> MergeSort(IEnumerable<int> source)
{