This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class NestedStopwatch | |
{ | |
public struct Split : IDisposable | |
{ | |
private NestedStopwatch _parent; | |
private int _index; | |
internal Split(NestedStopwatch sw, int index) | |
{ | |
_parent = sw; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person (name) { | |
this.name = name; | |
} | |
Person.prototype.getName = function () { | |
return this.name; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
OlderNewer