I'm translating the wcwidth python package to Roc, and in that they use a binary search to map a codepoint to its wcwidth value, at the moment I have a ~600 line long if-then-else, but I'm thinking of switching to a binary search like the python version uses. Does anyone have any thoughts on what the best way to do this in Roc would be?
if 0 <= codepoint && codepoint <= 31 then ControlCharacter # <control-0000> ~ <control-001F>
else if 32 <= codepoint && codepoint <= 93 then One # SPACE ~ RIGHT SQUARE BRACKET
else if codepoint == 94 then Zero # CIRCUMFLEX ACCENT
else if codepoint == 95 then One # LOW LINE
else if codepoint == 96 then Zero # GRAVE ACCENT
else if 97 <= codepoint && codepoint <= 126 then One # [26] LATIN SMALL LETTER A ~ TILDE
else if 127 <= codepoint && codepoint <= 159 then ControlCharacter # [33] <control-007F> ~ <control-009F>
else if 160 <= codepoint && codepoint <= 167 then One # NO-BREAK SPACE ~ SECTION SIGN
...
you should be able to write a pretty generic recursive version that drops part of the list on each step. The new list will be a seamless slice of the original list and no copying should happen.
Probably get middle value, do logic, drop first or second half, recurse.
Thanks, that's a nice solution that I wouldn't have thought of, maybe because I'd be worried about stuff being copied. Glad I asked!
Hannes has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC