Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Mark S. Everitt qubyte

View GitHub Profile
@qubyte
qubyte / walker.py
Created September 23, 2012 04:45
Simple quantum random walk
"""
Requires matplotlib for plotting. Tested with python 27. If you want to try this
without plotting, remove the final two lines and the pylab import. The guts only
depends on math and will work with vanilla python.
"""
import math
import pylab
def probabilities(posn):
@qubyte
qubyte / asyncSeries.js
Created September 24, 2012 16:39
Simple series execution for asynchronous functions.
/*
* Inspired by the async module, but only needing async.series, I decided to code something minimal.
*
* funcs is an array of functions taking a single argument (a callback). It is assumed that these functions
* operate with/on data outside of the local scope. The functions thus form a series of steps. cb is a
* a final callback, which is called if a function in the series is handed an error (zeroth argument of any
* function in the series as per convention) or otherwise when all functions in the array have been called.
* Functions are called in order, from zeroth element up.
*
* The original async.forEachSeries assembles other callback arguments into an array. This can be easily
@qubyte
qubyte / StandardDeviation.js
Created November 13, 2012 08:51
Calculate a running standard deviation.
// A standard deviation object constructor. Running deviation (avoid growing arrays) which
// is round-off error resistant. Based on an algorithm found in a Knuth book.
function StandardDeviation(firstMeasurement) {
this.workData = firstMeasurement;
this.lastWorkData = null;
this.S = 0;
this.count = 1;
}
// Add a measurement. Also calculates updates to stepwise parameters which are later used
@qubyte
qubyte / asyncForEachSeries.js
Last active December 12, 2015 03:18
An asynchronous for each series runner. Feed this a contiguous array of functions that accept a single (callback([error])) argument.
// Simple asynchronous forEach.
function forEachSeries(list, func, callback) {
var listIndex = 0;
function doOne(err) {
if (err) {
return callback(err);
}
var thisElem = list[listIndex];
@qubyte
qubyte / chromify.md
Last active December 14, 2015 03:18
Ever wanted to wrap your node.js console output in some visual chrome? This function automates it for you!

Description

Ever wanted to wrap your node.js console output in some visual chrome? This function automates it for you! It indents your text by one space, and centres the chrome above and below. Even for multiline strings the chrome length will be properly calculated using ninja disco rocket boots (or Math.max.apply on the array if the rocket boots are on loan). As a bonus, the third parameter takes a character to put at the start of your string, like an arrow or something.

The function

/**
 * Wrap a sting with visual chrome above and below. Automatic length.
 *
 * @param  {String} basic      A basic string to wrap. No whitespace padding needed.
 * @param  {String} chromeChar A character to use for the chrome.
@qubyte
qubyte / humanJshintReporter.js
Last active August 2, 2016 12:41
The default JSHint output is somewhat wasteful, and very bland, making it hard for a human to parse. The following does IMO a better job and colours the output for you. This requires the `colors` package, available via npm. If you don't want the dependency, just remove the color commands from strings. MIT licence.
/**
* The default JSHint output is somewhat wasteful, and very bland, making it hard for a human to
* parse. The following does IMO a better job and colours the output for you. This requires the
* `colors` package, available via npm. If you don't want the dependency, just remove the color
* commands from strings.
*
* Usage: When invoking jshint from the command line, point it to this file with the --reporter
* flag. e.g.
*
* jshint someFile.js --config /path/to/config.cfg --reporter /path/to/jshintReporter.js
@qubyte
qubyte / haversine.js
Created May 28, 2013 17:12
Haversine formula for stable distance calculation between pairs of coordinates.
// All angles in radians.
function haversine(aLong, aLat, bLong, bLat) {
var R = 6371; // Radius of Earth.
var latDiff = bLat - aLat;
var longDiff = bLong - aLong;
var step1 = Math.sin(latDiff / 2);
var step2 = Math.sin(longDiff / 2);
var step3 = step1 * step1 + step2 * step2 * Math.cos(aLat) * Math.cos(bLat);
@qubyte
qubyte / Gruntfile.js
Last active December 20, 2015 21:29
My Gruntfile.
/**
* Mark S Everitt 2013
* Licence: MIT
*/
var jshintConfig = {
files: ['gruntfile.js', 'lib/**/*.js', 'test/**/*.js', 'index.js'],
options: {
// Just an example.
globals: {
@qubyte
qubyte / nest.js
Created October 10, 2013 09:55
Nest one asynchronous function in another. Both functions are optional. MIT licence.
function nest(before, after) {
'use strict';
if (typeof before !== 'function') {
// Only after is defined, so just return it.
if (typeof after === 'function') {
return after;
}
// Neither function is defined, so return a function that just executes a callback.
@qubyte
qubyte / styleGuide.md
Created November 5, 2013 12:43
Qubyte's JavaScript style guide

Style guide

Well written code by several authors should look like well written code by one author. To this end, everyone should write code in the same style and formatting. This serves multiple purposes, and it should be pretty clear what the benefits of consistent style with no arguments are.

This is the guide for the Entrago CMS backend codebase.

Basic formatting