Skip to content

Instantly share code, notes, and snippets.

Michael Haufe mlhaufe

Block or report user

Report or block mlhaufe

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
mlhaufe / simulating-generics.ts
Last active Jul 8, 2019
Simulating Generics w/ Inheritance in TypeScript.
View simulating-generics.ts
abstract class Comparable {
abstract le(other: this): boolean
min(other: this): Comparable {
return this.le(other) ? this : other
abstract class Ring {
readonly zero: Ring,
mlhaufe / ConcTree.ts
Created Jul 8, 2017
Partial Implementation of ConcTree in TypeScript
View ConcTree.ts
abstract class ConcTree<T> {
abstract left: ConcTree<T>
abstract right: ConcTree<T>
abstract level: number
abstract size: number
class Empty<T> extends ConcTree<T> {
level: number = 0
size: number = 0
View PubSub.ts
interface Publisher<T> {
subscribe(s: Subscriber<T>): void
interface Subscriber<T> {
onNext(value: T): void
onError(e: Error): void
onDone(): void
onSubscribe(s: Subscription): void
mlhaufe / Either.ts
Last active Jan 16, 2017
View Either.ts
// data Either a b = Left a | Right b
abstract class Either<A, B> {
constructor(public value: A | B) { }
// Inject Left
// inl a
class Left<A> extends Either<A, never>{
constructor(value: A) { super(value) }
// Inject Right
View jquery great

Here is my general opinion from a PLT perspective:

##Faking Ad-Hoc Polymorphism

JavaScript does not support Ad-Hoc Polymorphism (One method name with multiple implementations based on parameters). As a result, many developers (including jQuery authors) try to fake it by using double-dispatch, or an inlined Great Maze of If-Else-dom/Switch-dom. To clarify what that is in the abstract here is an example of it (in TypeScript for clarity):

abstract class Shape extends Object {
    abstract onCollisionWith(shape: Shape): void
mlhaufe / index.html
Last active Dec 2, 2016
strict mode efficiency comparison w/ Newtons Method ( #jsbench #jsperf
View index.html
<!DOCTYPE html>
<meta charset="utf-8"/>
<title>strict mode efficiency comparison w/ Newtons Method</title>
<script src=""></script>
<script src="./suite.js"></script>
<h1>Open the console to view the results</h1>
mlhaufe / Color.cs
Last active Nov 17, 2016
simple color shading
View Color.cs
using System;
public class Program
public static void Main()
Func<string,double,string> shade = (hex, pct) => {
Func<int,int,int,int> clamp =
(value,min,max) => value < min ? min : value > max ? max : value;
var num = Convert.ToUInt32(hex.Replace("#",""),16);
mlhaufe / Term.ts
Created Oct 19, 2016
Term Evaluation in Pure OO style
View Term.ts
abstract class Term<T> {
abstract eval() : T
class Lit extends Term<number> {
constructor(public value: number) { super() }
eval() : number { return this.value }
class Inc extends Term<number> {
mlhaufe / cookieManager.js
Last active Jan 10, 2017
Basic Cookie Management
View cookieManager.js
var cookieManager = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
mlhaufe / handleEvent.js
Last active Jun 8, 2016
handleEvent example for ES5
View handleEvent.js
var Carousel = (function(){
"use strict"
function _handleEvent(e) { this['on' + e.type](e) },
function Carousel(el){
if(!(this instanceof Carousel))
return new Carousel(el)
this.el = el
You can’t perform that action at this time.