Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 5, 2012 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/3267955 to your computer and use it in GitHub Desktop.
Save Raynos/3267955 to your computer and use it in GitHub Desktop.
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
Copy link

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

@Raynos
Copy link
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