gpt4 book ai didi

elm - 你如何迭代一个列表(也许是一个)

转载 作者:行者123 更新时间:2023-12-04 17:41:28 25 4
gpt4 key购买 nike

我有以下 graphQL 结果:

[Just { details = Just "Engine failure at 33 seconds and loss of vehicle", launch_year = Just "2006", links = Just { article_link = Just "https://www.space.com/2196-spacex-inaugural-falcon-1-rocket-lost-launch.html" }, mission_name = Just "FalconSat" }]



基于以下类型:
type alias Launch =
{ mission_name : Maybe String
, details : Maybe String
, launch_year : Maybe String
, links : Maybe LaunchLinks
}


type alias Launches =
Maybe (List (Maybe Launch))


type alias LaunchLinks =
{ article_link : Maybe String
}

我想通过 List.map 并将结果显示在无序列表中。我从这个开始:
renderLaunch : Launches -> Html Msg
renderLaunch launches =
div [] <|
case launches of
Nothing ->
[ text "Nothing here" ]

Just launch ->
launch
|> List.map (\x -> x)
|> ul []

但我不断收到此错误:

This function cannot handle the argument sent through the (|>) pipe:

141| launch 142| |> List.map (\x -> x) 143| |> ul [] ^^^^^ The argument is:

List (Maybe Launch)

But (|>) is piping it a function that expects:

List (Html msg)

最佳答案

问题在于Just launch案例需要导致 List (Html msg)但代码导致返回不同的类型。

当您使用时 List.map (\x -> x) ,它本质上是一个空操作。您正在迭代 List (Maybe Launch)并返回相同的东西。我建议创建另一个需要 Maybe Launch 的函数值并将其用作您的映射函数。例如:

displayLaunch : Maybe Launch -> Html Msg
displayLaunch launch =
case launch of
Nothing -> text "No launch"
Just l -> text (Debug.toString l)

现在您可以将其插入映射函数中:

Just launch ->
launch
|> List.map displayLaunch
|> ul []

但是,哎呀!现在你会收到一个新的错误提示:
The 2nd branch is:

Html Msg

But all the previous branches result in:

List (Html msg)

这里的问题是我们现在返回一个 ul来自 Just launch分支,我们需要返回一个 html 列表。您可以使用 List.singleton创建一个只有一项的列表:

Just launch ->
launch
|> List.map displayLaunch
|> ul []
|> List.singleton

关于elm - 你如何迭代一个列表(也许是一个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57170658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com