Created
May 13, 2016 19:50
-
-
Save anton-107/575db1616ceaec66010373fde86735da to your computer and use it in GitHub Desktop.
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
class FiniteCardinality { | |
constructor (initialValue=0) { | |
this.value = 0; | |
} | |
increaseBy(val) { | |
this.value += Number(val); | |
} | |
} | |
class InfiniteCardinality { | |
constructor () { | |
this.value = Infinity; | |
} | |
increaseBy(/*val*/) { | |
// no-op | |
} | |
} | |
export class EmptySet { | |
constructor() { | |
this.elements = []; | |
this._cardinality = new FiniteCardinality(); | |
} | |
get size() { | |
return this.cardinality; | |
} | |
get cardinality() { | |
return this._cardinality; | |
} | |
addElement(el) { | |
this.elements.push(el); | |
this.cardinality.increaseBy(1); | |
} | |
contains(el) { | |
return this.elements.indexOf(el) > -1; | |
} | |
} | |
export class Set extends EmptySet { | |
constructor(initialElements=[]) { | |
super(); | |
initialElements.forEach(el => this.elements.addElement(el)); | |
} | |
} | |
export class NaturalNumbersSet extends EmptySet { | |
constructor() { | |
super(); | |
this._cardinality = new InfiniteCardinality(); | |
} | |
contains(el) { | |
return Number.isInteger(el) && el > 0; | |
} | |
} | |
export class IntegersSet extends EmptySet { | |
constructor() { | |
super(); | |
this._cardinality = new InfiniteCardinality(); | |
} | |
contains(el) { | |
return Number.isInteger(el); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment