Skip to content

Instantly share code, notes, and snippets.

View duarten's full-sized avatar
🤷‍♂️

Duarte Nunes duarten

🤷‍♂️
View GitHub Profile
@duarten
duarten / AsyncLock.cs
Created February 8, 2011 00:54
A simple awaitable asynchronous lock
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
#pragma warning disable 420
public class AsyncLock
{
@duarten
duarten / AsyncExchanger.cs
Created February 9, 2011 22:15
An awaitable exchanger with timeout support
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;
@duarten
duarten / WcfHttpProcessorDsl.cs
Created February 25, 2011 00:15
An WCF HTTP configuration DSL
static IProcessorProvider GetProcessorProvider()
{
return new ProcessorProviderFor<TheService>()
.RemoveDefaultMediaTypeProcessors()
.OnAllOperations(_ => _
.Use(typeof(DataValidationProcessor))
.UseForRequests(typeof(RequestLoggingProcessor)))
.OnGetOperation(_ => _
.UseForResponses(typeof(ImageFromTextMediaProcessor),
typeof(WaveFromTextMediaProcessor)))
@duarten
duarten / GetDuplicateAndMissing.cs
Created April 27, 2011 00:29
A solution to the mongoDB puzzle (http://goo.gl/A35lk)
//
// 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
public interface IBufferPool
{
byte[] TakeBuffer(int size);
void ReturnBuffer(byte[] buffer);
}
public class BufferPoolWithLeakDetection : IBufferPool
{
public class MemoryLeakException : Exception
{
@duarten
duarten / Actor.cs
Created May 5, 2011 16:42
A simple Actor implementation
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())
@duarten
duarten / LazyQsort.clj
Created June 19, 2011 22:03
A lazy quick sort
(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))))))
# 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
@duarten
duarten / AsyncEnumerator.cs
Created August 1, 2011 20:30
AsyncEnumerator redux
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);
@duarten
duarten / commatize.clj
Created February 7, 2012 18:27 — forked from unclebob/commatize
A function to format a number with commas
(defn commatize [n]
(->> n str reverse (partition-all 3) (interpose \,) flatten reverse (apply str)))