如何更新列表中的项目并维护其索引?
How do I update an item within a list and maintain its 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
[]
The 2nd argument to function update
is causing a mismatch.
59| Dict.update index (Just { link |
isFeatured = isFeatured }) dictionary
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Function update
is expecting
the 2nd argument to be:
Maybe
{ contentType : ContentType
, profile : Profile
, title : Title
, topics : List Topic
, url : Url
, isFeatured : Bool
}
-> Maybe
{ contentType : ContentType
, isFeatured : Bool
, profile : Profile
, title : Title
, topics : List Topic
, url : Url
}
But it is:
Maybe
{ contentType : ContentType
, isFeatured : Bool
, profile : Profile
, title : Title
, topics : List Topic
, url : Url
}
Hint: It looks like a function needs 1 more argument.
是否有一个简单的示例说明如何更新列表中的任意项目?
是的,您可以 map
链接到具有更新值的链接:
let
updateLink l =
if l.title == link.title then
{ l | isFeatured = True }
else
l
in
List.map updateLink links
老实说,我不明白你代码中的 isFeatured
是什么,但我假设你想在 link.title 匹配时将其更新为 True。
Is there a simple example of how I can update an arbitrary item within a list?
像 this 这样的东西怎么样,它大致基于您提供的代码:
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 }]
我还应该注意,如果排序很重要,则更可靠的方法是向您的记录添加索引字段并对列表进行排序 if/when 必要。
如何更新列表中的项目?
我尝试了以下方法:
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
[]
The 2nd argument to function
update
is causing a mismatch.59| Dict.update index (Just { link | isFeatured = isFeatured }) dictionary ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Function
update
is expecting the 2nd argument to be:Maybe { contentType : ContentType , profile : Profile , title : Title , topics : List Topic , url : Url , isFeatured : Bool } -> Maybe { contentType : ContentType , isFeatured : Bool , profile : Profile , title : Title , topics : List Topic , url : Url }
But it is:
Maybe { contentType : ContentType , isFeatured : Bool , profile : Profile , title : Title , topics : List Topic , url : Url }
Hint: It looks like a function needs 1 more argument.
是否有一个简单的示例说明如何更新列表中的任意项目?
是的,您可以 map
链接到具有更新值的链接:
let
updateLink l =
if l.title == link.title then
{ l | isFeatured = True }
else
l
in
List.map updateLink links
老实说,我不明白你代码中的 isFeatured
是什么,但我假设你想在 link.title 匹配时将其更新为 True。
Is there a simple example of how I can update an arbitrary item within a list?
像 this 这样的东西怎么样,它大致基于您提供的代码:
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 }]
我还应该注意,如果排序很重要,则更可靠的方法是向您的记录添加索引字段并对列表进行排序 if/when 必要。