diff options
-rw-r--r-- | stdlib/stream.ml | 13 | ||||
-rw-r--r-- | stdlib/stream.mli | 7 |
2 files changed, 18 insertions, 2 deletions
diff --git a/stdlib/stream.ml b/stdlib/stream.ml index 0a4af3d6e..753bce005 100644 --- a/stdlib/stream.ml +++ b/stdlib/stream.ml @@ -145,7 +145,18 @@ let of_list l = ;; let of_string s = - from (fun c -> if c < String.length s then Some s.[c] else None) + let count = ref 0 in + from (fun _ -> + (* We cannot use the index passed by the [from] function directly + because it returns the current stream count, with absolutely no + guarantee that it will start from 0. For example, in the case + of [Stream.icons 'c' (Stream.from_string "ab")], the first + access to the string will be made with count [1] already. + *) + let c = !count in + if c < String.length s + then (incr count; Some s.[c]) + else None) ;; let of_channel ic = diff --git a/stdlib/stream.mli b/stdlib/stream.mli index 1098a2765..aeb0da1e8 100644 --- a/stdlib/stream.mli +++ b/stdlib/stream.mli @@ -32,7 +32,12 @@ val from : (int -> 'a option) -> 'a t To create a new stream element, the function [f] is called with the current stream count. The user function [f] must return either [Some <value>] for a value or [None] to specify the end of the - stream. *) + stream. + + Do note that the indices passed to [f] may not start at [0] in the + general case. For example, [[< '0; '1; Stream.from f >]] would call + [f] the first time with count [2]. +*) val of_list : 'a list -> 'a t (** Return the stream holding the elements of the list in the same |