Skip to content

Instantly share code, notes, and snippets.

View jacob414's full-sized avatar
💭
Retired but semi-active, hobby projects & activism

Jacob Oscarson jacob414

💭
Retired but semi-active, hobby projects & activism
View GitHub Profile
@jacob414
jacob414 / usig.coffee
Created February 17, 2011 15:07
Micro global message bus in 4 LOC CoffeeScript
# Really, really tiny message bus
# Yes, I know it leaks.
usig._bus = {}
usig.connect = (msg, cb) -> (usig._bus[msg] ?= []).push(cb)
usig.signal = (msg, args...) ->
cb args... for cb in usig._bus[msg] if usig._bus[msg]
@jacob414
jacob414 / groupby.coffee
Created January 5, 2011 17:48
A grouping (see python's itertools.groupby()) function in CoffeeScript
# Edit 1, thx @satyr!
groupby = (iter, keyfn) ->
grouped = {}
(grouped[keyfn e] ?= []).push e for e in iter
grouped
@jacob414
jacob414 / repr.coffee
Created December 9, 2010 11:17
Quick and dirty Javascript repr() function using CoffeeScript
repr = (o, depth=0, max=2) ->
if depth > max
'<..>'
else
switch typeof o
when 'string' then "\"#{o.replace /"/g, '\\"'}\""
when 'function' then 'function'
when 'object'
if o is null then 'null'
if _.isArray o
@jacob414
jacob414 / backbone-tutorial.js
Created November 18, 2010 12:54
Minimal example of data handling in Backbone.js
/* Scaled-down Backbone.js demonstration
* By Jacob Oscarson (http://twitter.com/jacob414), 2010
* MIT Licenced, see http://www.opensource.org/licenses/mit-license.php */
$(function() {
window.ulog = function(msg) { $('#log').append($('<div>'+msg+'</div>')); }
// Faking a little bit of Backbone.sync functionallity
Backbone.sync = function(method, model, succeeded) {
ulog('<strong>'+method + ":</strong> " + model.get('label'));
if(typeof model.cid != 'undefined') {