如何映射列表的数字?

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

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

map的第二个论点不是我所期望的:

130 | ,div [](如果是model.activatedOutput那么(List.map({strength} - > div [] [文字强度])model.teams)else [])^^^^^^^^^^^ .teams是:

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

map需要第二个参数:

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

我认为错误是由于强度必须是要映射的字符串。但是我在视图中将它转换为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 [] )
    ]
list filter functional-programming elm
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.