From 03d5cf5686e9ea5448ee5e4901792130d9527a74 Mon Sep 17 00:00:00 2001 From: Arnaud Spiwack Date: Mon, 24 Feb 2014 16:23:04 +0100 Subject: A view type for IStream. View types are better practice than option types for pattern-matching. (Plus, they save a minute amount of allocations)--- lib/iStream.ml | 10 +++++----- lib/iStream.mli | 7 ++++++- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/iStream.ml b/lib/iStream.ml index f7d50612f9..ba08ffd24e 100644 --- a/lib/iStream.ml +++ b/lib/iStream.ml @@ -6,9 +6,11 @@ (* * GNU Lesser General Public License Version 2.1 *) (************************************************************************) -type 'a node = +type ('a,'r) peek = | Nil -| Cons of 'a * 'a t +| Cons of 'a * 'r + +type 'a node = ('a,'a t) peek and 'a t = 'a node Lazy.t @@ -32,9 +34,7 @@ let rec is_empty s = match Lazy.force s with | Nil -> true | Cons (_, _) -> false -let peek s = match Lazy.force s with -| Nil -> None -| Cons (x, s) -> Some (x, s) +let peek = Lazy.force let rec of_list = function | [] -> empty diff --git a/lib/iStream.mli b/lib/iStream.mli index caae0ad35a..fd3fa6c503 100644 --- a/lib/iStream.mli +++ b/lib/iStream.mli @@ -31,7 +31,12 @@ val thunk : 'a t Lazy.t -> 'a t val is_empty : 'a t -> bool (** Whethere a stream is empty. *) -val peek : 'a t -> ('a * 'a t) option +type ('a,'r) peek = +| Nil +| Cons of 'a * 'r +(** View type for {!peek} *) + +val peek : 'a t -> ('a , 'a t) peek (** Return the head and the tail of a stream, if any. *) (** {6 Standard operations} -- cgit v1.2.3