riot icon indicating copy to clipboard operation
riot copied to clipboard

Make match%b expressions tail-recursive

Open leostera opened this issue 1 year ago • 0 comments

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

leostera avatar Jan 24 '24 17:01 leostera