riot
riot copied to clipboard
Make match%b expressions tail-recursive
currently when we write a recursive function that uses a match%b
expression to pattern-match on bytestrings, we end up creating a stack, instead of tail-recursing.
My gut tells me this is the try-catches we have, and we may want to move to a result-based error-recovery pattern.
For example, i'd expect this function to be tail recursive, but it isn't:
let rec split ?(left = {%b||}) str =
match%b str with
| {| "\r\n"::bytes, rest::bytes |} -> [ left; rest ]
| {| c::utf8, rest::bytes |} -> split ~left:(Bytestring.join c left) rest