For the complete documentation index, see llms.txt. This page is also available as Markdown.

Non empty lists using Zippers

type alias Book =
    List Page

currentPage : Book -> Page
currentPage book =
    case book of
        page :: _ ->
            page
        [] ->
            Debug.todo "this is a dead end."
   
type alias Book =
    { previous : List Page
    , current : Page
    , next : List Page
    }

currentPage : Book -> Page
currentPage book =
    book.current

Question

How can I ensure that a list always contains at least one element.

Answer

Such a list is called a Zipper List:

type alias ZipperList a =
    { previous : List a
    , current : a
    , next : List a
    }

Package: Use miyamoen/select-list for a complete implementation of a Zipper List.

Further reading

Last updated