This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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+/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PriorityQueue = function (fn) { | |
var queue = []; | |
return { | |
push: function (a) { | |
queue.push(a); | |
queue.sort(fn); | |
return this; | |
}, | |
shift: function () { | |
return queue.shift(); |
NewerOlder