Skip to content

Instantly share code, notes, and snippets.

View nefarioustim's full-sized avatar

Tim Huegdon nefarioustim

View GitHub Profile
@nefarioustim
nefarioustim / memoize.js
Created November 30, 2011 22:41
JavaScript Memoization
function memoize(fn) {
return function() {
var args = Array.prototype.slice.call(arguments),
hash = "",
i = args.length;
currentArg = null;
while (i--) {
currentArg = args[i];
hash += (currentArg === Object(currentArg)) ?
JSON.stringify(currentArg) : currentArg;
@nefarioustim
nefarioustim / profile-loop.js
Created November 30, 2011 14:57
Homespun JavaScript profiling
for (
var start = new Date().getTime(),
iterationCount = 0,
millisec = 0;
millisec < 1000;
iterationCount++
) {
// Your profiled code
// goes here
@nefarioustim
nefarioustim / throttle.js
Created June 28, 2011 16:01
Throttle a function
function throttle(fn, delay) {
var timer = null;
return function () {
var context = this,
args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
@nefarioustim
nefarioustim / project-euler-problem-2.js
Created June 5, 2011 14:55
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
for(
var sum = 0, f1 = 1, f2 = 2, next;
f2 < 4E6;
f2 & 1 || (sum += f2), next = f1 + f2, f2 = (f1 = f2, next)
);
console.log(sum);
@nefarioustim
nefarioustim / project-euler-problem-1.2.js
Created June 5, 2011 12:37
Find the sum of all the multiples of 3 or 5 below 1000.
// Do the maths
for(
var sum = 0, i = 1;
i < 1000;
!(i % 3 && i % 5) && (sum += i), i++
);
// Log the result
console.log(sum);
@nefarioustim
nefarioustim / getUTCDateTime.js
Created April 11, 2011 09:12
Prototype a method onto the Date object that returns a UTCDateTime string.
Date.prototype.getUTCDateTime = function() {
var zf = function(num) {
return ((num + 100) + '').substr(1);
};
return [
this.getUTCFullYear(),
'-',
zf(this.getUTCMonth() + 1),
'-',
import urllib
try:
from urlparse import parse_qs
except ImportError:
from cgi import parse_qs
def url_params(url, **kwargs):
bits = url.split('?')
query_vars = {}
if len(bits) > 1:
from django.conf.urls.defaults import *
from blog.models import Category, Post
post_info_dict = {
'queryset': Post.pub.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('blog.views',