textmetrics/edit

Edit-script algebraic data type and helpers shared by the diff module.

An EditScript is a linear sequence of Edit values describing how to transform one list into another. Replaying the Equal and Delete steps recovers the original (old) list, while replaying Equal and Insert steps recovers the new list.

This module operates on plain lists; it does not perform Unicode normalization. Callers comparing strings should split inputs into graphemes via gleam/string.to_graphemes before computing a diff.

Types

A single edit step.

  • Equal(item)item is present in both inputs at the corresponding position.
  • Insert(item)item appears only in the new input.
  • Delete(item)item appears only in the old input.
pub type Edit(a) {
  Equal(item: a)
  Insert(item: a)
  Delete(item: a)
}

Constructors

  • Equal(item: a)
  • Insert(item: a)
  • Delete(item: a)

A complete edit script — a list of Edit values that describes how to transform one list into another.

pub type EditScript(a) =
  List(Edit(a))

A run of consecutive edits sharing the same constructor.

runs/1 groups a script’s Equal / Insert / Delete steps into EqualRun / InsertRun / DeleteRun so consumers (unified-diff emission, syntax highlighting, …) can iterate by block instead of by item.

pub type Run(a) {
  EqualRun(items: List(a))
  InsertRun(items: List(a))
  DeleteRun(items: List(a))
}

Constructors

  • EqualRun(items: List(a))
  • InsertRun(items: List(a))
  • DeleteRun(items: List(a))

Values

pub fn cost(script: List(Edit(a))) -> Int

Total number of Insert and Delete operations in script.

For an optimal Myers diff this equals distance.levenshtein_list(old, new) with substitution counted as one insert plus one delete.

pub fn recover_new(script: List(Edit(a))) -> List(a)

Replay the Equal and Insert steps of script to recover the new list.

recover_new(diff(old, new)) == new for every input pair, by construction.

pub fn recover_old(script: List(Edit(a))) -> List(a)

Replay the Equal and Delete steps of script to recover the old list.

recover_old(diff(old, new)) == old for every input pair, by construction.

pub fn runs(script: List(Edit(a))) -> List(Run(a))

Group consecutive edits that share a constructor into runs.

Search Document