let find ?(from=0) ?length s ~f =
(* index and virtual_length are maybe a bit redundant but I favor
readability of the branches of the match *)
let from = if from <= 0 then 0 else from in
let rec find_from index virtual_length l =
match l, length with
| [], _ -> None
| _, Some lgth when lgth <= virtual_length -> None
| h :: t, _ when index < from -> find_from (index + 1) virtual_length t
| h :: t, _ when index >= from && f h -> Some index
| h :: t, _ -> find_from (index + 1) (virtual_length + 1) t
in
find_from 0 0 s