src/Pure/General/buffer.ML
author wenzelm
Sun, 27 Oct 2024 11:02:21 +0100
changeset 81266 8300511f4c45
parent 80822 4f54a509bc89
permissions -rw-r--r--
clarified signature;

(*  Title:      Pure/General/buffer.ML
    Author:     Makarius

Scalable text buffers.
*)

signature BUFFER =
sig
  type T
  val empty: T
  val is_empty: T -> bool
  val add: string -> T -> T
  val length: T -> int
  val contents: T -> string list
  val content: T -> string
  val build: (T -> T) -> T
  val build_content: (T -> T) -> string
end;

structure Buffer: BUFFER =
struct

abstype T = Buffer of string list
with

val empty = Buffer [];

fun is_empty (Buffer xs) = null xs;

fun add "" buf = buf
  | add x (Buffer xs) = Buffer (x :: xs);

fun length (Buffer xs) = fold (Integer.add o size) xs 0;

fun contents (Buffer xs) = rev xs;
val content = implode o contents;

fun build (f: T -> T) = f empty;
fun build_content f = build f |> content;

end;

end;