Skip to content

Instantly share code, notes, and snippets.

var mongoose = require("mongoose"),
connection = mongoose.connection,
gracefulExit = function() {
connection.close(function() {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
};
mongoose.connect(process.env.MONGO_URI);
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
sys.puts(stdout)
}
exec("ls -la", puts);
@JustinMorgan
JustinMorgan / TestShuffle.aspx
Created October 19, 2015 20:39
Shuffle Visualizer: Visual sanity-check for randomness in deck-shuffling algorithms (C#/ASP.NET WebForms version)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestShuffle.aspx.cs" Inherits="TestShuffle" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test shuffle</title>
<style>
body {
@JustinMorgan
JustinMorgan / Shuffle.cs
Created October 19, 2015 20:41
Deck shuffling puzzle: C# tester boilerplate
public static class Shuffle
{
static Random r = new Random();
public static void swap(ref int a, ref int b)
{
if (a != b)
{
a ^= b;
b ^= a;
@JustinMorgan
JustinMorgan / weekNumber.js
Last active October 20, 2015 21:50
Which number week of the year is it?
function weekNumber(date) {
var currYear = date.getFullYear();
var jan1 = new Date(currYear,0,1);
return Math.ceil((((date - jan1) / 86400000) + jan1.getDay() + 1)/7);
}
function weekNumberUTC(date) {
var currYear = date.getUTCFullYear();
var jan1 = new Date(currYear,0,1);
return Math.ceil((((date - jan1) / 86400000) + jan1.getUTCDay() + 1)/7);
}
@JustinMorgan
JustinMorgan / DictionaryExtensions.cs
Created December 1, 2015 00:03
ForEach and Zip for IDictionary
using System;
using System.Collections.Generic;
using System.Linq;
namespace Extensions
{
public static class DictionaryExtensions
{
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
{
@JustinMorgan
JustinMorgan / shuffle-visualizer.coffee
Last active February 22, 2016 19:21
Shuffle Visualizer: Visual sanity-check for randomness in deck-shuffling algorithms (JS and CoffeeScript versions)
#Usage:
#1. Declare your shuffle() function. No parameters necessary, since `deck` is a global variable.
#2. Run `test()`.
deckSize = 52
deck = [0...deckSize]
rand = (min = 0, max = deckSize) ->
Math.floor(Math.random() * (max - min)) + min
//--------------------------- Context-Binding Lambda Expressions ------------------------------------
// Expression bodies
var evens = [2,4,6,8,10];
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var fives = [];
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
@JustinMorgan
JustinMorgan / JsonDumper.cs
Last active January 17, 2017 21:25
Dump complex object to JSON (quick & dirty approximation of JavaScript's console.log)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Text;
public static class JsonDumperSimple
{
public static string Stringify(object obj)
documentation?("jQuery.fn.order", {
Description: "jQuery plugin for client-side list sorting"
Usage: (foo, selector = null, sortBy = null, options = null) -> [
$(foo).order(selector, sortBy, options)
$(foo).order(selector, sortBy)
$(foo).order(selector, options)
$(foo).order(options)
$(foo).order()
]
Parameters: