Skip to content

Instantly share code, notes, and snippets.

View maxdignan's full-sized avatar

Max Dignan maxdignan

View GitHub Profile
@maxdignan
maxdignan / trial.js
Created July 11, 2015 16:37
Test Driven Interview Question CMM
function withFor(list) {
var sum = 0;
list.forEach( function(item) {
if (typeof item === 'number') {
sum += item;
}
});
return sum
};
@maxdignan
maxdignan / trials2.js
Created July 11, 2015 16:56
TDD Interview Q 2 CMM
(function tester(func) {
console.log(func([],[]), 'should be []');
console.log(func([1,2],[1,2]), 'should be [1,1,2,2]');
console.log(func([1,2],[1]), 'should be [1,1,2]');
console.log(func(['hi'],[undefined, 'lo']), 'should be [hi,undefined, lo]');
console.log(func([2,3],[]), 'should be [2,3]');
console.log(func([],[2,'und', 5]), 'should be [2,und,3]');
})(function concat(arr1, arr2){
var catted = [];
var iter = 0;
@maxdignan
maxdignan / HTTPGet.js
Created July 18, 2015 17:08
Vanilla JS HTTP GET Request
function httpGet(theUrl, callback, callbackContext, errorCallback) {
var req = new XMLHttpRequest();
req.open("GET", theUrl, true);
req.onreadystatechange = (function(){
if (req.readyState == 4 && (req.status >= 200 && req.status < 300)){
callback.call(callbackContext);
} else if (req.readyState == 4) {
errorCallback();
}
});
@maxdignan
maxdignan / ArrayBuilder.js
Last active October 18, 2015 21:15
ArrayBuilder.js
var arrayBuilder = function(){
return {
arr: [],
addElem: function(element){
this.arr.push(element);
},
getArray: function(){
var c = this.arr;
this.clearArray();
@maxdignan
maxdignan / DependencyInjection.js
Last active December 2, 2015 19:15
Library to make Injectable functions with Dev-defined resource context
var DI = function(){
var throwDelayedError = function(typeOfErr, err){
setTimeout(function(){
throw typeOfErr + ': ' + err;
}, 0);
};
var injectables = {};
@maxdignan
maxdignan / async_handler.rb
Last active January 10, 2016 01:45
Async handler to manage multiple db calls within a Rails Controller Action concurrently, without any hassle
def async_handler(*args)
dass = self
threads = []
args.each do |t|
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
dass.send t
end
end
end
@maxdignan
maxdignan / FileStreaming.js
Created April 26, 2016 20:53
FileStreamingNode
var Iconv = require('Iconv').Iconv;
var fs = require('fs');
var start = Date.now();
function runner(i){
rStream = fs.createReadStream('enwik8');
var iconv = new Iconv('UTF-8', 'UTF-16LE');
wStream = fs.createWriteStream('enwik16');
return rStream.pipe(iconv).pipe(wStream);
@maxdignan
maxdignan / ExistentialQQ.rb
Last active June 3, 2016 18:29
Ruby Existential Check
class Object
def QQ
if !self.nil?
return self
end
Qe.new
end
end
class Qe
class YourFirstClass
def initialize(word, num_a, num_b, num_c)
@word = word
@num_a = num_a
@num_b = num_b
@num_c = num_c
end
def hello(name)
"Hello #{name}"
require 'ostruct' #needed, but available in Ruby.
os = OpenStruct.new
os.method_name = "here's your value you want"
os.method_name #-> "here's your value you want"