Elm:如何在嵌套列表上应用 List.map
Elm : How to apply List.map on Nested List
这是我用简单的 List Float 完成的基本工作
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))
列表中的每个项目...
感谢您的帮助!
更新
在@Simon 的帮助下
我对代码进行了一些修改,使其工作方式如下:
renderCoordinates : List (List (List Float)) -> Html Msg
renderCoordinates coordinates =
let
map1 : List (List (List Float)) -> List (Html Msg)
map1 lists =
List.concatMap map2 lists
map2 : List (List Float) -> List (Html Msg)
map2 floats =
List.map (\coordinate -> li [] [ text (Debug.toString coordinate) ]) floats
in
ol [ type_ "A" ]
(map1 coordinates)
这将打印 A: [X, Y]
你需要分部分进行,并在每个阶段连接结果
renderCoordinates : List (List (List Float)) -> Html Msg
renderCoordinates coordinates =
let
map1 : List (List Float) -> List (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)
这是我用简单的 List Float 完成的基本工作
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))
列表中的每个项目...
感谢您的帮助!
更新
在@Simon 的帮助下 我对代码进行了一些修改,使其工作方式如下:
renderCoordinates : List (List (List Float)) -> Html Msg
renderCoordinates coordinates =
let
map1 : List (List (List Float)) -> List (Html Msg)
map1 lists =
List.concatMap map2 lists
map2 : List (List Float) -> List (Html Msg)
map2 floats =
List.map (\coordinate -> li [] [ text (Debug.toString coordinate) ]) floats
in
ol [ type_ "A" ]
(map1 coordinates)
这将打印 A: [X, Y]
你需要分部分进行,并在每个阶段连接结果
renderCoordinates : List (List (List Float)) -> Html Msg
renderCoordinates coordinates =
let
map1 : List (List Float) -> List (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)