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
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 | |
{ |
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
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(); | |
} |
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
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>>(); |
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
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(); |
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
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 | |
{ |
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
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)); | |
} |
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
void Main() | |
{ | |
var records = GetRecordStream(); | |
foreach(var record in records) | |
{ | |
SaveRecord(record); | |
} | |
} | |
private IEnumerable<Record> GetRecordStream() |
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
void Main() | |
{ | |
ReadAndSaveRecords(); | |
} | |
private void ReadAndSaveRecords() | |
{ | |
bool moreRecords = true; | |
while(moreRecords) | |
{ |
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
void Main() | |
{ | |
var records = GetRecordStream(); | |
foreach (var record in records) | |
{ | |
SaveRecord(record); | |
} | |
} | |
private IEnumerable<string> GetRecordStream() |
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
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) | |
{ |
NewerOlder