Skip to content

Instantly share code, notes, and snippets.

View bioball's full-sized avatar

Daniel Chao bioball

View GitHub Profile
@bioball
bioball / permutation.rb
Last active December 25, 2015 07:19
This is a function I wrote that takes an array, and returns all possible permutations of arrays of that same size.
def permute(arr)
permutation(arr.sort)
end
def permutation(arr, result=[])
k = nil
result += [arr.dup]
(arr.length-1).times do |i|
if arr[i] < arr[i+1]
k = i
@bioball
bioball / permutation.js
Last active December 25, 2015 07:29
This is a Javascript function that takes an array, and returns all possible permutations of the same array.
function permute(arr){
return permutation(arr.sort());
}
function permutation(arr, result){
result = typeof result !== 'undefined' ? result : [];
result = result.concat([arr]);
//find index k
var k = null;
@bioball
bioball / b-tree.js
Last active December 28, 2015 18:09
This is a class that creates a b-tree of order 3. Learn more about b-trees here: http://en.wikipedia.org/wiki/B-tree. I don't know what the practical use of this in JavaScript is, but feel free to use this if you need a b-tree in your application.
///////////////////////////
//// B-TREE OF ORDER 3 ////
///////////////////////////
var makeBTree = function(){
var newBTree = {};
var root;
//////////////////////////
@bioball
bioball / nqueens.js
Last active December 29, 2015 03:29
This might just be the fastest possible non-bitwise JavaScript algorithm for the N Queens puzzle http://en.wikipedia.org/wiki/Eight_queens_puzzle. This was written in collaboration with githib.com/stites
var nQueens = function(n){
if(n === 1){ return 1; }
n = n - 1;
var
m = Math.ceil(n/2),
count = 0,
col = new Int8Array(n + 1),
maj = new Int8Array(n + n + 1),
@bioball
bioball / jwt-middleware.js
Last active August 29, 2015 14:01
JWT verification middleware example
// This is an example express middleware that can be inserted into any route that needs to be protected, assuming that authentication is done via JSON web tokens
var crypto = require('crypto');
var verifyJWTToken = function(req, res, next){
var secret = process.env.TOKEN_SECRET;
// 1. get the token from the headers. I'm going to go ahead and split up the three parts already
var token = req.headers['x-access-token'].split('.');
// 2. Get the signature, and the rest of the token. Calculate the digest with HMAC-SHA256 so we can see if it matches the signature
@bioball
bioball / inheritence.js
Created June 17, 2014 19:56
Inheritance patterns with constructor functions
// here's a normal JavaScript constructor function
var Block = function(){
this.width = 30;
this.height = 50;
this.otherthing = "whateverelse";
};
// these are class methods that all blocks inherit from
Block.prototype.doThing = function(){
doctype html
html
head
title Nothing wrong with using Jade with Angular!
body(ng-app="app")
div(ng-controller="fooController")
h1 {{ title }}
p {{ body }}
@bioball
bioball / authentication.js
Created February 12, 2015 06:12
Angular AUTH example
angular.module('app')
.factory('authentication', [
'user',
function(user){
return {
authentication: function(){
return user.identify();
}
};
}
@bioball
bioball / authentication.js
Created February 12, 2015 06:12
Angular AUTH example
angular.module('app')
.factory('authentication', [
'user',
function(user){
return {
authentication: function(){
return user.identify();
}
};
}
@bioball
bioball / authentication.js
Last active August 29, 2015 14:15
Angular AUTH example
angular.module('app')
.factory('authentication', [
'user',
'$state',
function(user, $state){
return {
authenticate: function(protect){
return user.identify()
.then(function(){
return true;