forked from creationix/howtonode.org
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Van Cutsem
authored and
Tom Van Cutsem
committed
Nov 9, 2010
1 parent
dbef73b
commit 2a3406a
Showing
10 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
var Trait = require('traits').Trait; | ||
var print = require('sys').print; | ||
|
||
var TEquality = Trait({ | ||
equals: Trait.required, | ||
differs: function(x) { return !this.equals(x); } | ||
}); | ||
|
||
var TMagnitude = Trait.compose(TEquality, Trait({ | ||
smaller: Trait.required, | ||
greater: function(x) { return !this.smaller(x) && this.differs(x) }, | ||
between: function(min, max) { | ||
return min.smaller(this) && this.smaller(max); | ||
} | ||
})); | ||
|
||
function TColor(rgb) { | ||
return Trait.compose(TEquality, Trait({ | ||
get rgb() { return rgb; }, | ||
equals: function(col) { return col.rgb.equals(this.rgb); } | ||
})); | ||
} | ||
|
||
function TCircle(center, radius, rgb) { | ||
return Trait.compose( | ||
TMagnitude, | ||
TEquality, | ||
Trait.resolve({ equals: 'equalColors' }, TColor(rgb)), | ||
Trait({ | ||
center: center, | ||
radius: radius, | ||
area: function() { return Math.PI * this.radius * this.radius; }, | ||
equals: function(c) { return c.center === this.center && | ||
r.radius === this.radius }, | ||
smaller: function(c) { return this.radius < c.radius } | ||
})); | ||
} | ||
|
||
function Circle(center, radius, rgb) { | ||
return Trait.create(Object.prototype, | ||
TCircle(center, radius, rgb)); | ||
} | ||
|
||
function test() { | ||
var red = { | ||
equals: function(o){ return ''+o === this.toString(); }, | ||
toString:function(){return 'red';}}; | ||
var c1 = Circle(0, 1, red); | ||
var c2 = Circle(1, 2, red); | ||
print('c1.area: ' + c1.area()); | ||
print('\nc1 < c2? ' + c1.smaller(c2)); | ||
print('\nc1 !== c2?' + c1.differs(c2)); | ||
print('\nc1 equalColors c2? '+c1.equalColors(c2)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Github: tvcutsem | ||
Email: tomvc.be@gmail.com | ||
Homepage: http://soft.vub.ac.be/~tvcutsem | ||
Twitter: tvcutsem | ||
Location: Brussels, Belgium | ||
|
||
I'm a computer science researcher at the University of Brussels. My focus is on programming language technology. I serve on the ECMAScript technical committee and I'm the author of traits.js, a library for trait composition in Javascript, see http://traitsjs.org |