Skip to content

Instantly share code, notes, and snippets.

@jabney
jabney / PriorityQueue.js
Last active September 17, 2017 17:53
A heap-based priority queue in JavaScript
// PriorityQueue.js MIT License © 2014 James Abney http://github.com/jabney
(function(ex) {
'use strict';
// ---------------------------------------------------------------
// PriorityQueue Constructor
// A heap-based priority queue.
// ---------------------------------------------------------------
ex.PriorityQueue = function PriorityQueue(compare) {
var compare = compare || function(a, b) { return a < b; },
@jabney
jabney / shuffle.js
Last active August 29, 2015 14:11
Shuffle an array in place so that the positions of its values are random
// shuffle.js © 2014 James Abney http://github.com/jabney
(function(ex) {
'use strict';
// Export shuffle to 'random' namespace.
var random = ex.random || (ex.random = Object.create(null));
// Shuffle an array in place so that the positions of its values are random.
random.shuffle = function shuffle(a) {
var i, r, tmp, len = a.length;
@jabney
jabney / setOps.js
Last active February 1, 2023 10:55
Fast JavaScript set operations: union, intersection, difference, complement, and equals. Includes support for objects.
// setOps.js MIT License © 2014 James Abney http://github.com/jabney
// Set operations union, intersection, symmetric difference,
// relative complement, equals. Set operations are fast.
(function(so) {
'use strict';
var uidList = [], uid;
// Create and push the uid identity method.
@jabney
jabney / encode.js
Last active August 29, 2015 14:10
Encode an object's type as a numeric value.
// encode.js MIT License © 2014 James Abney http://github.com/jabney
// Encode built-in type as a numeric value.
//
// typeOf(null); // => 'Null'
// typeOf(undefined); // => 'Undefined'
// typeOf([]); // => 'Array'
//
// encodeType(null); // => 1
// encodeType(undefined); // => 2
@jabney
jabney / declare.js
Last active August 29, 2015 14:10
An experiment with "forward type checking" in JavaScript. Execute a method based upon an object's type.
// declare.js MIT License © 2014 James Abney http://github.com/jabney
// An experiment with "forward type checking". Register types
// using 'declare' along with corresponding methods. The methods
// get called depending upon the type of the passed object.
//
// // Create a new declare object.
// var logByType = fwdType.declare();
//
// // Register types and methods.
@jabney
jabney / entropy.js
Last active January 29, 2024 23:38
Javascript implementation of a Shannon entropy calculation in bits per symbol
// entropy.js MIT License © 2014 James Abney http://github.com/jabney
/***************************************
* ES2015
***************************************/
// Shannon entropy in bits per symbol.
function entropy(str) {
const len = str.length