This module exposes a list that has at most one selected item.
The invariants here:
But, these only hold if there are no duplicates in your list.
Convert a Selection list back to a regular list. This is useful
for creating view functions, for example. If you want a list that has
the selected item, use mapSelected like this:
[ 1, 2, 3 ]
|> fromList
|> select 2
|> mapSelected
{ selected = \a -> (True, a)
, rest = \a -> (False, a)
}
|> toList
--> [ (False, 1), (True, 2), (False, 3) ]Mark an item as selected. This will select at most one item. Any previously selected item will be unselected.
fromList ["Burrito", "Chicken Wrap", "Taco Salad"]
|> select "Burrito"
|> selected --> Just "Burrito"Attempting to select an item that doesn't exist is a no-op.
fromList ["Burrito", "Chicken Wrap", "Taco Salad"]
|> select "Doner Kebab"
|> selected --> NothingMark an item as selected by specifying a function. This will select the
first item for which the function returns True. Any previously selected item
will be unselected.
fromList ["Burrito", "Chicken Wrap", "Taco Salad"]
|> selectBy (String.startsWith "B")
|> selected --> Just "Burrito"Deselect any selected item. This is a no-op if nothing is selected in the first place.
Get the selected item, which might not exist.
fromList ["Burrito", "Chicken Wrap", "Taco Salad"]
|> select "Burrito"
|> selected --> Just "Burrito"Apply a function to all the items.
fromList [1, 2, 3]
|> map ((*) 2)
|> toList --> [2, 4, 6]Apply a function to all the items, treating the selected item specially.
fromList [1, 2, 3]
|> select 2
|> mapSelected { selected = (*) 2, rest = identity }
|> toList --> [1, 4, 3]
A list of items, one of which might be selected.