Skip to content

Instantly share code, notes, and snippets.

@boxxxie
Created July 13, 2012 04:24
Show Gist options
  • Save boxxxie/3102707 to your computer and use it in GitHub Desktop.
Save boxxxie/3102707 to your computer and use it in GitHub Desktop.
I love immutability
//requires underscore.js & JSON.js
/*
* This is a functional implementation of a SET
* adding and removing from sets do not overwrite previous values of the set
*/
function Set(initial_items){
if(initial_items && !_.isObject(initial_items)){
throw "the set must be given a list of initial items, or empty arguments"
}
function initialize_items(initial_items){
return _.chain(initial_items || [])
.map(JSON.stringify) //make hashes
.zip(initial_items) //pair hashes with items
.unique(false,_.first) //not sorted
.reduce(function(list,item_hash_pair){
var hash = item_hash_pair[0];
var item = item_hash_pair[1];
list[hash] = item;
return list;
},{})
.value();
}
return (function Trusted_Set(set_items){
var items = set_items;
return {
add:function(item){
var item_hash = JSON.stringify(item); //only works for sets
var new_set = _.clone(items);
new_set[item_hash] = item;
return Trusted_Set(new_set);
},
remove:function(item){
var item_hash = JSON.stringify(item); //only works for sets
var new_set = _.clone(items);
delete new_set[item_hash];
return Trusted_Set(new_set);
},
exists:function(item){
var item_hash = JSON.stringify(item); //only works for sets
return _.has(items,item_hash);
},
list:function(){
return _.values(items);
}
}
})
(initialize_items(initial_items || []))
}
//FIXME: put these in a test file and use a test framework
function Set_tests(){
console.log(_.isEqual(Set([1,2,3]).list(),[1,2,3])+ ': _.isEqual(Set([1,2,3]).list(),[1,2,3])');
console.log(_.isEqual(Set().add(1).list(),[1])+ ': _.isEqual(Set().add(1).list(),[1])');
console.log(_.isEqual(Set([2]).add(1).list(),[1,2])+ ': _.isEqual(Set([2]).add(1).list(),[1,2])');
console.log(_.isEqual(Set([2]).add(1).remove(2).list(),[1])+ ': _.isEqual(Set([2]).add(1).remove(2).list(),[1])');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment