This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
A library for encapsulating keyed lists in Elm
version 1.1.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.1.0
Committed At 2017-10-30 17:43:09 UTC
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1

Modules

README

KeyedList

A KeyedList is a list that assigns each item a locally unique identifier, which makes it easier to later update or remove items via a Msg. It encapsulates the ids, so your Model doesn't need to keep track of them.

Instead of

type alias Model =
    { submodels : List (Int, SubModel)
      uid : Int
      ...
    }


update : Msg -> Model -> Model
update msg model =
    case msg of
    Add submodel ->
        { model
            | submodels = (model.uid, submodel) :: model.submodels
        , uid = model.uid + 1
        }
    
    Update id submsg ->
        let
        updateHelper (subId, submodel) =
            if subId == id then
            (subId, SubModel.update submsg submodel)
            else
            (subId, submodel)
        in
        { model | submodels = List.map updateHelper model.submodels }

    Remove id ->
        let
        removeHelper (subId, _) = subId /= id
        in
        { model | submodels = List.filter removeHelper model.submodels }

you can write

type alias Model =
    { submodels : KeyedList SubModel
      ...
    }


update : Msg -> Model -> Model
update msg model =
    case msg of
    Add submodel ->
        { model | submodels = KeyedList.cons submodel model.submodels ]

    Update key submsg ->
        { model | submodels = KeyedList.update key (SubModel.update submsg) model.submodels }

    Remove key ->
        { model | submodels = KeyedList.remove key model.submodels }