🌳
Walking though the Elm woods
  • Introduction
  • Structure of the book
  • Frequently asked questions
    • How can different types share data?
    • How to break Dependency Cycles?
    • How to structure an Elm project?
    • How to turn a Msg into a Cmd Msg?
    • How to update nested Records?
    • What are comparable types?
    • Why are Booleans bad?
    • 🔜Future topics
  • Recipes
    • Writing a Single Page Application
      • Share state across pages
      • Debounced Validation
      • Reusable views
    • Making impossible states Impossible
      • Non empty lists using Zippers
      • Restrict records using Opaque Types
      • Write safer functions using Phantom Types
    • Designing Elm package APIs
      • Create upwards compatible APIs
    • 🔜Future topics
  • Frameworks and packages
    • elm/parser
    • mdgriffith/elm-ui
    • 🔜Future topics
Powered by GitBook
On this page
  • Question
  • Answer
  • Further reading

Was this helpful?

  1. Recipes
  2. Making impossible states Impossible

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
    }

Further reading

PreviousMaking impossible states ImpossibleNextRestrict records using Opaque Types

Last updated 5 years ago

Was this helpful?

Package: Use for a complete implementation of a Zipper List.

📦Package:

📦Package:

miyamoen/select-list
miyamoen/select-list
wernerdegroot/listzipper