Non empty lists using Zippers
Problem
Solution
1
type alias Book =
2
List Page
3
​
4
currentPage : Book -> Page
5
currentPage book =
6
case book of
7
page :: _ ->
8
page
9
[] ->
10
Debug.todo "this is a dead end."
11
Copied!
1
type alias Book =
2
{ previous : List Page
3
, current : Page
4
, next : List Page
5
}
6
​
7
currentPage : Book -> Page
8
currentPage book =
9
book.current
Copied!

Question

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

Answer

Such a list is called a Zipper List:
1
type alias ZipperList a =
2
{ previous : List a
3
, current : a
4
, next : List a
5
}
Copied!
Package: Use miyamoen/select-list for a complete implementation of a Zipper List.

Further reading