Created
February 27, 2019 15:06
-
-
Save JamieDixon/9e2013909cb2188fc1be247c2d7d50ec 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 Empty = Symbol(); | |
const stateIdentity = 0; | |
const adjusters = { | |
'(': 1, | |
')': -1 | |
} | |
const getClosingParenPositionRec = (start, str) => { | |
const inner = ([char = Empty, ...rest], state = stateIdentity, count = start) => { | |
const nextState = state + (adjusters[char] || stateIdentity); | |
return char === Empty || nextState === 0 | |
? count | |
: inner(rest, nextState, count + 1); | |
} | |
return inner(str.substring(start)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment