Skip to content

Instantly share code, notes, and snippets.

View jremmen's full-sized avatar

John Remmen jremmen

  • Planning Center
  • San Diego
View GitHub Profile
@jremmen
jremmen / random_generators.rb
Created April 20, 2015 03:31
Some random generators in ruby based on Coursera reactive programming course
module Generators
module Utils
def self.included(other)
other.extend(ModuleMethods)
end
module ModuleMethods
def arbitrary
@arbitrary ||= {}.tap do |o|
o[Integer] = ->{Random.rand(-1e+10..1e+10).to_i}
@jremmen
jremmen / iterator.js
Last active August 29, 2015 14:19
lazy iterator
var Iterator = function(m, n) {
this.start = m;
this.end = n || Infinity;
this.step = 1;
this.current = this.start;
this.queue = new IteratorQueue();
this.next = function() {
var c = this.current;
this.current += this.step;
@jremmen
jremmen / set.js
Last active August 29, 2015 14:17
js: recursive 2 set operations
function diff(a, b) {
return _operation(a, b, [], diff, true)
}
function union(a, b) {
return _operation(a, b, b, union, true)
}
function intersect(a, b) {
return _operation(a, b, [], intersect)
@jremmen
jremmen / gist:ff775efa98e58813fa3b
Last active August 29, 2015 14:16
js: prime factorization examples
prime_factorization = function(n) {
var i = 2;
var factors = [];
while (n > 0 && i <= n) {
while (n % i == 0) {
factors.push(i);
n = n / i;
}
i++;
}
@jremmen
jremmen / nqueens.js
Last active August 29, 2015 14:03
js: n Queens solver
Queens = function(num) {
function range(n) {
return Array.apply(null, new Array(n)).map(function(_, i) { return i; });
};
function is_safe(q, qs) {
if(qs.length == 0) return true;
return qs.filter(function(x, i) {
return Math.abs(q - x) != Math.abs(qs.length - (qs.length - (i + 1)));
@jremmen
jremmen / submax.js
Created July 2, 2014 23:14
js: maximum sub array in linear time
// O(n)
Array.prototype.submax = function() {
var max = sum = 0;
for(var i = 0, l = this.length; i < l; i++) {
sum = Math.max(sum + this[i], 0);
max = Math.max(max, sum);
}
return max;
}
@jremmen
jremmen / submax.js
Created July 2, 2014 22:36
js: maximum sub array solution using divide and conquer
// O(n log n)
Array.prototype.submax = function() {
if(this.length === 0) return 0;
if(this.length === 1) return this[0];
var m = Math.floor(this.length / 2);
var max_left = find_max(this, m - 1, 0);
var max_right = find_max(this, m, this.length);
@jremmen
jremmen / jquery.zoomblo.js
Last active August 29, 2015 14:02
jquery: zoomblo, light weight image zoomer
(function($) {
$.fn.zoomblo = function(full) {
$(this).css({
position: 'relative',
overflow: 'hidden'
});
var org = {
@jremmen
jremmen / jquery.tripleclick.js
Last active August 29, 2015 14:02
jquery: tripleclick event
(function($) {
$.event.special.tripleclick = {
clicked: 0,
time: 0,
threshold: 80,
setup: function() {
$(this).on('click', $.event.special.tripleclick.handler);
@jremmen
jremmen / multimap_multireduce.js
Last active August 29, 2015 14:02
js: multimap and multireduce array methods
Array.prototype.multimap = function(f) {
return this.length == 0 ? [] : [(this[0] instanceof Array ? this[0].multimap(f) : f(this[0]))].concat(this.slice(1).multimap(f));
}
Array.prototype.multireduce = function(f, z) {
return this.length == 0 ? z : f((this[0] instanceof Array ? this[0].multireduce(f, z) : this[0]), this.slice(1).multireduce(f, z));
}