mrpinsky / elm-keyed-list / KeyedList

A library for lists of things you want to track, kind of like Html.Keyed but for your data model.

Keyed Lists


type KeyedList a

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.


type Key

An item identifier. Use with update and remove.

Build

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

Modify

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.

Consume

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 Keys. 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

Common Helpers

isEmpty : KeyedList a -> Basics.Bool

Check if a list is currently empty

length : KeyedList a -> Basics.Int

Get the current length of a list

Transform

filter : (a -> Basics.Bool) -> KeyedList a -> KeyedList a

Keep only elements that satisfy the predicate, preserving Keys.

map : (a -> b) -> KeyedList a -> KeyedList b

Apply a function to every item in a list, preserving Keys.