Elm:如何在嵌套列表上应用List.map

问题描述 投票:0回答:1

这是我使用简单的列表浮动完成的基本工作

renderCoordinates : List Float -> Html Msg
renderCoordinates coordinates =
    ol [ type_ "A"]
        (List.map (\coordinate -> li [] [text (String.fromFloat coordinate)]) coordinates)

[涉及List (List(List Float)),我被困住了。我想知道我是否可以这样吗?

renderCoordinates : List (List(List Float)) -> Html Msg
renderCoordinates coordinates =
    ol [ type_ "A"]
        (List.map (List.map (\coordinate -> li [] [text (String.fromFloat coordinate)]) ) coordinates)

-List.map (List.map (List.map (\coordinate -> li [] [text (String.fromFloat coordinate)]) ) ) coordinates doesnt work as well

基本上我希望显示List (List(List Float))列表中的每个项目...任何帮助,感激不尽!

nested-lists elm
1个回答
0
投票

您需要分批进行,并在每个阶段合并结果

renderCoordinates : List (List (List Float)) -> Html Msg
renderCoordinates coordinates =
    let
        map1 : List (List Float) -> Html Msg
        map1 lists =
            L.concatMap map2 lists

        map2 : List Float -> List (Html Msg)
        map2 floats =
            List.map (\coordinate -> li [] [ text (String.fromFloat coordinate) ]) floats
    in
    ol [ type_ "A" ]
        (List.concatMap map1 coordinates)
© www.soinside.com 2019 - 2024. All rights reserved.