let take_while_with_index t ~f = let rec loop idx acc = function | h :: t when f idx h -> loop (idx + 1) (h :: acc) t | [] | _ :: _ -> List.rev acc in loop 0 [] t