Skip to content

Instantly share code, notes, and snippets.

View jcracknell's full-sized avatar

James Cracknell jcracknell

  • GNWT Department of Justice
  • Yellowknife, NT
View GitHub Profile
@jcracknell
jcracknell / ContravariantSpecificity.1.scala
Last active December 24, 2015 14:29
Examples demonstrating scala's bizzare specificity rules as applied to contravariant types.
class Animal
class Cat extends Animal
abstract class Whisperer[-A <: Animal] extends (A => Unit)
object Whisperer {
implicit def animalWhisperer: Whisperer[Animal] =
new Whisperer[Animal] { def apply(a: Animal): Unit = { println("animal") } }
implicit def catWhisperer: Whisperer[Cat] =
@jcracknell
jcracknell / LiteralEncoding.scala
Last active December 22, 2015 05:49
LiteralEncoding.scala
object LiteralEncoding {
private val Hex = "0123456789abcdef".toArray
private val StringEscapes = {
val escapes = Map(
'\b' -> 'b', // backspace
'\t' -> 't', // tab
'\n' -> 'n', // linefeed
'\f' -> 'f', // formfeed
'\r' -> 'r', // carriage return
public static string RemoveDiacritics(this string str) {
var formD = str.Normalize(NormalizationForm.FormD);
var bp = 0;
var buffer = new char[formD.Length];
for(var i = 0; i < formD.Length; i++)
if(UnicodeCategory.NonSpacingMark != CharUnicodeInfo.GetUnicodeCategory(formD[i]))
buffer[bp++] = formD[i];
return new string(buffer, 0, bp);
@jcracknell
jcracknell / StringLiteralEncoding.cs
Last active December 18, 2015 01:09
Encode C# strings to an equivalent string literal.
using System;
using System.Text;
using System.Linq;
public static class StringLiteralEncoding {
private static readonly char[] HEX_DIGIT_LOWER = "0123456789abcdef".ToCharArray();
private static readonly char[] LITERALENCODE_ESCAPE_CHARS;
static StringLiteralEncoding() {
// Per http://msdn.microsoft.com/en-us/library/h21280bw.aspx
@jcracknell
jcracknell / SslRedirectionHttpHandler.cs
Last active December 17, 2015 01:29
Incredibly simple redirection to equivalent SSL-secured URI. This is intended to be configured on a dedicated site listening on port 80, and will blindly redirect all GET requests to the same URL with the `https` protocol, and `403.4 SSL Required` everything else.
using System;
using System.Web;
namespace SslRedirection {
public class SslRedirectionHttpHandler : IHttpHandler {
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) {
if ("GET".Equals(context.Request.HttpMethod, StringComparison.Ordinal)) {
context.Response.StatusCode = 301;
@jcracknell
jcracknell / levenshtein.js
Last active December 17, 2015 00:11
Efficient Javascript implementation of Levenshtein distance.
// Compute the Levenshtein distance between `Array`-like sequences `a` and `b`; the minimum number of
// single-element insertions, deletions and substitutions necessary to transform `a` into `b`.
// Strict equality semantics are used by default to compare sequence elements; a function `equal` can
// optionally be provided to alter this behavior.
function levenshtein(a, b, equal) {
// `aii` and `bii` are used to track `ai - 1` and `bi - 1` more or less free of charge.
// `d0` and `d1` are used to store the 'previous' and 'current' rows respectively.
// `x`, `y` and `z` are used to permit the implementation of an optimized `min` using a ternary expression.
var ai, aii, ae, al, bi, bii, bl, d0, d1, dd, x, y, z;
if(a === b) return 0;