Module Core_stack


module Core_stack: sig .. end
Core_stack is a replacement for OCaml's standard Stack module that follows Core idioms and adds some functions.

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.

Core_stack uses same implementation as the OCaml library. It is a reimplementation rather than a wrapper to enable direct implementations of some functions (e.g. fold) that cannot be easily implemented given the functions provided by OCaml's Stack module.


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 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.