Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

michaelficarra commented Aug 5, 2012

  if (Array.isArray(x)) res.push.apply(res, x);
  else res.push(x);
res.concat(x);
@Raynos

This comment has been minimized.

Copy link
Owner Author

Raynos commented Aug 6, 2012

@michaelficarra

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
You can’t perform that action at this time.