Skip to content

Instantly share code, notes, and snippets.

@nubunto
nubunto / time.js
Last active August 29, 2015 14:17
simple and extensible time manipulation
var time = (function() {
var timeout = function(func, t) {
function f() {
// call the function and get the result
var r = func.call(null);
// should we recurse?
if(r) setTimeout(f, t);
}
@nubunto
nubunto / index.js
Created March 6, 2015 03:59
database server, for the hacker school interview.
// require the http module
var http = require('http');
// require the util module to format things
var util = require('util');
//the get regex matches /get and whatever and the set regex matches /set and whatever
var getRegex = /\/get.*/, setRegex = /\/set.*/, database = {};
// create a server with the 'handler' function and start listening on 4000
@nubunto
nubunto / compose.js
Last active August 29, 2015 14:13
A implementation of the "compose" function that takes a arbitrary number of functions.
var compose = function () {
//take the functions passed in as an array.
var fns = [].slice.call(arguments, 0);
//returns a function that will combine all of them
return function (arg) {
return fns.reduce(function (retValue, next) {
return next.call(null, retValue);
}, arg); //the initial value will be the argument
};
};
@nubunto
nubunto / manjs.js
Created December 27, 2014 20:05
MANJS, a Simple Documentation Generator.
//the regexes of manjs.
//the 'manjs' regex parses anything that is preceded by an at-sign,
//next, the 'annotation_name' regex breaks that in two parts, and gets what's after the at-sign,
//and the 'annotation_type' regex gets the type of annotation.
var regexes = {
manjs: /(@\w+)\s(.+)/g,
annotation_name: /@\w+\s(.+)/,
annotation_type: /@\w+/
}
@nubunto
nubunto / coming_from_js.clj
Last active August 29, 2015 13:56
A simple demonstration of why Lisp languages are AWESOME!
;; Let's assume you are coming from Javascript and wants to mess with Clojure.
;; In clojure, we can define functions and stuff in two ways: using the "def" function:
(def foo (fn [x] (* x 2)))
;; It assigns an anonymous function (the part after "fn") to the name "foo". This function is simple
;; and only multiplies it's argument by 2.
;; Or, you can use defn, like this:
(defn foo [x] (* x 2))
@nubunto
nubunto / map_reduce.js
Last active December 30, 2015 12:39
map/reduce/each functionality, with a example.
var type, each, filter, map, reduce;
type = function (arg) {
//filter out the type of the object, by getting the [[Class]] property.
return Object.prototype.toString.call(arg).slice(8, -1).toLowerCase();
};
each = function (arg, fn) {
var typef = type(arg); // get the type of the argument, but the real one.
var i, len = (arg && arg.length), value;
if(typef === 'array') { //if we're seeing an array,
for (i = 0, len = arg.length; i < len; i++) {
@nubunto
nubunto / PriorityQueue.js
Last active December 29, 2015 03:29
Priority Queue implemented in JavaScript. It is a closure, that returns an object, with push, shift, and length. It's first argument is a function that sorts the queue as you add elements to it. It allows successive method calls.
var PriorityQueue = function (fn) {
var queue = [];
return {
push: function (a) {
queue.push(a);
queue.sort(fn);
return this;
},
shift: function () {
return queue.shift();