Skip to content

Instantly share code, notes, and snippets.

View sghall's full-sized avatar
🥊

Steve Hall sghall

🥊
  • San Francisco, CA
View GitHub Profile
@sghall
sghall / gist:7859113
Last active August 3, 2018 11:37
Helper function for creating a d3 chord diagram
// blog-post - http://www.delimited.io/blog/2013/12/8/chord-diagrams-in-d3
//*******************************************************************
// CHORD MAPPER
//*******************************************************************
function chordMpr (data) {
var mpr = {}, mmap = {}, n = 0,
matrix = [], filter, accessor;
mpr.setFilter = function (fun) {
filter = fun;
@sghall
sghall / gist:8167665
Created December 29, 2013 05:13
An example of using D3 Hexbins and Leaflet Maps. See blog post listed at the top for details and working example.
// See blog post for full details http://www.delimited.io/blog/2013/12/1/hexbins-with-d3-and-leaflet-maps
//**********************************************************************************
//******** LEAFLET HEXBIN LAYER CLASS *********************************************
//**********************************************************************************
L.HexbinLayer = L.Class.extend({
includes: L.Mixin.Events,
initialize: function (rawData, options) {
this.levels = {};
this.layout = d3.hexbin().radius(10);
this.rscale = d3.scale.sqrt().range([0, 10]).clamp(true);
@sghall
sghall / gist:8167693
Created December 29, 2013 05:18
Creating Force Bubble Charts in D3 with dynamic numbers of categories. See blog pos list at the topt for full details and working example.
//Blog post with working examples: http://www.delimited.io/blog/2013/12/19/force-bubble-charts-in-d3
d3.csv('data/fuel.csv', function (error, data) {
var width = 1000, height = 1000;
var fill = d3.scale.ordinal().range(['#827d92','#827354','#523536','#72856a','#2a3285','#383435'])
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
_.each(data, function (elem) {
@sghall
sghall / gist:6ced6002e2fa3e442c08691c8d03142d
Created January 18, 2017 07:22
Manual Square Root in JavaScript
function sqrt(num, guess) {
guess = guess || num / 3;
if (Math.abs(num - guess * guess) < 5e-15) {
return guess;
} else {
return sqrt(num, (num / guess + guess) / 2);
}
};
@sghall
sghall / gist:441e2ab9bb45934822b1d4c769fe1e14
Created January 18, 2017 07:37
Fizz Buzz in JavaScript
function fizzBuzz(num) {
for (let i = 1; i <= num; i++) {
const fizz = i % 3 === 0 ? 'Fizz': '';
const buzz = i % 5 === 0 ? 'Buzz': '';
console.log(fizz || buzz ? fizz + buzz: i)
}
};
@sghall
sghall / gist:dc6884adf1e1ffce769a1349008a3ea7
Created January 18, 2017 08:39
Breadth First Traversal and Depth First Traversal in JavaScript
//DFS
Tree.prototype.traverse = function (callback) {
var stack=[this];
var n;
while(stack.length>0) {
n = stack.pop();
callback(n.value);
@sghall
sghall / gist:a06a92e6ec1a3bc02ea3ed974634b127
Created January 19, 2017 06:35
Is Prime Number JavaScript
function isPrime(num) {
for (var i = 2; i < Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
@sghall
sghall / gist:b01f1da5c2b48245e7814b6ef56b59fa
Created January 22, 2017 00:53
Insertion Sort in JavaScript
const insertionSort = arr => {
const len = arr.length;
let i, j, temp;
for (i = 1; i < len; i++) {
j = i;
temp = arr[i];
while (j > 0 && arr[j - 1] > temp) {
arr[j] = arr[j - 1];
@sghall
sghall / gist:f2fd434c1e4787ec77350da6a8efd4a6
Created January 22, 2017 03:53
Bubble Sort in JavaScript
const swap = (arr, index1, index2) => {
[arr[index1], arr[index2]] = [arr[index2], arr[index1]];
};
const bubbleSort = (arr) => {
const len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0, k = len - 1 - i; j < k; j++) {
if (arr[j] > arr[j + 1]) {
@sghall
sghall / gist:8586ff24aa28d03b1484309d00719e18
Last active November 27, 2017 06:58
Merge Sort in JavaScript
const merge = (arrL, arrR) => {
const result = [];
let cursorL = 0;
let cursorR = 0;
while (cursorL < arrL.length && cursorR < arrR.length) {
if (arrL[cursorL] < arrR[cursorR]) {
result.push(arrL[cursorL++]);