Skip to content

Instantly share code, notes, and snippets.

@klovadis
klovadis / gist:5170485
Created Mar 15, 2013
A Lua script for Redis that allows you to set hash values based on a dictionary table
View gist:5170485
-- sets all fields for a hash from a dictionary
local hmset = function (key, dict)
if next(dict) == nil then return nil end
local bulk = {}
for k, v in pairs(dict) do
table.insert(bulk, k)
table.insert(bulk, v)
end
return redis.call('HMSET', key, unpack(bulk))
end
@klovadis
klovadis / gist:5170446
Created Mar 15, 2013
Two Lua scripts for Redis to turn HGETALL and HMGET into dictionary tables
View gist:5170446
-- gets all fields from a hash as a dictionary
local hgetall = function (key)
local bulk = redis.call('HGETALL', key)
local result = {}
local nextkey
for i, v in ipairs(bulk) do
if i % 2 == 1 then
nextkey = v
else
result[nextkey] = v
@klovadis
klovadis / gist:5170386
Created Mar 15, 2013
Lua script for redis to turn a dictionary table into a bulk reply
View gist:5170386
-- turns a dictionary table into a bulk reply table
local dict2bulk = function (dict)
local result = {}
for k, v in pairs(dict) do
table.insert(result, k)
table.insert(result, v)
end
return result
end
@klovadis
klovadis / gist:5099102
Created Mar 6, 2013
Configuration of your JavaScript component
View gist:5099102
Namespace.ModuleName = function (opt) {
// default configuration values
var defaults = {
moduleContextSelector: '.select-an-awesome > .module',
moduleTitle: 'Some Arbitrary Title',
moduleTimeoutSpeed: 3000
}
@klovadis
klovadis / index.js
Created Feb 19, 2013
A basic webserver using the connect framework.
View index.js
// to install the connect framework, go to your applications directory
// and type "npm install connect" in your command prompt.
// to launch the webserver, place this file in your application
// directory and execute it using "node index.js"
var connect = require('connect')
, app = connect()
, webserver = require('http').createServer(app);
@klovadis
klovadis / robot.js
Created Dec 5, 2012 — forked from Shipow/robot.js
Shipow#001
View robot.js
var Robot = function(robot) {
robot.rotateCannon(-90);
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.ahead();
//i'll add a clone but i need to refactor collision
//robot.clone();
};
@klovadis
klovadis / gist:4108171
Created Nov 18, 2012
Example of synchroneous flow
View gist:4108171
var ee = new (require('events').EventEmitter)();
var foo;
// listener function
ee.on('test', function (newValue) {
foo = newValue;
});
// before emitting
foo = 'one';
@klovadis
klovadis / scope.js
Created May 28, 2012
Example of "this" within event listeners
View scope.js
// create EventEmitter instance
var myEventEmitter = new EventEmitter();
// attach a property
myEventEmitter.someVar = "some var";
// add event listener for "test"
myEventEmitter.on('test', function ()
console.log(this.someVar);
});
@klovadis
klovadis / gist:2818766
Created May 28, 2012
Code flow example of the EventEmitter
View gist:2818766
// create EventEmitter instance
var myEventEmitter = new EventEmitter();
// add event listener for "test"
myEventEmitter.on('test', function ()
console.log('2');
});
// the following code will output 1 2 3
console.log('1');
@klovadis
klovadis / newlinestream.js
Created May 26, 2012
Helper function to handle messages from a newline separated stream
View newlinestream.js
// returns a function that spits out messages to a callback
// function; those messages have been split by newline
function newLineStream(callback) {
var buffer = '';
return (function (chunk) {
var i = 0, piece = '', offset = 0;
buffer += chunk;
while ( (i = buffer.indexOf('\n', offset)) !== -1) {
piece = buffer.substr(offset, i - offset);
offset = i + 1;