Skip to content

Instantly share code, notes, and snippets.

View cameronism's full-sized avatar

Cameron Jordan cameronism

View GitHub Profile
@cameronism
cameronism / Template.cs
Last active August 29, 2015 13:57
Fast, generic, string template pre-processor for templates with variables
public static class Template
{
public struct Span<T>
{
/// <summary>If Literal is not null it should be used and Variable should be ignored</summary>
public readonly string Literal;
/// <summary>Variable should only be used if Literal is null</summary>
public readonly T Variable;
public Span(string literal, T value)
@cameronism
cameronism / NeedleIndex.cs
Created May 27, 2014 14:27
Needle in a haystack
// find the needle that exactly matches the specified portion of haystack
// will return -1 for count of 0
public static int NeedleIndex(byte[] haystack, int offset, int count, byte[][] needles)
{
Debug.Assert(needles.Length <= 8, "Can only search for up to 8 needles at once");
ulong mask = 0;
for (int i = 0; i < needles.Length; i++)
{
if (needles[i] != null && needles[i].Length == count)
@cameronism
cameronism / SlowCloner.cs
Created December 10, 2014 00:24
Slow .NET Object Cloner
public static class SlowCloner
{
public static T Clone<T>(T val, int depth = 64)
{
return (T)CloneNonGeneric(val, typeof(T), depth);
}
static object CloneNonGeneric(object val, Type type, int depth = 64)
{
if (val == null) return null;
@cameronism
cameronism / NestedStopwatch.cs
Created March 12, 2015 03:58
Low overhead nested stopwatch for .NET
public sealed class NestedStopwatch
{
public struct Split : IDisposable
{
private NestedStopwatch _parent;
private int _index;
internal Split(NestedStopwatch sw, int index)
{
_parent = sw;
@cameronism
cameronism / DelayedTeardown.cs
Created May 20, 2015 21:52
Safe helper to defer setup until needed and teardown until after a delay
/// <summary>
/// Safe helper to defer setup until needed and teardown until after a delay
/// </summary>
/// <remarks>
/// You're on your own if setup or teardown throws or if Dispose is not called on each Acquire result
/// </remarks>
public class DelayedTeardown
{
// -1 indicates that setup needs to be run
private int _count = -1;
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
// Code
(function(container, name){
var index;
function log(){
console.log(index);
// Exercise 1 - OO || !OO
// Define a data structure for cars (make and color), and a function
// that logs a string like "I'm a red Mercedes" to the console.
// Make two versions: a functional version, and a object-oriented version.
function logCar (c) {
console.log("I'm a " + c.color + " " + c.make);
}
// Part 1.
// Implement a function prototype extension that caches function results for
// the same input arguments of a function with one parameter.
Function.prototype.cached = function() {
var cache = {},
func = this;
return function( arg ) {
return cache[ arg ] = arg in cache ? cache[ arg ] : func(arg);
function Person (name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
}
// 1
document.getElementById('the_div').addEventListener(
'click', function(){ log('the_div!') }, true);
document.getElementById('the_list').addEventListener(
'click', function(){ log('the_list!') }, !true);
document.getElementById('the_item').addEventListener(
'click', function(){ log('the_item!') }, true);