Skip to content

Instantly share code, notes, and snippets.

@phi16
Last active February 21, 2016 13:54
Show Gist options
  • Save phi16/092fe7f0446289c517da to your computer and use it in GitHub Desktop.
Save phi16/092fe7f0446289c517da to your computer and use it in GitHub Desktop.
Closure Sample
// ------------------------------------------------------------------
// Usual Version
// ------------------------------------------------------------------
var i = 0;
var draw = function(xs){
console.log("Draw",i,":",xs);
i++;
};
var Mesh = function(v){
var arr = v;
this.add = function(d){
for(var i=0;i<arr.length;i++){
arr[i] += d;
}
};
this.draw = function(){
draw(arr);
};
};
var Model = function(ms){
var meshes = ms;
this.add = function(d){
for(var i=0;i<meshes.length;i++){
meshes[i].add(d);
}
};
this.index = function(i){
return meshes[i];
};
this.draw = function(){
meshes.forEach(function(e){
e.draw();
});
};
};
var modelMaker = function(vs){
var a = [];
vs.forEach(function(v){
a.push(new Mesh(v));
});
return new Model(a);
};
var m = modelMaker([[0,1,2],[3,4,5]]);
m.add(1);
m.index(0).add(2);
m.draw();
// Draw 0 : [3, 4, 5]
// Draw 1 : [4, 5, 6]
// ------------------------------------------------------------------
// Closure Version
// ------------------------------------------------------------------
var draw = (function(){
var i = 0;
return function(xs){
console.log("Draw",i,":",xs);
i++;
};
})();
var Mesh = function(v){
var arr = v;
return {
add : function(d){
for(var i=0;i<arr.length;i++){
arr[i] += d;
}
},
draw : function(){
draw(arr);
}
};
};
var Model = function(ms){
var meshes = ms;
return {
add : function(d){
for(var i=0;i<meshes.length;i++){
meshes[i].add(d);
}
},
index : function(i){
return meshes[i];
},
draw : function(){
meshes.forEach(function(m){
m.draw();
});
}
};
};
var modelMaker = function(vs){
var a = [];
vs.forEach(function(v){
a.push(Mesh(v));
});
return Model(a);
};
var m = modelMaker([[0,1,2],[3,4,5]]);
m.add(1);
m.index(0).add(2);
m.draw();
// Draw 0 : [3, 4, 5]
// Draw 1 : [4, 5, 6]
// ------------------------------------------------------------------
// Ultimate Version
// ------------------------------------------------------------------
var draw = (function(){
var i = 0;
return function(xs,c){
console.log("Draw",i,":",xs.map(c(i)));
i++;
};
})();
var Mesh = function(v){
return function(m){
draw(v,m);
};
};
var Model = function(ms){
var c = function(i){return function(x){return x}};
return {
add : function(d){
c = (function(k){
return function(j){return function(x){
return d+k(j)(x);
};};
})(c);
},
draw : function(){
ms.forEach(function(m){
m(c);
});
},
index : function(i){
return {
add : function(d){
c = (function(k){
return function(j){return function(x){
return i==j?d+k(j)(x):k(j)(x);
};};
})(c);
}
}
}
};
};
var modelMaker = function(vs){
var a = [];
vs.forEach(function(v){
a.push(Mesh(v));
});
return Model(a);
};
var m = modelMaker([[0,1,2],[3,4,5]]);
m.add(1);
m.index(0).add(2);
m.draw();
// Draw 0 : [3, 4, 5]
// Draw 1 : [4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment