Created
March 10, 2018 01:53
-
-
Save lyleunderwood/8e1cfead6e19911a41e1fc14dc369334 to your computer and use it in GitHub Desktop.
This file contains 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
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