Created
August 5, 2012 23:27
-
-
Save Raynos/3267955 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
var map = require("iterators").map | |
concatMap.async = concatMapAsync | |
module.exports = concatMap | |
function concatMap(xs, fn, thisValue) { | |
if (arguments.length === 2) { | |
thisValue = this | |
} | |
var res = []; | |
for (var i = 0; i < xs.length; i++) { | |
var x = fn.call(thisValue, xs[i], i); | |
if (Array.isArray(x)) res.push.apply(res, x); | |
else res.push(x); | |
} | |
return res; | |
} | |
function concatMapAsync(xs, fn, thisValue, callback) { | |
map(xs, fn, thisValue, function (err, result) { | |
if (err) { | |
return callback(err) | |
} | |
var arr = [] | |
result = arr.concat.apply(arr, result) | |
callback(null, result) | |
}) | |
} |
michaelficarra
commented
Aug 5, 2012
It's substacks code. I'm sure he has a reason for using push.apply
instead of concat. Probably because it's destructive when concat is not destructive.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment