A library for lists of things you want to track, kind of like Html.Keyed but for your data model.
A list that gives each element a locally unique Key
for later modification or removal.
Convert a list using fromList
or build one from scratch with empty
and push
.
An item identifier. Use with update
and remove
.
empty : KeyedList a
A list with no items.
cons : a -> KeyedList a -> KeyedList a
Attach a new item to the beginning of the list.
push : a -> KeyedList a -> KeyedList a
Attach a new item to the end of the list.
fromList : List a -> KeyedList a
Convert a List
to a KeyedList
, preserving the order of the items.
decoder : Json.Decode.Decoder a -> Json.Decode.Decoder (KeyedList a)
Decode a KeyedList
from JSON
update : Key -> (a -> a) -> KeyedList a -> KeyedList a
Update the value of a list for a specific Key
with a given function.
remove : Key -> KeyedList a -> KeyedList a
Remove an item from a list by Key
. If the Key
is not found, no changes are made.
toList : KeyedList a -> List a
Convert to a regular List
.
keyedMap : (Key -> a -> b) -> KeyedList a -> List b
Create a List
out of items and their Key
s. This is particularly useful in the view
of a Model
that contains a KeyedList
.
type alias Model =
{ submodels : KeyedList SubModel
...
}
view : Model -> Html Msg
view model =
keyedMap viewKeyedSubmodel model.submodels
|> div []
viewKeyedSubmodel : Key -> SubModel -> Html Msg
viewKeyedSubmodel key submodel =
div [ onClick <| Click key ] [ SubModel.view submodel ]
encode : (a -> Json.Encode.Value) -> KeyedList a -> Json.Encode.Value
Encode a KeyedList
to JSON
isEmpty : KeyedList a -> Basics.Bool
Check if a list is currently empty
length : KeyedList a -> Basics.Int
Get the current length of a list
filter : (a -> Basics.Bool) -> KeyedList a -> KeyedList a
Keep only elements that satisfy the predicate, preserving Key
s.
map : (a -> b) -> KeyedList a -> KeyedList b
Apply a function to every item in a list, preserving Key
s.