Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adds additional Array-operations on the stack.

  • Loading branch information...
commit 5f3a7a1c78695a4b7e5cad2cd2822e17413af30f 1 parent 0120ccf
@dsc dsc authored
Showing with 69 additions and 8 deletions.
  1. +27 −2 README.markdown
  2. +16 −1 index.js
  3. +2 −2 package.json
  4. +24 −3 test/seq.js
View
29 README.markdown
@@ -252,11 +252,36 @@ Set the stack to a new array.
Set the stack to [].
-.push(x,y...), .pop(), .shift(), .unshift(x), .splice(...)
-----------------------------------------------------------
+.push(x,y...), .pop(), .shift(), .unshift(x), .splice(...), reverse()
+---------------------------------------------------------------------
+.map(...), .filter(...), .reduce(...)
+-------------------------------------
Executes an array operation on the stack.
+The methods `map`, `filter`, and `reduce` are also proxies to their Array counterparts:
+they have identical signatures to the Array methods, operate synchronously on the context
+stack, and do not pass a Context object (unlike `seqMap` and `parMap`).
+
+The result of the transformation is assigned to the context stack; in the case of `reduce`,
+if you do not return an array, the value will be wrapped in one.
+
+````javascript
+Seq([1, 2, 3])
+ .reduce(function(sum, x){ return sum + x; }, 0)
+ .seq(function(sum){
+ console.log('sum: %s', sum);
+ // sum: 6
+ console.log('stack is Array?', Array.isArray(this.stack));
+ // stack is Array: true
+ console.log('stack:', this.stack);
+ // stack: [6]
+ })
+;
+````
+
+
+
Explicit Parameters
===================
View
17 index.js
@@ -342,7 +342,7 @@ function builder (saw, xs) {
}, this)
;
- ['push','pop','shift','unshift','splice']
+ ['push','pop','shift','unshift','splice','reverse']
.forEach(function (name) {
this[name] = function () {
context.stack[name].apply(
@@ -355,6 +355,21 @@ function builder (saw, xs) {
}, this)
;
+ [ 'map', 'filter', 'reduce' ]
+ .forEach(function (name) {
+ this[name] = function () {
+ var res = context.stack[name].apply(
+ context.stack,
+ [].slice.call(arguments)
+ );
+ // stack must be an array, or bad things happen
+ context.stack = (Array.isArray(res) ? res : [res]);
+ saw.next();
+ return this;
+ };
+ }, this)
+ ;
+
this.extend = function (xs) {
if (!Array.isArray(xs)) {
throw new Error('argument to .extend() is not an Array');
View
4 package.json
@@ -1,6 +1,6 @@
{
"name" : "seq",
- "version" : "0.3.3",
+ "version" : "0.3.4",
"description" : "Chainable asynchronous flow control with sequential and parallel primitives and pipeline-style error handling",
"main" : "./index.js",
"repository" : {
@@ -12,7 +12,7 @@
"hashish" : ">=0.0.2 <0.1"
},
"devDependencies" : {
- "expresso" : "=0.7.x"
+ "expresso" : ">=0.7.x"
},
"script" : {
"test" : "expresso"
View
27 test/seq.js
@@ -169,10 +169,10 @@ exports.catchParMultipleErrors = function() {
Seq()
.par('one', function() {
- setTimeout(this.bind({}, 'rawr1'), 25);
+ setTimeout(this.bind({}, 'rawr1'), 25);
})
.par('two', function() {
- setTimeout(this.bind({}, 'rawr2'), 50);
+ setTimeout(this.bind({}, 'rawr2'), 50);
})
.catch(function(err,key) {
caught[key] = err;
@@ -551,7 +551,28 @@ exports.stack = function () {
assert.eql(arguments.length, 3);
assert.eql([a,b,e], ['a','b','e']);
assert.eql(this.stack, ['a','b','e']);
- this(null);
+ this.pass(null);
+ })
+ .reverse()
+ .seq(function (a, b, e){
+ assert.eql(arguments.length, 3);
+ assert.eql([a,b,e], ['e','b','a']);
+ assert.eql(this.stack, ['e','b','a']);
+ this.pass(null);
+ })
+ .map(function(ch){ return ch.toUpperCase(); })
+ .seq(function (A, B, E){
+ assert.eql(arguments.length, 3);
+ assert.eql([A,B,E], ['E','B','A']);
+ assert.eql(this.stack, ['E','B','A']);
+ this.pass(null);
+ })
+ .reduce(function(s, ch){ return s + ':' + ch; })
+ .seq(function (acc){
+ assert.eql(arguments.length, 1);
+ assert.eql(acc, 'E:B:A');
+ assert.eql(this.stack, ['E:B:A']);
+ this.pass(null);
})
.seq(function () {
clearTimeout(to);
Please sign in to comment.
Something went wrong with that request. Please try again.