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
selectedMap is the feature function in this package.
Use selectedMap in view.
view : SelectList String -> Html Msg
view selectList =
ul [] <|
SelectList.selectedMap
(\position item ->
li [ onClick (Set item) ]
[ text <| toString <| SelectList.index item
, toString <| SelectList.selected item
]
)
selectListCreate a SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selected
== 4Create a SelectList if list has elements.
If empty, Nothing.
fromList [] == Nothing
fromList [ 2, 3, 4 ] == Just (fromLists [] 2 [ 3, 4 ])Create a SelectList containing exactly one element.
singleton 3 == fromLists [] 3 []Destruct SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> toTuple
== ( 1, 2, 3 ) 4 ( 5, 6 )Return the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selected
== 4Return the elements after the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> listAfter
== [ 5, 6 ]Return the elements before the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> listBefore
== [ 1, 2, 3 ]Destruct SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> toList
== [ 1, 2, 3, 4, 5, 6 ]Check 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
== TrueCheck if the selected element is only element in select list.
fromLists [] 4 []
|> isSingle
== TrueLength of SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> length
== 6Length of the elements before the selected element
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> beforeLength
== 3Length of the elements after the selected element
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> afterLength
== 2Index of the selected element.
This is alias of beforeLength.
index =
beforeLengthReverse a select list. Pivot is selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> reverse
== fromLists [ 6, 5 ] 4 [ 3, 2, 1 ]Attempt to perform action over selectList and return original SelectList
in cases where this action returns Nothing.
attempt f selectList =
f selectList
|> Maybe.withDefault selectListDelete the selected element, then select an element after the selected.
If a list after selected is empty, then select an element before the selected.
Returns Nothing if SelectList has only single value.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> delete
== Just (fromLists [ 1, 2, 3 ] 5 [ 6 ])
fromLists [ 1, 2, 3 ] 4 []
|> delete
== Just (fromLists [ 1, 2 ] 3 [])
fromLists [] 4 []
|> delete
== NothingInsert new selected element, then move the old before it.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> insertBefore 8
== fromLists [ 1, 2, 3, 4 ] 8 [ 5, 6 ]Insert new selected element, then move the old after it.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> insertAfter 8
== fromLists [ 1, 2, 3 ] 8 [ 4, 5, 6 ]Apply a function to every element of a SelectList.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> map (\num -> num * 2)
== fromLists [ 2, 4, 6 ] 8 [ 10, 12 ]Apply a function to elements before the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> mapBefore (\selected -> 2 * selected)
== fromLists [ 2, 4, 6 ] 4 [ 5, 6 ]Apply a function to elements after the selected element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> mapAfter (\selected -> 2 * selected)
== fromLists [ 1, 2, 3 ] 4 [ 10, 12 ]Update the selected element using given function.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> updateSelected (\selected -> 2 * selected)
== fromLists [ 1, 2, 3 ] 8 [ 5, 6 ]Update elements before the selected element using given function.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> updateBefore (\before -> List.map ((*) 2) before)
== fromLists [ 2, 4, 6 ] 4 [ 5, 6 ]Update elements after the selected element using given function.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> updateBefore (\after -> List.map ((*) 2) after)
== fromLists [ 1, 2, 3 ] 4 [ 10, 12 ]Alias of update function.
replaceX x =
updateX (always x)Replace the selected element with new one.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> replaceSelected 10
== fromLists [ 1, 2, 3 ] 10 [ 5, 6 ]Replace elements before the selected element with new elements.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> replaceBefore [ 7, 8 ]
== fromLists [ 7, 8 ] 4 [ 5, 6 ]Replace elements after the selected element with new elements.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> replaceAfter [ 9, 10, 11 ]
== fromLists [ 1, 2, 3 ] 4 [ 9, 10, 11 ]Position is used with selectedMap.
Position is Selected if the selected element,
BeforeSelected if an element before the selected element,
and AfterSelected if an element after the selected element.
Apply a function to every element of a SelectList.
The transform function receives a Position and SelectList which selects a focused element.
Use in view.
view : SelectList String -> Html Msg
view selectList =
ul [] <|
SelectList.selectedMap
(\position item ->
li [ onClick (Set item) ]
[ text <| toString <| SelectList.index item
, toString <| SelectList.selected item
]
)
selectListGet a focused item and index from select list.
Position describes whether it is selected, or not.
Compared with List.indexedMap.
selectedMap : (Position -> SelectList a -> b) -> SelectList a -> List b
indexedMap : (Int -> a -> b) -> List a -> List bUnlike indexedMap, we can get full access to all elements in the list.
And set new list to Model.
If you don't use non-empty list, use selectedMapForList that receives List instead of SelectList.
Apply a function to every element of a List.
The transform function receives a SelectList which selects a focused element.
Use in view.
view : SelectList String -> Html Msg
view selectList =
ul [] <|
SelectList.selectedMapForList
(\item ->
li [ onClick (Set <| SelectList.toList <| SelectList.update updateFunction item) ]
[ text <| toString <| SelectList.index item
, toString <| SelectList.selected item
]
)
selectListUse this instead of indexedMap.
Move selected element.
Move a selected element by n steps. Pass an index over the length, then move to head/last.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveBy -2
== fromLists [ 1 ] 4 [ 2, 3, 5, 6 ]
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveBy 1
== fromLists [ 1, 2, 3, 5 ] 4 [ 6 ]
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveBy 3
== fromLists [ 1, 2, 3, 5, 6 ] 4 []Move a selected element by n steps while loop. Pass an index over the length, then loop.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveWhileLoopBy 4
== fromLists [ 1 ] 4 [ 2, 3, 5, 6 ]
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveWhileLoopBy -4
== fromLists [ 1 2, 3, 5, 6 ] 4 []Move a selected element to head.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveToHead
== fromLists [] 4 [ 1, 2, 3, 5, 6 ]Move a selected element to last.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> moveToLast
== fromLists [ 1, 2, 3, 5, 6 ] 4 []Select new element, otherwise move focus.
Select the first element, before the old selected, that satisfies a predicate. If none match, return Nothing.
isOdd num =
modBy 2 num /= 0
fromLists [ 1, 2 ] 3 [ 4, 5, 6 ]
|> selectBeforeIf isOdd
== Just (fromLists [ 0 ] 1 [ 2, 3, 4, 5, 6 ])Select the first element, after the old selected, that satisfies a predicate. If none match, return Nothing.
isOdd num =
modBy 2 num /= 0
fromLists [ 1, 2 ] 3 [ 4, 5, 6 ]
|> selectAfterIf isOdd
== Just (fromLists [ 1, 2, 3, 4 ] 5 [ 6 ])Select an element by n steps. Pass an index over the length, then returns Nothing.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectBy -1
== Just (fromLists [ 1, 2 ] 3 [ 4, 5, 6 ])
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectBy 2
== Just (fromLists [ 1, 2, 3, 4, 5 ] 6 [])
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectBy 3
== NothingSelect an element by n steps while loop. Pass an index over the length, then loop.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectWhileLoopBy 3
== fromLists [] 1 [ 2, 3, 4, 5, 6 ]
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectWhileLoopBy -5
== fromLists [ 1, 2, 3, 4 ] 5 [ 6 ]Select a head element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectHead
== fromLists [] 1 [ 2, 3, 4, 5, 6 ]Select a last element.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
|> selectLast
== 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 []
]List of all SelectList before the selected.
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 ]
]List of all SelectList after the selected.
fromLists [ 1, 2, 3 ] 4 [ 5, 6 ]
== [ fromLists [ 1, 2, 3, 4 ] 5 [ 6 ]
, fromLists [ 1, 2, 3, 4, 5 ] 6 []
]
A nonempty list which always has exactly one element selected.