Skip to content

Instantly share code, notes, and snippets.

View spencerwi's full-sized avatar

Spencer Williams spencerwi

View GitHub Profile
@spencerwi
spencerwi / gist:4180430
Created December 1, 2012 03:30
Python vimrc options
filetype plugin indent on
syntax on
set autoindent
set expandtab
set shiftwidth=4
set tabstop=4
set smarttab
SELECT
Name, DoB, (current_date - DoB) as Age
FROM
People
WHERE
Name = 'John Smith'
;
let person = filter (\x -> name x == "John Smith") people
age dob = getCurrentTime >>= return . (\now -> (diffDays now dob) `div` 365) . utctDay
in
[name person, dob person, age (dob person)]
// BAD:
List<PersonEntity> pes = someFetcherFunction.findByAgeGreaterThan(18);
if (pes.contains(currentPE)) { return "Hello, sir/madam."; }
else { return "This method is for adults only!"; }
// GOOD:
static Integer LEGAL_ADULTHOOD_AGE = 18; // in class PersonEntity
//...
List<PersonEntity> attendees = conference.getAttendees();
Collections.sort(new Comparator<PersonEntity> {
@Override
public int sort(PersonEntity person1, PersonEntity person2) {
Date person1ArrivalDate = person1.getArrivalDate(),
person2ArrivalDate = person2.getArrivalDate();
if (person1ArrivalDate == null) person1ArrivalDate = DEFAULT_ARRIVAL_DATE;
if (person2ArrivalDate == null) person2ArrivalDate = DEFAULT_ARRIVAL_DATE;
int comparisonByArrivalDateResult = person1ArrivalDate.compareTo(person2ArrivalDate);
if (comparisonByArrivalDateResult == 0) {
@spencerwi
spencerwi / gist:6531144
Last active December 22, 2015 21:09
for a blog post
List<PersonEntity> attendees = conference.getAttendees();
Collections.sort(new Comparator<PersonEntity> {
@Override
public int sort(PersonEntity person1, PersonEntity person2) {
final int BOTH_ARE_EQUAL = 0;
int comparisonByArrivalDateResult = this.compareByArrivalDate(person1, person2);
if (comparisonByArrivalDateResult == BOTH_ARE_EQUAL) {
int comparisonByTicketTierResult = this.compareByTicketTier(person1, person2);
if (comparisonByTicketTierResult == BOTH_ARE_EQUAL) {
return this.compareByLastName(person1, person2);
@spencerwi
spencerwi / tsvtocsv.hs
Last active December 23, 2015 00:49
Convert TSV on stdin to CSV on stdout
import Data.List
import Text.Regex
encloseField :: String -> String
encloseField field
| "," `isInfixOf` field = "\"" ++ (escapeField field) ++ "\""
| otherwise = escapeField field
escapeField :: String -> String
escapeField field
@spencerwi
spencerwi / underscore-shell.js
Last active December 24, 2015 17:29
Start a quick Javascript repl with underscore preloaded using nodejs. Requires nodejs and npm-installed underscore.
var fs = require('fs');
var repl = require('repl');
var __ = require('underscore')._;
var files = {};
var jsonFiles = process.argv.slice(2);
jsonFiles.forEach(function(val) {
files[val] = JSON.parse(fs.readFileSync(val));
});
@spencerwi
spencerwi / docfrag.js
Last active December 28, 2015 06:09
Buffering DOM updates using DocumentFragments
/* Assumption: arrayOfElements is an array of DOM nodes created from a template or otherwise. */
/* Don't do this! This reflows the DOM a *lot* */
var targetNode = document.getElementById('target');
var appendToTargetNode = targetNode.appendChild.bind(targetNode);
arrayOfElements.forEach(appendToTargetNode);
/* Instead, use a DocumentFragment */
@spencerwi
spencerwi / displayNone.js
Created November 13, 2013 20:21
Poor-man's DocumentFragment using display:none
/* Again, the assumption: arrayOfElements is an array of DOM nodes created from a template or otherwise. */
/* We can set our targetNode to "display:none", change it, and then reset it, making it into a poor man's DocumentFragment */
var targetNode = document.getElementById('target');
var previousDisplayValue = targetNode.style.display;
targetNode.style.display="none"; /* 1 reflow */
var appendToNode = targetNode.appendChild.bind(targetNode);
arrayOfElements.forEach(appendtoTargetNode); /* 0 reflows */