Skip to content

Instantly share code, notes, and snippets.

View JamieDixon's full-sized avatar

Jamie Dixon JamieDixon

View GitHub Profile
@JamieDixon
JamieDixon / gist:7932003
Created December 12, 2013 17:34
Basic AddOne function that adds one to each item in an IEnumerable<int> without using yield of foreach.
// Write an AddOne method that doesn't use yield or foreach
static IEnumerable<int> AddOne(IEnumerable<int> items)
{
var returnEnumer = Enumerable.Empty<int>();
var enumerator = items.GetEnumerator();
while(enumerator.MoveNext())
{
returnEnumer = returnEnumer.Concat(Enumerable.Repeat(enumerator.Current + 1, 1));
}
@JamieDixon
JamieDixon / gist:8297357
Created January 7, 2014 10:14
A collection that returns items added in the last 60 seconds
void Main()
{
var timeStorage = new TimeStorage();
var item = "Hello World";
timeStorage.Add(item);
var items = timeStorage.GetItems();
items.Dump();
}
@JamieDixon
JamieDixon / gist:8422709
Last active January 3, 2016 06:29
Want to force the update of a binding because a non observable dependency has changed? KABOOM! Nastyness
// Force all properties bound to the price data binding to reevaluate
function forceBindingRefresh(bindingName, element) {
var bindings = $(element).data("bind");
var koElement = ko.dataFor(element);
if (bindings.indexOf(bindingName) >= 0) {
// Lets split up the bindings so we can deal with each one
bindings = bindings.split(",");
@JamieDixon
JamieDixon / gist:9661702
Created March 20, 2014 11:20
Calculating with functions
function zero(exp) { return numberExpression(0, exp); }
function one(exp) { return numberExpression(1, exp); }
function two(exp) { return numberExpression(2, exp); }
function three(exp) { return numberExpression(3, exp); }
function four(exp) { return numberExpression(4, exp); }
function five(exp) { return numberExpression(5, exp); }
function six(exp) { return numberExpression(6, exp); }
function seven(exp) { return numberExpression(7, exp); }
function eight(exp) { return numberExpression(8, exp); }
function nine(exp) { return numberExpression(9, exp); }
function hamming (n) {
// TODO: Program me
var hammedNumbers = [];
var startIndex = 1;
while(hammedNumbers.length < n)
{
var primeFactors = calcPrimeFactors(startIndex);
var largestPrime = primeFactors[primeFactors.length - 1];
var moveZeros = function (arr) {
var zeros = [];
var foo = arr.filter(function(value) {
var isZero = value === 0;
if(isZero) { zeros.push(0); }
return value !== 0;
})
return foo.concat(zeros);
}
var anarchy = function () {
var len = "htgnel".split("").reverse().join("");
return 'I can write numbers like, ' + ["."][len] + ", " + [".","."][len] + ", " + [".",".","."][len] + ".";
}
function once(fn) {
var isUsed = false;
return function(){
if(!isUsed)
{
isUsed = true;
var arg = Array.prototype.slice.call(arguments, 0).reverse()[0];
fn(arg);
return arg;
}
function once(fn) {
var isUsed = false;
return function(param){
if(!isUsed)
{
isUsed = true;
fn(param);
return param;
}
}
@JamieDixon
JamieDixon / gist:46dc2fe92b143fcc7a6c
Created May 8, 2014 09:15
HTML5 Validation Module
/*jslint devel: true */
/*jslint browser: true, nomen: true */
/*global jQuery, cf*/
(function IIFE($, cf) {
"use strict";
cf.FormValidation = function formValidation(formSelectors) {
var self = this;
function html5ValidationSupported() {