module Stack:Core_stack is a replacement for OCaml's standard Stack module that follows Core idioms and adds some functions.sig
..end
Differences from the standard module:
pop
and top
return an option
rather than raise Empty
.
iter
takes a labeled argument.
push
takes the stack argument first.
length
is O(1)
exception Empty
type 'a
t
include Binable.S1
include Sexpable.S1
include Container.S1
to_list
and to_array
returns the elements in order from the top of
the stack to the bottom.val of_list : 'a list -> 'a t
of_list l
returns a stack whose top is the first element of l
and
bottom is the last element of l
.val invariant : 'a t -> unit
val create : unit -> 'a t
create ()
returns an empty stack.val push : 'a t -> 'a -> unit
push t x
adds x
to the top of stack t
.val pop : 'a t -> 'a option
pop t
returns None
if t
is empty, otherwise it returns Some x
where
x
is the top of t
and removes x
from the top of t
.val pop_exn : 'a t -> 'a
pop_exn t
removes and returns the top element of t
, raising Empty
if
t
is empty.val top : 'a t -> 'a option
top t
returns None
if t
is empty, otherwise it returns Some x
where
x
is the top of t
.val top_exn : 'a t -> 'a
top_exn t
returns the top element of t
, raising Empty
if t
is empty.val clear : 'a t -> unit
clear t
discards all elements from t
.val copy : 'a t -> 'a t
copy t
returns a copy of t
.val until_empty : 'a t -> ('a -> unit) -> unit
until_empty t f
repeatedly pops an element v
off of t
and runs f v
until t
becomes empty. It is fine if f
adds more elements to t
, in
which case the most-recently-added element will be processed first.