Skip to content

Instantly share code, notes, and snippets.

@eldritchideen
Last active August 29, 2015 14:26
Show Gist options
  • Save eldritchideen/9d0004bd2935c68ca49f to your computer and use it in GitHub Desktop.
Save eldritchideen/9d0004bd2935c68ca49f to your computer and use it in GitHub Desktop.
function Vector(x,y) {
this.x = x
this.y = y
}
Vector.prototype.plus = function(other) {
return new Vector(this.x + other.x, this.y + other.y)
};
Vector.prototype.minus = function(other) {
return new Vector(this.x - other.x, this.y - other.y)
};
Object.defineProperty(Vector.prototype, "length", {
get: function() { return Math.sqrt((this.x * this.x) + (this.y * this.y)) }
});
console.log(new Vector(1, 2).plus(new Vector(2, 3)));
// → Vector{x: 3, y: 5}
console.log(new Vector(1, 2).minus(new Vector(2, 3)));
// → Vector{x: -1, y: -1}
console.log(new Vector(3, 4).length);
// → 5
// Your code here.
function ISeq() {}
ISeq.prototype.next = function () {
console.log("Returns next value");
};
ISeq.prototype.hasNext = function() {
console.log("true if there is more elements left in the sequence");
}
function ArraySeq(arr) {
this.arr = arr;
this.curr = 0;
};
ArraySeq.prototype = Object.create(ISeq.prototype);
ArraySeq.prototype.next = function() {
var res = this.arr[this.curr];
this.curr += 1;
return res;
};
ArraySeq.prototype.hasNext = function() {
if (this.curr < this.arr.length) {
return true;
} else {
return false;
}
};
function logFive(seq) {
var count = 0;
while(seq.hasNext() && count < 5) {
console.log(seq.next());
count += 1;
}
}
logFive(new ArraySeq([1, 2, 3, 4, 5, 6]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment