Last active
September 28, 2017 14:27
-
-
Save CMCDragonkai/27b0fa7bd223b760fe58265de97a4052 to your computer and use it in GitHub Desktop.
Is array 1 a prefix of array 2? #javascript
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
// 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