let dup strm =
let peek_nth n =
let rec loop n =
function
| [] -> None
| [ x ] -> if n = 0 then Some x else None
| _ :: l -> loop (n - 1) l
in loop n (Stream.npeek (n + 1) strm)
in Stream.from peek_nth