Created
December 3, 2016 22:03
-
-
Save jtheisen/f680afab642454fc0747b21ec3b11e02 to your computer and use it in GitHub Desktop.
Lodash intersperse
This file contains hidden or 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
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