Skip to content

Instantly share code, notes, and snippets.

@lyleunderwood
Created March 10, 2018 01:53
Show Gist options
  • Save lyleunderwood/8e1cfead6e19911a41e1fc14dc369334 to your computer and use it in GitHub Desktop.
Save lyleunderwood/8e1cfead6e19911a41e1fc14dc369334 to your computer and use it in GitHub Desktop.
const takeByPattern = (lists, pattern) => {
let listsCopy = lists.map(l => l.slice());
let relevantLists = listsCopy.filter((l, idx) => pattern.includes(idx));
let finalList = [];
const totalRemaining = () => relevantLists.map(l => l.length).reduce((sum, count) => sum + count, 0);
while(totalRemaining() > 0) {
pattern.forEach(idx => finalList.push(listsCopy[idx].shift()));
}
return finalList.filter(element => element);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment