Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created April 14, 2018 21:09
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 dance2die/84d3bb00fafdb1ced427fc349c4135d7 to your computer and use it in GitHub Desktop.
Save dance2die/84d3bb00fafdb1ced427fc349c4135d7 to your computer and use it in GitHub Desktop.
Array.prototype.take = function(count) {
return this.filter((_, i) => i < count);
};
Array.prototype.takeWhile = function(predicate) {
return this.filter((_, i) => predicate(_, i));
};
function takeDemo(orders) {
const firstTwoOrders1 = orders.take(2);
printHeaderFooter(
"First Two Orders - Take",
() => printOrders(firstTwoOrders1, indentBy),
indentBy
);
const firstTwoOrders2 = orders.takeWhile((order, index) => index <= 1);
printHeaderFooter(
"First Two Orders - TakeWhile",
() => printOrders(firstTwoOrders2, indentBy),
indentBy
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment