Skip to content

Instantly share code, notes, and snippets.

@faiwer
Last active June 26, 2017 09:00
Show Gist options
  • Save faiwer/7baf523526dab6ac4b302268e16bb526 to your computer and use it in GitHub Desktop.
Save faiwer/7baf523526dab6ac4b302268e16bb526 to your computer and use it in GitHub Desktop.
const kittens = [];
for(const cat of cats)
{
if(cat.months < 7)
{
kittens.push(cat.name);
if(kittens.length === limit)
break;
}
}
// versus
const FUNC_NAME = (limit, predicate, list, i = 0, newList = []) => {
const isDone = limit <= 0 || i >= list.length
const isMatch = isDone ? undefined : predicate(list[i])
return isDone ? newList :
isMatch ? FUNC_NAME(limit - 1, predicate, list, i + 1, [...newList, list[i]])
: FUNC_NAME(limit, predicate, list, i + 1, newList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment