Elm 如何更新列表中的项目并维护其索引?

Elm 如何更新列表中的项目并维护其索引?,elm,Elm,如何更新列表中的项目 我尝试了以下方法: setFeaturedLink links link = let dictionary = Dict.fromList links result = Dict.filter (\k v -> v.title == link.title) dictionary |> Dict.toList |> List.head index =

如何更新列表中的项目

我尝试了以下方法:

setFeaturedLink links link =
    let
        dictionary =
            Dict.fromList links

        result =
            Dict.filter (\k v -> v.title == link.title) dictionary |> Dict.toList |> List.head

        index =
            case result of
                Just kv ->
                    let
                        ( i, _ ) =
                            kv
                    in
                        i

                Nothing ->
                    -1
    in
        if not <| index == -1 then
            Dict.update index (Just { link | isFeatured = isFeatured }) dictionary |> Dict.values
        else
            []
但事实是:

Maybe
    { contentType : ContentType
    , isFeatured : Bool
    , profile : Profile
    , title : Title
    , topics : List Topic
    , url : Url
    }
提示:函数似乎还需要一个参数


是否有一个简单的示例说明如何更新列表中的任意项目?

是的,您可以将链接映射到具有更新值的链接:

let
  updateLink l =
    if l.title == link.title then
      { l | isFeatured = True }
    else
      l
in
  List.map updateLink links

老实说,我不明白您的代码中有什么
isFeatured
,但我假设如果link.title匹配,您希望将其更新为True。

是的,您可以将链接映射到具有更新值的链接:

let
  updateLink l =
    if l.title == link.title then
      { l | isFeatured = True }
    else
      l
in
  List.map updateLink links
老实说,我不明白您的代码中有什么
isFeatured
,但我假设如果link.title匹配,您希望将其更新为True

有没有一个简单的例子来说明如何更新列表中的任意项

类似这样的东西怎么样,它松散地基于您提供的代码:

import Html exposing (text)
import List

type alias Thing = { title: String, isFeatured: Bool }

bar = (Thing "Bar" False)

things = [(Thing "Foo" False), 
         bar]

featureThing things thing = 
  List.map (\x -> if x.title == thing.title 
                    then { x | isFeatured = True} 
                    else x) 
           things

updatedThings = featureThing things bar

main =
  text <| toString updatedThings
  -- [{ title = "Foo", isFeatured = False },
  --  { title = "Bar", isFeatured = True }]
导入Html(文本)
导入列表
键入别名Thing={title:String,isFeatured:Bool}
bar=(事物“bar”为假)
things=[(things“Foo”False),
[酒吧]
特色事物
List.map(\x->如果x.title==thing.title
然后{x | isFeatured=True}
(十)
东西
updatedThings=功能化内容栏
主要=
正文
有没有一个简单的例子来说明如何更新列表中的任意项

类似这样的东西怎么样,它松散地基于您提供的代码:

import Html exposing (text)
import List

type alias Thing = { title: String, isFeatured: Bool }

bar = (Thing "Bar" False)

things = [(Thing "Foo" False), 
         bar]

featureThing things thing = 
  List.map (\x -> if x.title == thing.title 
                    then { x | isFeatured = True} 
                    else x) 
           things

updatedThings = featureThing things bar

main =
  text <| toString updatedThings
  -- [{ title = "Foo", isFeatured = False },
  --  { title = "Bar", isFeatured = True }]
导入Html(文本)
导入列表
键入别名Thing={title:String,isFeatured:Bool}
bar=(事物“bar”为假)
things=[(things“Foo”False),
[酒吧]
特色事物
List.map(\x->如果x.title==thing.title
然后{x | isFeatured=True}
(十)
东西
updatedThings=功能化内容栏
主要=

好的文本:)好的文本:)