Skip to content

Instantly share code, notes, and snippets.

@uriee
Last active August 29, 2015 14:13
Show Gist options
  • Save uriee/999407c03300ef8f4348 to your computer and use it in GitHub Desktop.
Save uriee/999407c03300ef8f4348 to your computer and use it in GitHub Desktop.
The Revision Object
var playFor = function(transformations,val) {
return [val].concat(transformations.map(function(x){
val = x[1](val);
return val;
}));
};
pop = function(arr) {
return [arr.slice(0,1)[0],arr.slice(1,arr.length)];
}
compose2 = function(f1,f2) {
return function(val){return f1(f2(val))};
}
compose = function(transformations) { /*should get a reversed transHistory*/
if (transformations.length === 1) {
return function(x){return x};
}
var f = pop(transformations);
return compose2(f[0],compose(f[1]));
}
var rev = function(value,History){
return function(){
var values;
values = History || [];
if (value) values.push([value,function(){return value}]);
var v = function(){
if(values.length > 0) return values[values.length-1][0]
return undefined
}
var history = function(b) {
var length = values.length
var back = (length > b ? b : length)
return values.slice(length-back,length)
}
return {
val : function(x) {
if(x){
values.push([x,function(){return x}]);
return this;
}
else return v();
},
trans : function(func) {
if(func) {
ret = func(v())
values.push([ret,func]);
return this;
}
},
map : function(func) {
if(func) {
ret = v().map(func)
values.push([ret, function(f) {return function(arr){return arr.map(f)}}(func)]);
return this;
}
},
allHistory : function(b) {
return history(b);
},
valueHistory : function(b) {
return history(b).map(function(value){
return value[0];
});
},
transHistory : function(b) {
return history(b).map(function(value){
return value[1];
});
},
spillGuts : function(){
return rev(values.slice())
},
forwordFor : function(val) {
return playFor(values.slice(1,values.length),val);
},
rewindFor : function(val) {
return playFor(values.slice(1,values.length).reverse(),val)
},
clone : function(){
return rev(null,values.slice());
},
rollBack : function(){
if (!values.length) throw Error("No History to Roll-In");
return rev(null,values.slice(0,values.length-1));
}
}
}()
}
var inc = function(x) {
return x+1;
}
var incBy = function(x) {
return function(y) {
return x+y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment