Skip to content

Instantly share code, notes, and snippets.

@joakimk joakimk/Update.elm
Last active Sep 22, 2016

Embed
What would you like to do?
One attempt at simple adding or updating a record in a list in Elm
import UpdateList exposing (addOrUpdateById)
update msg model =
case msg of
NewOrUpdatedItem item ->
({model | items = model.items |> addOrUpdateById item}, Cmd.none)
module UpdateList exposing (addOrUpdateById)
type alias Identifyable a = { a | id : Int }
addOrUpdateById : Identifyable a -> List (Identifyable a) -> List (Identifyable a)
addOrUpdateById record list =
if isNewRecord record list then
List.concat [ [ record ], list ]
else
List.map (updateById record) list
updateById : Identifyable a -> Identifyable a -> Identifyable a
updateById record r =
if r.id == record.id then
record
else
r
isNewRecord : Identifyable a -> List (Identifyable a) -> Bool
isNewRecord record list =
not
(
list
|> List.map (\r -> r.id)
|> List.member record.id
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.