Yet another SelectList implementation
A SelectList is a non-empty list which always has exactly one element selected. It is an example of a list zipper.
Inspired by these modules
mapBy is the main function in this package.
Use mapBy in view.
view : SelectList String -> Html Msg
view selectList =
ul [] <|
SelectList.mapBy
(\position item ->
li [ onClick (Set item) ]
[ text <| toString <| SelectList.index item
, toString <| SelectList.selected item
]
)
selectListA SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selected
== 4A SelectList if list has elements.
If empty, Nothing.
fromList [] == Nothing
fromList [ 2, 3, 4 ] == Just (fromLists [] 2 [ 3, 4 ])A SelectList containing exactly one element.
singleton 3 == fromLists [] 3 []Return a List containing the elements in a SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> toList
== [ 1, 2, 3, 4, 5, 6 ]Return the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selected
== 4Return the elements before the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> listBefore
== [ 1, 2, 3 ]Return the elements after the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> listAfter
== [ 5, 6 ]Index of the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> index
== 3Check if the selected element is first element.
fromLists [] 4 [ 5, 6 ]
|> isHead
== TrueCheck if the selected element is last element.
fromLists [ 1, 2, 3 ] 4 []
|> isLast
== TrueLength of SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> length
== 6Length of the elements after the selected element
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> afterLength
== 2Length of the elements before the selected element
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> beforeLength
== 3Attempt to perform action over selectList and return original SelectList
in cases where this action returns Nothing.
attempt f selectList =
f selectList
|> Maybe.withDefault selectListAdd elements to the end of a SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> append [ 7, 8 ]
|> toList
== [ 1, 2, 3, 4, 5, 6, 7, 8 ]Add elements to the beginning of a SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> prepend [ 7, 8 ]
|> toList
== [ 7, 8, 1, 2, 3, 4, 5, 6 ]Direction
Modify the selected element using given function.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> modify (\selected -> 2 * selected)
== fromLists [ 1, 2, 3 ] 8 [ 5, 6 ]Replace the selected element with new one.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> set 10
== fromLists [ 1, 2, 3 ] 10 [ 5, 6 ]Insert the new selected element, then move the old After/Before.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> insert After 8
== fromLists [ 1, 2, 3 ] 8 [ 4, 5, 6 ]Delete the selected element, then select After/Before element.
Returns Nothing if list of After/Before elements is empty.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> delete After
== Just (fromLists [ 1, 2, 3 ] 5 [ 6 ])
fromLists [ 1, 2, 3 ] 4 []
|> delete After
== Just (fromLists [ 1, 2 ] 3 [])
fromLists [] 4 []
|> delete After
== NothingA selectList selects a element before/after the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> changePosition After 2
== Just (fromLists [ 1, 2, 3, 4, 5 ] 6 [])
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> changePosition After 3
== NothingA selectList selects a head/last element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> changePositionToEnd After
== fromLists [ 1, 2, 3, 4, 5 ] 6 []
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> changePositionToEnd Before
== fromLists [] 1 [ 2, 3, 4, 5, 6 ]A selected element moves forward/backward.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveSelection Before 2
== Just (fromLists [ 1 ] 4 [ 2, 3, 5, 6 ])
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveSelection After 3
== NothingA selected element moves to head/last.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveSelectionToEnd Before
== Just (fromLists [] 4 [ 1, 2, 3, 5, 6 ])
fromLists [ 1, 2, 3, 5, 6 ] 4 []
|> moveSelectionToEnd After
== NothingChange the selected element to the nearest one which passes a predicate function. Find the list after selected element preferentially.
isEven num =
num % 2 == 0
fromLists [ 1, 2 ] 3 [ 4, 5, 6 ]
|> select isEven
== fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]List of all selectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
== [ fromLists [] 1 [ 2, 3, 4, 5, 6 ]
, fromLists [ 1 ] 2 [ 3, 4, 5, 6 ]
, fromLists [ 1, 2 ] 3 [ 4, 5, 6 ]
, fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
, fromLists [ 1, 2, 3, 4 ] 5 [ 6 ]
, fromLists [ 1, 2, 3, 4, 5 ] 6 []
]Transform each element of the SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> map (\num -> num * 2)
== fromLists [ 2, 4, 6 ] 8 [ 10, 12 ]Position is used with mapBy.
Position is Selected if the selected element,
BeforeSelected if an element before the selected element,
and AfterSelected if an element after the selected element.
Transform each element of the SelectList.
The transform function receives a Position
and SelectList which selects a transformed element.
mapBy is the main function in this package.
Use mapBy in view.
view : SelectList String -> Html Msg
view selectList =
ul [] <|
SelectList.mapBy
(\position item ->
li [ onClick (Set item) ]
[ text <| toString <| SelectList.index item
, toString <| SelectList.selected item
]
)
selectListIf you can not use non-empty list, use mapBy_ that receives List instead of SelectList.
This receives List instead of SelectList.
A nonempty list which always has exactly one element selected.
Create one using
fromLists,fromListorsingleton.