-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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