Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

text.compare

edit distance, similarity, the closest of a list, line-by-line diffs

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

Functions

functiondescription
distance(a: str, b: str)edit distance: inserts, deletes, substitutions and swaps of neighbors to turn a into b
similarity(a: str, b: str)1 - distance / longer length: 1 is identical, 0 shares nothing
closest(s: str, options: list)the option with the smallest edit distance to s (ignoring case); nil for an empty list
text_diff(a: str, b: str)line diff: removed lines start with -, added with +, kept with two spaces

distance

distance(a: str, b: str): edit distance: inserts, deletes, substitutions and swaps of neighbors to turn a into b

distance("kitten", "sitting")
# → 3
distance("form", "from")
# → 1

See also: similarity, closest

similarity

similarity(a: str, b: str): 1 - distance / longer length: 1 is identical, 0 shares nothing

similarity("color", "colour")
# → 0.833333

See also: distance

closest

closest(s: str, options: list): the option with the smallest edit distance to s (ignoring case); nil for an empty list

"aple".closest(["apple", "maple", "ape"])
# → "apple"

See also: distance

text_diff

text_diff(a: str, b: str): line diff: removed lines start with -, added with +, kept with two spaces

text_diff("a\nb\nc", "a\nc\nd")
# →   a
# → - b
# →   c
# → + d

See also: distance

More examples

compare

# typo fixer
"recieve".closest(["deceive", "receive", "recipe"])
# → "receive"
# how alike
similarity("color", "colour").percent(0)
# → "83%"
# what changed
text_diff("a\nb\nc", "a\nB\nc\nd")
# →   a
# → - b
# → + B
# →   c
# → + d