Skip to content

Commit

Permalink
forEach, in sequence; map, in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
zoubin committed Oct 13, 2015
1 parent 79fef05 commit c99007f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 24 deletions.
69 changes: 50 additions & 19 deletions README.md
Expand Up @@ -14,6 +14,7 @@ Methods:

- [filter](#filter)
- [map](#map)
- [forEach](#forEach)
- [reduce](#reduce)
- [chain](#chain)

Expand All @@ -35,32 +36,62 @@ filter(
function (v) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(v % 2);
});
});
rs(v % 2)
})
})
},
function (err, results) {
console.log('promise:', results);
console.log('promise:', results)
}
);
)
```

## map

`fn` is called with elements in parallel.
If you want to run `fn` in sequence,
use [forEach](#forEach) instead.

Signature: `map(arr, fn, done)`

```javascript
map(
[1, 2, 3, 4],
function (v, i, a, next) {
process.nextTick(function () {
next(null, v << 2);
});
next(null, v << 2)
})
},
function (err, results) {
console.log('async:', results);
console.log('async:', results)
}
);
)

```

## forEach

`fn` is called with elements in sequence.
If you want to run `fn` in parallel,
use [map](#map) instead.

Signature: `forEach(arr, fn, done)`

```javascript
var count = 0

forEach(
[1, 2, 3, 4],
function (v, i, _, next) {
process.nextTick(function () {
console.log(count++ === i)
next(null, v << 2)
})
},
function (err, results) {
console.log(results)
}
)

```

Expand All @@ -73,13 +104,13 @@ reduce(
[1, 2, 3, 4],
function (a, b, i, arr, next) {
process.nextTick(function () {
next(null, a + b);
});
next(null, a + b)
})
},
function (err, results) {
console.log('async:', results);
console.log('async:', results)
}
);
)

```

Expand Down Expand Up @@ -123,21 +154,21 @@ methods.chain(
)

function odd(v) {
return v % 2;
return v % 2
}

function plusplus(v, i, a, next) {
process.nextTick(function () {
next(null, ++v);
});
next(null, ++v)
})
}

function sum(a, b) {
return new Promise(function (rs) {
process.nextTick(function () {
rs(a + b);
});
});
rs(a + b)
})
})
}

```
Expand Down
17 changes: 17 additions & 0 deletions example/forEach.js
@@ -0,0 +1,17 @@
var forEach = require('..').forEach

var count = 0

forEach(
[1, 2, 3, 4],
function (v, i, _, next) {
process.nextTick(function () {
console.log(count++ === i)
next(null, v << 2)
})
},
function (err, results) {
console.log(results)
}
)

3 changes: 2 additions & 1 deletion index.js
@@ -1,5 +1,6 @@

exports.forEach = exports.map = require('./lib/map')
exports.forEach = require('./lib/forEach')
exports.map = require('./lib/map')
exports.filter = require('./lib/filter')
exports.reduce = require('./lib/reduce')
exports.chain = require('./lib/chain')
Expand Down
6 changes: 5 additions & 1 deletion lib/each.js → lib/forEach.js
@@ -1,6 +1,10 @@
var run = require('run-callback')

module.exports = function each(arr, fn, start, end, res, done) {
module.exports = function (arr, fn, done) {
each(arr, fn, 0, arr.length, [], done)
}

function each(arr, fn, start, end, res, done) {
if (end <= start) {
return done(null, res)
}
Expand Down
21 changes: 19 additions & 2 deletions lib/map.js
@@ -1,6 +1,23 @@
var each = require('./each')
var run = require('run-callback')

module.exports = function (arr, fn, done) {
each(arr, fn, 0, arr.length, [], done)
var pending = arr.length
var results = []
var errored = false
arr.forEach(function (v, i) {
run([fn, v, i, arr], function (err, r) {
if (errored) {
return
}
if (err) {
errored = true
return done(err)
}
results[i] = r
if (--pending === 0) {
done(null, results)
}
})
})
}

1 change: 0 additions & 1 deletion lib/reduce.js
Expand Up @@ -30,4 +30,3 @@ module.exports = function (arr, fn, initial, done) {
}(start))
}


42 changes: 42 additions & 0 deletions test/forEach.js
@@ -0,0 +1,42 @@
var test = require('tape')
var each = require('..').forEach

test('forEach', function(t, cb) {
var count = 0
each(
[1, 2, 3, 4],
function (v, i, a, next) {
process.nextTick(function () {
t.equal(count++, i)
next(null, v << 2)
})
},
function (err, results) {
t.same(results, [4, 8, 12, 16])
cb()
}
)

})

test('forEach, err', function(t) {
t.plan(1)
each(
['1', 2, '3', 4],
function (v, i, a, next) {
process.nextTick(function () {
try {
var code = v.charCodeAt(0)
} catch (e) {
return next(e)
}
next(null, code)
})
},
function (err) {
t.ok(err instanceof Error)
}
)

})

0 comments on commit c99007f

Please sign in to comment.