This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Selection List Data Structure in Elm
version 1.0.0
license BSD3
native-modules False
elm-version 0.15.1 <= v < 0.16.0
Tag 1.0.0
Committed At 2015-07-18 03:56:18 UTC
elm-lang/core 2.1.0 <= v < 3.0.0 2.1.0




A selection list is a collection that keeps track of which item in the collection is currently selected. This data structure is especially useful when working with components such as tabs, pages, slideshows and anything pagination related.

type alias SelectionList a =
  { previous : List a
  , selected : a
  , next     : List a

Create a selection list

You can easily create a selection list from a list and a default value

mySelectionList : SelectionList Int
mySelectionList =
  fromList 1 [2, 3, 4, 5]

This selection list will have 1 as its first and selected value and the list will be the next values.

Change selected values

You can also easily move along the selection list. If you wish that 2 be selected, you can just call next

mySelectionList2 =
  next mySelectionList

We can query for the selected index with the dot syntax

mySelectionList2.selected -- 2

We can also go to the previous element

mySelectionList3 =
  previous mySelectionList2 -- 1

Or go to an nth element

mySelectionList4 =
  goto 3 mySelectionList -- 4

Mapping over a selection list

You can map over a selection list

mySelectionList5 =
  map (\x -> x * x) mySelectionList

You can map by index

mySelectionList6 =
  indexedMap (\index x -> index + x) mySelectionList

or you can apply different behaviors depending on whether the element is selected or not

mySelectionList7 =
  selectedMap (\isSelected x -> if isSelected then -1 else x) mySelectionList

Querying a selection list

You can ask how long the selection list is

length mySelectionList -- 5

or you can ask for the index of the selected element in the selection list

selectedIndex (next mySelectionList) -- 1

Note that the selection list is 0-based and thus the first element is the 0th element.

Convert to a list

Finally, you can convert a selection list to a list

toList mySelectionList -- [1, 2, 3, 4, 5]