Skip to content

Instantly share code, notes, and snippets.

@JamieDixon
Created February 27, 2019 15:06
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 JamieDixon/9e2013909cb2188fc1be247c2d7d50ec to your computer and use it in GitHub Desktop.
Save JamieDixon/9e2013909cb2188fc1be247c2d7d50ec to your computer and use it in GitHub Desktop.
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