Last active
December 13, 2015 22:59
-
-
Save AutoSponge/4988442 to your computer and use it in GitHub Desktop.
Take the idea of a LinkedList and apply it to a queue to run a series of operations on a given input.
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
(function(global, undef) { | |
function QueueList(fn, next) { | |
if (!(this instanceof QueueList)) { | |
return new QueueList(fn, next); | |
} | |
this.fn = fn; | |
this.next = next || undef; | |
} | |
QueueList.prototype.after = function(fn) { | |
return new QueueList(fn, this); | |
}; | |
QueueList.prototype.insert = function (fn) { | |
var insert = this.next; | |
this.next = new QueueList(fn, insert); | |
return this.next; | |
}; | |
QueueList.prototype.fire = function (arg) { | |
val = this.fn.call(this, arg); | |
if (this.next) { | |
return this.next.fire(val); | |
} | |
return val; | |
}; | |
global.QueueList = QueueList; | |
}(this)); |
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
function add1(val) { | |
return val + 1; | |
} | |
function doubleVal(val) { | |
return val + val; | |
} | |
function squareVal(val) { | |
return val * val; | |
} | |
var list = QueueList(squareVal).after(doubleVal).after(add1); | |
list.fire(1); //16 | |
var orig = list.insert(add1); | |
list.fire(1); //36 | |
orig.fire(1); //16 |
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
(function(global, undef) { | |
function Stack(fn, next) { | |
if (!(this instanceof Stack)) { | |
return new Stack(fn, next); | |
} | |
this.fn = fn; | |
this.next = next || undef; | |
} | |
Stack.prototype.push = function(fn) { | |
return new Stack(fn, this); | |
}; | |
Stack.prototype.insert = function (fn) { | |
var insert = this.next; | |
this.next = new Stack(fn, insert); | |
return this.next; | |
}; | |
Stack.prototype.find = function (fn) { | |
if (this.fn === fn) { | |
return this; | |
} | |
if (this.next) { | |
return this.next.find(fn); | |
} | |
}; | |
Stack.prototype.fire = function (arg) { | |
val = this.fn.call(this, arg); | |
if (this.next) { | |
return this.next.fire(val); | |
} | |
return val; | |
}; | |
global.Stack = Stack; | |
}(this)); |
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
(function(global, undef) { | |
function Stack(fn, next) { | |
if (!(this instanceof Stack)) { | |
return new Stack(fn, next); | |
} | |
this.fn = fn; | |
this.next = next || undef; | |
} | |
Stack.prototype.push = function(fn) { | |
return new Stack(fn, this); | |
}; | |
Stack.prototype.add = function (fn) { | |
return this.next = new Stack(fn, this.next); | |
}; | |
Stack.prototype.addAll = function (arr) { | |
var prev = this; | |
arr.forEach(function (fn) { | |
prev = prev.add(fn); | |
}); | |
return this; | |
}; | |
Stack.prototype.index = function (idx) { | |
return !idx ? this : this.next && this.next.index(idx - 1); | |
}; | |
Stack.prototype.search = function (fn) { | |
return (this.fn === fn) ? this : this.next && this.next.search(fn); | |
}; | |
Stack.prototype.fire = function (arg) { | |
val = this.fn.call(this, arg); | |
return this.next ? this.next.fire(val) : val; | |
}; | |
Stack.prototype.clone = function (fn, next) { | |
return new Stack(fn || this.fn, next || this.next); | |
}; | |
global.Stack = Stack; | |
}(this)); | |
/**/ | |
function add1(val) { | |
return val + 1; | |
} | |
function doubleVal(val) { | |
return val + val; | |
} | |
function squareVal(val) { | |
return val * val; | |
} | |
var list = Stack(squareVal).push(doubleVal).push(add1); | |
console.assert(list.fire(1) === 16); | |
var orig = list.add(add1); | |
console.assert(list.fire(1) === 36); | |
console.assert(orig.fire(1) === 16); | |
console.assert(list.search(doubleVal).fire(1) === 4); | |
var stack = Stack(add1); | |
stack.add(doubleVal) | |
.add(squareVal); | |
console.assert(stack.fire(1) === 16); | |
console.assert(Stack(add1).addAll([add1, doubleVal, squareVal]).fire(1) === 36); |
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
function add1(val) { | |
return val + 1; | |
} | |
function doubleVal(val) { | |
return val + val; | |
} | |
function squareVal(val) { | |
return val * val; | |
} | |
var stack = Stack(squareVal).push(doubleVal).push(add1); | |
stack.fire(1); //16 | |
var orig = stack.insert(add1); | |
stack.fire(1); //36 | |
orig.fire(1); //16 | |
stack.find(doubleVal).fire(1); //4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment