Skip to content

Instantly share code, notes, and snippets.

@bruno-c
Forked from 140bytes/LICENSE.txt
Created May 27, 2011 20:20
Show Gist options
  • Save bruno-c/996085 to your computer and use it in GitHub Desktop.
Save bruno-c/996085 to your computer and use it in GitHub Desktop.
functor collection: works with coll(k) to get, coll.add(k, v) to add, coll.del(k) to remove
function( f /* blank */ ){
f = function(k){ return f.h[k] }; // Get an object by key
f.add = function(k,v){ return f.h[k]=v }; // Add a new object
f.del = function(k){ delete f.h[k] }; // Remove from collection
f.h = {}; // Store the objects in the function object itself
return f
}
C = function(f){f=function(k){return f.h[k]};f.add=function(k,v){return f.h[k]=v};f.del=function(k){delete f.h[k]};f.h={};return f};
var col = C();
col.add('a', 'apple');
col.add('b', 'beer');
col.add('c', 'cola');
col.add('d', 'dodo');
col.del('d');
console.log( ['a', 'b', 'c', 'd'].map(col));
console.dir(col);
var col2 = C();
col2.add('moo', 'cow');
console.dir(col2);
function(f){f=function(k){return f.h[k]};f.add=function(k,v){return f.h[k]=v};f.del=function(k){delete f.h[k]};f.h={};return f}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Bruno Carriere <http://js-montreal.org>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "funcolmicro",
"description": "",
"keywords": [ "collection", "hashmap", "functor" ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment