Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active September 28, 2017 14:27
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 CMCDragonkai/27b0fa7bd223b760fe58265de97a4052 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/27b0fa7bd223b760fe58265de97a4052 to your computer and use it in GitHub Desktop.
Is array 1 a prefix of array 2? #javascript
// this is fully tail recursive
// also note that isPrefixOf([], []) is true
function isPrefixOf ([head1, ...tail1], [head2, ...tail2], prefixEquality = true) {
if (!prefixEquality) return false;
if (head1 === undefined) return true;
if (head2 === undefined) return false;
return isPrefixOf(tail1, tail2, head1 === head2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment