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.Concurrent; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
#pragma warning disable 420 | |
public class AsyncLock | |
{ |
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 AsyncExchanger<T> | |
{ | |
private ExchangerAwaiter _awaiter; | |
private class ExchangerAwaiter : IAwaiter<T> | |
{ | |
private const int PENDING = 0; | |
private const int DONE = 1; | |
private const int TIMEOUT = 2; |
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
static IProcessorProvider GetProcessorProvider() | |
{ | |
return new ProcessorProviderFor<TheService>() | |
.RemoveDefaultMediaTypeProcessors() | |
.OnAllOperations(_ => _ | |
.Use(typeof(DataValidationProcessor)) | |
.UseForRequests(typeof(RequestLoggingProcessor))) | |
.OnGetOperation(_ => _ | |
.UseForResponses(typeof(ImageFromTextMediaProcessor), | |
typeof(WaveFromTextMediaProcessor))) |
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
// | |
// Given an array of N (can be very large) 64 bit integers where every integer 1-N | |
// appears once in the array, except there is one integer missing and one integer | |
// duplicated, find the missing and duplicated integers. The algorithm should run | |
// in linear time and in small constant space and leave the array untouched. | |
// | |
static void GetDuplicateAndMissing(long[] a, out long duplicate, out long missing) | |
{ | |
long xor = 0; // duplicate ^ missing |
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 interface IBufferPool | |
{ | |
byte[] TakeBuffer(int size); | |
void ReturnBuffer(byte[] buffer); | |
} | |
public class BufferPoolWithLeakDetection : IBufferPool | |
{ | |
public class MemoryLeakException : Exception | |
{ |
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 Actor<TState> | |
{ | |
volatile int executing; | |
readonly ConcurrentQueue<Func<TState, Task>> funcs = new ConcurrentQueue<Func<TState, Task>>(); | |
public TState State { get; set; } | |
public void Act(Func<TState, Task> func) | |
{ | |
if (TryAcquire()) |
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
(ns sort | |
(:use clojure.test)) | |
(defn qsort [xs] | |
"Sorts the specified sequence using the quick sort algorithm" | |
(if-let [[pivot & rest] (seq xs)] | |
(let [smaller? #(< % pivot)] | |
(lazy-cat (qsort (filter smaller? rest)) | |
[pivot] | |
(qsort (remove smaller? rest)))))) |
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
# I want this method in ruby-core | |
def let | |
yield | |
end | |
def fib(i) | |
let do |n = 1, result = 0| | |
if i == -1 | |
result | |
else |
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 AsyncEnumerator | |
{ | |
// | |
// Asynchronously but sequentially executes all tasks returned by the specified enumerator. | |
// | |
public static Task Run(this IEnumerator<Task> source, | |
CancellationToken token = default(CancellationToken)) | |
{ | |
return runInternal<object>(source, token); |
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
(defn commatize [n] | |
(->> n str reverse (partition-all 3) (interpose \,) flatten reverse (apply str))) |
OlderNewer