Skip to content

Instantly share code, notes, and snippets.

@jtheisen
Created December 3, 2016 22:03
Show Gist options
  • Save jtheisen/f680afab642454fc0747b21ec3b11e02 to your computer and use it in GitHub Desktop.
Save jtheisen/f680afab642454fc0747b21ec3b11e02 to your computer and use it in GitHub Desktop.
Lodash intersperse
function intersperse(a, f) {
var pairs = _.zip(
_.tail(a),
_.slice(a, 0, -1)
);
var inner = _.map(pairs, _.isFunction(f) ? _.spread(f) : _.constant(f));
var result = _.chain(a)
.zip(inner)
.flatten()
.slice(0, -1)
.value();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment