I came up with a much simpler way to do (non-nested) patternsFull code: https://gist.github.com/jules-hedges/69a07606c1f0100682daeb5d82885296