如何映射列表编号?

时间:2019-02-25 22:29:27

标签: list filter functional-programming elm

我有一个包含字符串(玩家)和整数(强度)的列表。我实现了用, div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] )输出球员 现在,我还想输出存储在该记录/列表中的强度值。但是,获取参数错误。

  

map的第二个参数不是我期望的:

     

130 | ,div [](如果为model.activatedOutput,则为(List.map({   强度}-> div [] [文本强度])model.teams)else [])                                                                                                          ^^^^^^^^^^^^^ .teams的值为a:

List { activated : Bool, player : String, strength : Int }
     

但是map需要第二个参数为:

List { activated : Bool, player : String, strength : String }

我认为该错误是由于强度必须是要映射的字符串而引起的。但是我在视图中将其转换为字符串。所以我真的不知道这个错误的来源。

这是我代码的其他部分(导致错误的行是下面代码的最后一行):

-- MODEL
type alias Player =
  { player : String
  , strength : Int
  , activated : Bool
  }


type alias Model =
  { content : String
  , teams : List Player
  , currentPlayer : String
  , currentStrength : Int
  , activatedOutput : Bool
  }

-- UPDATE
...
 Add ->
      { model | teams = ({player = model.currentPlayer, strength = model.currentStrength, activated = True} :: model.teams), currentPlayer = "", currentStrength = 0 } 



init : Model
init =
  { content = ""
  , teams = []
  , currentPlayer = ""
  , currentStrength = 0
  , activatedOutput = False

   }
-- VIEW

view : Model -> Html Msg
view model =
  let
    playername = " Player " ++ String.fromInt (List.length model.teams + 1)
  in
  div []
    [ h1 [style "font-family" "impact"] [ text "Team Creator" ]
    , p [style "font-family" "sans-serif", style "font-size" "15px", style "color" "grey"] [ text "With the Team Creator you can create teams. Insert information about the name and the strength(1-5) of every player and finally how many teams you want to have created by the Team Creator" ]
    ,  h2 [style "font-family" "impact"] [ text "Number of Teams:" ]
    , input [ placeholder  "Number", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentNumber), onInput ChangeNumber] []
    ,  h2 [style "font-family" "impact"] [ text "Players per Team:" ]
    , input [ placeholder  "Playernumber", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentPlayernumber), onInput ChangePlayernumber] []
    ,  h2 [style "font-family" "impact"] [ text "Name and Strength:" ]
    , div[] [ input [placeholder  playername, style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#488aff", value model.currentPlayer, onInput ChangePlayer] [] ]
    , input [ placeholder  " Strength", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#4286F5", value (String.fromInt model.currentStrength), onInput ChangeStrength] []
    , div [] [ button [ style "background-color" "#66cc81", style "color" "white", style "margin-top" "20px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Add] [ text "+ADD Player" ] ]
    , div [] [ button [ style "background-color" "#4286F5", style "color" "white", style "margin-top" "10px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Submit] [ text "SUBMIT!" ] ]
    ,  h2 [style "font-family" "impact", style "margin-top" "20px"] [ text "Generated Teams:" ]
    , div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] )
    , div [] ( if model.activatedOutput then (List.map (\{ strength} -> div [] [ text strength ]) model.teams) else [] )
    ]

1 个答案:

答案 0 :(得分:1)

HTML.text本身不会将数字转换为String。它的类型为String -> Html msg,因此已经期望传递给它的参数为String。您需要先将其通过String.fromInt