Skip to content

Instantly share code, notes, and snippets.

View dcolthorp's full-sized avatar

Drew Colthorp dcolthorp

View GitHub Profile
/** Used by Flavor to mark a type in a readable way. */
export interface Flavoring<FlavorT> {
_type?: FlavorT;
}
/** Create a "flavored" version of a type. TypeScript will disallow mixing flavors, but will allow unflavored values of that type to be passed in where a flavored version is expected. This is a less restrictive form of branding. */
export type Flavor<T, FlavorT> = T & Flavoring<FlavorT>;
/** Used by Brand to mark a type in a readable way. */
export interface Branding<BrandT> {
_type: BrandT;
/** An empty type which "brands" a value as having been checked for validity */
enum IsValidBrand {}
/** A `Valid<T>` is just a T that the type system knows is valid. */
export type Valid<T> = T & IsValidBrand;
/** This is some arbitrary example type which we want to validate. */
interface Model { nonEmptyArray: number[] }
/** A predicate to check that a model is valid. The return type tells TypeScript that this proves the argument is valid. */
@dcolthorp
dcolthorp / Ref.cs
Last active October 5, 2020 18:19
Ref and Store redux-like implementation for c#
public delegate TResult RefFunc<TState, TResult>(ref TState current);
public delegate void RefAction<TState>(ref TState current);
public delegate void RefAction<TState, TEvent>(ref TState state, TEvent action);
namespace Framework
{
public static class RefPublishStrategies
{
@dcolthorp
dcolthorp / ReactiveProperty.cs
Created March 30, 2017 12:20
ReactiveProperty<T>
public static class ReactiveProperty
{
public static ReactiveProperty<T> Create<T>(IObservable<T> observable) => new ReactiveProperty<T>(observable);
}
public class ReactiveProperty<T> : INotifyPropertyChanged, IDisposable
{
private readonly IDisposable _subscription;
private Exception _exception;
private T _value;
@dcolthorp
dcolthorp / broken markdown.md
Created February 24, 2017 13:44
This paragraph breaks Bear (markdown compatibility mode)

C#, unfortunately, still does not have convenient ways of creating immutable classes with a convenient copy-and-update mechanism, deep equality semantics, and a sensible GetHashCode implementation. (And this was cut from C#= 7. 😢) By representing our state with structs, our action handlers can be written conveniently as

var myDictionary = new Dictionary<string,object> {{"bar", 1}};
var cmd = connection.CreateCommandWithNamedParameters("select * from table where foo = ?bar", myDictionary);
var reader = cmd.ExecuteReader();
@dcolthorp
dcolthorp / di.rb
Created November 26, 2012 03:10
Simply Ruby Dependency Injection
class Context
def initialize()
@cache = {}
end
def [](klass)
if @cache.has_key?(klass)
@cache[klass]
else
$(domNode).waypoint(function() {
initializeChart(domNode);
}, {offset: "100%", triggerOnce: true});
initializeChart(domNode);
<script id="messages-template" type="text/html">
<ul class="messages">
<li><span class="text">&nbsp;</span></li>
</ul>
</script>