NoRedInk/list-selection/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

List.Selection

This module exposes a list that has at most one selected item.

The invariants here:

  • You can select at most one item.
  • You can't select an item that isn't part of the list.
type Selection a

A list of items, one of which might be selected.

fromList : List a -> Selection a

Create a Selection a with nothing selected.

toList : Selection a -> List a

Convert a Selection list back to a regular list. This is useful for creating view functions, for example.

select : a -> Selection a -> Selection a

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 --> Nothing
selectBy : (a -> Bool) -> Selection a -> Selection a

Mark 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 : Selection a -> Selection a

Deselect any selected item. This is a no-op if nothing is selected in the first place.

selected : Selection a -> Maybe a

Get the selected item, which might not exist.

fromList ["Burrito", "Chicken Wrap", "Taco Salad"]
    |> select "Burrito"
    |> selected --> Just "Burrito"
map : (a -> b) -> Selection a -> Selection b

Apply a function to all the items, including the currently selected item.

fromList [1, 2, 3]
    |> map ((*) 2)
    |> toList --> [2, 4, 6]