Skip to content

Instantly share code, notes, and snippets.

View StephenCleary's full-sized avatar

Stephen Cleary StephenCleary

View GitHub Profile
@StephenCleary
StephenCleary / Program.cs
Last active December 30, 2015 21:59
Serialization size test for Azure caching. Requires NuGet packages: Comparers, Newtonsoft.Json
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Comparers;
@StephenCleary
StephenCleary / LinkedDic.cs
Last active December 31, 2015 04:09 — forked from ayende/LinkedDic.cs
Added tests with Builder, SortedImmutableDictionary, and LinkedDictionaryUnoptimized.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Voron.Util
{
public class LinkedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>
where TValue : class, new()
{
private readonly TValue _deleteMarker = new TValue();
@StephenCleary
StephenCleary / async.cs
Last active February 9, 2017 11:54 — forked from davidfowl/async.cs
Async quiz: How many threads are used? What does it print?
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// The more evil version. :)
class Program
{
static void Main(string[] args)
{
@StephenCleary
StephenCleary / InMemoryConfiguration.cs
Created September 6, 2017 20:53
AddJson and AddJsonObject extension methods for IConfigurationBuilder
public sealed class InMemoryFileProvider : IFileProvider
{
private readonly string _json;
public InMemoryFileProvider(string json)
{
_json = json;
}
public IFileInfo GetFileInfo(string subpath) => new FileInfo(_json);
@StephenCleary
StephenCleary / coordination.ts
Created August 18, 2017 04:48
AsyncEx... for TypeScript
class Future<T> {
private resolver: (value: T) => void; // readonly
private rejecter: (error: Error) => void; // readonly
private _isCompleted: boolean = false;
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolver = resolve;
this.rejecter = reject;
});
@StephenCleary
StephenCleary / Try.cs
Last active March 16, 2019 02:32
Try (error) monad in C#
public static class Try
{
public static Try<T> FromException<T>(Exception exception) => Try<T>.FromException(exception);
public static Try<T> FromValue<T>(T value) => Try<T>.FromValue(value);
public static Try<T> Create<T>(Func<T> func) => Try<T>.Create(func);
}
public sealed class Try<T>
{
public static Try<T> FromException(Exception exception) => new Try<T>(exception, default, true);
@StephenCleary
StephenCleary / Auth0Authenticator.cs
Created September 6, 2017 18:09
Auth0 authenticator for use with Azure Functions
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;
@StephenCleary
StephenCleary / Async WebAPI
Last active April 17, 2020 03:05
Simple WebAPI app with Console client that shows that async methods do release threads to the thread pool.
Simple WebAPI app with Console client that shows that async methods do release threads to the thread pool.
@StephenCleary
StephenCleary / JsonCreationConverter.cs
Created September 6, 2017 01:16
A JsonConverter that determines the type of the deserialized object by its JSON representation. Combination of several approaches including CustomCreatorConverter and https://stackoverflow.com/questions/22537233/json-net-how-to-deserialize-interface-property-based-on-parent-holder-object/22539730#22539730
/// <summary>
/// Creates a custom object based on the json values.
/// </summary>
/// <typeparam name="T">The object type to convert.</typeparam>
public abstract class JsonCreationConverter<T> : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
@StephenCleary
StephenCleary / ObserverProgress.cs
Created November 6, 2013 03:23
ObserverProgress: An IProgress implementation that forwards to an IObserver.
using System;
using System.Threading.Tasks;
namespace Nito.AsyncEx
{
/// <summary>
/// A progress implementation that sends progress reports to an observer stream. Optionally ends the stream when the task completes.
/// </summary>
/// <typeparam name="T">The type of progress value.</typeparam>
internal sealed class ObserverProgress<T> : IProgress<T>