Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / overload.js
Last active December 19, 2015 12:49
Function.prototype.addMethod = function(name, method) {
var existingMethod = this.prototype[name];
this.prototype[name] = function() {
if (method.length == arguments.length) {
return method.apply(this, arguments);
} else if (typeof existingMethod == "function") {
return existingMethod.apply(this, arguments);
}
@reu
reu / debounce.js
Last active December 22, 2015 23:19
Returns a function that as long as it continues to be called, it will not be invoked. It will only be called after `time` milliseconds.
function debounce(fn, time) {
var time = time || 300,
scope = this,
delay;
return function() {
var args = arguments;
if (delay) clearTimeout(delay);
delay = setTimeout(function() {
@reu
reu / angular-autoscroll.js
Created October 10, 2013 04:42
Angular.js autoscroll directive.
angular.module("autoscroll", [])
.service("scroller", function() {
this.defaultTime = 500;
this.scroll = function(element, height, time) {
element.animate({
scrollTop: height
}, time || this.defaultTime);
}
@reu
reu / line-reader.js
Created January 4, 2014 16:38
How to read a file line by line in Node.js using the yet unstable Readline library (http://nodejs.org/api/readline.html)
var fs = require("fs"),
readline = require("readline");
var reader = readline.createInterface({
input: fs.createReadStream("large-file.txt"),
output: fs.createWriteStream("/dev/null"),
terminal: false
});
reader.on("line", function(line) {
@reu
reu / gulp-self-enclosing.js
Created April 16, 2014 17:35
Self enclosing function for each file
var es = require("event-stream");
module.exports = function() {
return es.map(function(file, callback) {
file.contents = new Buffer("(function() {\n" + String(file.contents) + "\n})();");
callback(null, file);
});
};
# This class implements async evaluation by transparently executing the block on a different thread.
class Future < Lazy
def initialize(&block)
@attribute = ::Lazy.new(&block)
@thread = ::Thread.new { @attribute.__send__(:__call__) }
end
private
def __call__
@reu
reu / array-unique.js
Last active August 29, 2015 14:08
array-unique.js
Array.prototype.unique = function() {
return this.filter(function(value, index, array) {
return array.indexOf(value) === index;
});
}
@reu
reu / functional.js
Last active August 29, 2015 14:10
Some building blocks for functional programming in Javascript
function curry(fn) {
var arity = fn.length,
args = Array.prototype.slice.call(arguments, 1);
function accumulator() {
var leftArgs = args.concat(Array.prototype.slice.call(arguments, 0));
if (leftArgs.length >= arity) {
return fn.apply(this, leftArgs);
} else {
#!/usr/bin/env ruby
require "webrick"
unless ARGV[0]
STDERR.puts "Usage: cgiup [PATH TO CGI SCRIPT]"
exit 1
end
cgi_script = File.expand_path(ARGV[0])
def luminance(color = "FFFFFF")
r, g, b = color.chars.each_slice(2).map(&:join).map(&:hex)
r * 0.299 + g * 0.587 + b * 0.114
end