Skip to content

Instantly share code, notes, and snippets.

@klovadis
klovadis / gist:5170485
Created March 15, 2013 15:03
A Lua script for Redis that allows you to set hash values based on a dictionary table
-- 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 March 15, 2013 14:59
Two Lua scripts for Redis to turn HGETALL and HMGET into dictionary tables
-- 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 March 15, 2013 14:51
Lua script for redis to turn a dictionary table into a bulk reply
-- 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 March 6, 2013 12:51
Configuration of your JavaScript component
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 February 19, 2013 18:04
A basic webserver using the connect framework.
// 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 December 5, 2012 14:10 — forked from Shipow/robot.js
Shipow#001
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 November 18, 2012 23:39
Example of synchroneous flow
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 12:06
Example of "this" within event listeners
// 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 11:56
Code flow example of the EventEmitter
// 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 11:53
Helper function to handle messages from a newline separated stream
// 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;