let stream_peek_nth strm n =
let rec loop i =
function
| x :: xs -> if i = 1 then Some x else loop (i - 1) xs
| [] -> None
in loop n (Stream.npeek n strm)