I have a list containing strings(players) and integers(strengths). I achieved to output the players with , div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] )
Now I also wanted to output the strength value stored in this record/list. But Get an argument error.
The 2nd argument to map is not what I expect:
130| , div [] ( if model.activatedOutput then (List.map ({
strength} -> div [] [ text strength ]) model.teams) else [] )
^^^^^^^^^^^ The value at .teams is a:
List { activated : Bool, player : String, strength : Int }
But map needs the 2nd argument to be:
List { activated : Bool, player : String, strength : String }
I thought the error was due to the fact that strength has to be a string to map. But I had it converted into a String in the view. So I don't really get where this error comes from.
Here are some other parts of my code(The line actually causing the error is the last line in the code below):
-- 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 [] )
]
HTML.text does not by itself convert numbers to Strings. It has type String -> Html msg, therefore it already expects the argument passed to it to be a String. You need to pass it through String.fromInt first.
Related
I have model
type alias Model =
{
url : String
, devices : List Device
, input : String
, isFilter : Bool
, deviceSuggestion : Maybe Device
}
type alias Device =
{
status : String
, license_plate : String
, device_id : String
}
and below is my render view and i want to get the device_id and port to JS
renderPost : Device -> Html Msg
renderPost devices =
div []
[
[ text "Device_id : "
, button [ onClick PortDeviceID ] [ text (Debug.toString devices.device_id) ]
, pre [] [ text "device_model : " , text (Debug.toString devices.device_model) ]
, pre [] [ text "license_plate : " , text (Debug.toString devices.license_plate) ]
]
renderPosts : Model -> Html Msg
renderPosts model =
([ text "Device List" ] ++ List.map (\device -> renderPost device) model.devices)
and my updates is here. I have no idea how to get the text because the devices.device_id is from json file
PortDeviceID ->
( { model | deviceID = "" }, //port device_id --> not working)
GotResult result ->
case result of
Ok devices ->
( { model | devices = devices }, portDevice devices ) // this give full list
I want to do like , when i click the button then i can port the specific device_id to JS. The thing is it is in Device -> Html Msg format , so im stuck lol. Any ideas to do it? Any help is appreciate !
Case solved here,
in View , i do like below, instantiate the devices.device_id
renderPost : Device -> Html Msg
renderPost devices =
div []
[
[ text "Device_id : "
, button [ onClick (PortDeviceID devices.device_id)] [ text (Debug.toString devices.device_id) ]
Then in updates , same let the portDeviceID String
PortDeviceID deviceID->
( { model | deviceID = "" }, portDeviceID deviceID )
Anyway thanks for help Jack Leow
I've created a function that adds strings to a list, when a button is pressed.
Now the problem is that I can't Output the information that was pushed into the list.
I started to write a Submit function but I don't really get which model I have to include there. I want the output to be in the last div so there where now is "model.currentPlayer". In the Msg part I don't really get which variable I have to use there, since the full List should just be inserted in the div where I want to have my Output.
import Browser
import Html exposing (Html, Attribute, button, text, h1, h2, h3, p, div, input, text)
import Html.Attributes exposing (style)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Player =
{ player : String
, strength : Int
--, number : Int
--, playernumber : Int
--, placeholder : String
--, counter : Int
}
type alias Model =
{ content : String
, teams : List Player
, currentNumber : Int
, currentPlayernumber: Int
, currentPlayer : String
, currentStrength : Int
, placeholderPlayer : String
, placeholderCounter : Int
, placeholderStrength: Int
}
init : Model
init =
{ content = ""
, teams = []
, currentNumber = 0
, currentPlayernumber = 0
, currentPlayer = ""
, currentStrength = 0
, placeholderPlayer = ""
, placeholderCounter = 1
, placeholderStrength = 0
}
-- UPDATE
type Msg
= Change String
| ChangeNumber String
| ChangePlayernumber String
| ChangePlayer String
| ChangeStrength String
| Add
--| Submit String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
ChangeNumber number ->
{ model | currentNumber = Maybe.withDefault 0 (String.toInt number) }
ChangePlayernumber playernumber ->
{ model | currentPlayernumber = Maybe.withDefault 0 (String.toInt playernumber) }
ChangePlayer player ->
{ model | currentPlayer = player }
ChangeStrength strength ->
{ model | currentStrength = Maybe.withDefault 0 (String.toInt strength) }
Add ->
{ model | teams = ({player = model.currentPlayer, strength = model.currentStrength} :: model.teams), currentPlayer = "", currentStrength = 0 }
{- Submit player ->
{ model | teams = } -}
-- 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"{-, value model.teams, onClick Submit-}] [ text "SUBMIT!" ] ]
, h2 [style "font-family" "impact", style "margin-top" "20px"] [ text "Generated Teams:" ]
, div [] (List.map (\{ player } -> div [] [ text player ]) model.teams)
]
You could define a view function that prints the names of each player in a list of divs like so:
playerListView : List Player -> Html Msg
playerListView players =
let
playerRow player =
div [] [ text player.player ]
in
div [] (List.map playerRow players)
Using it in place of your last row in your example would look like this:
, playerListView model.teams
Or, if you want to keep it all in one line, the last line in your example could be:
, div [] (List.map (\{ player } -> div [] [ text player ]) model.teams)
How to add dictionary's key, value in array as array index form, like this
[
{
"summary": "fdsfvsd"
},
{
"content_date": "1510158480"
},
{
"content_check": "yes"
}
]
As per your question, you want to work with array of dictionaries in Swift,
here is the simple way to achieve the same :
var arrayOfDict = [[String: String]]()
//creating dictionaries
let dict1 = ["name" : "abc" , "city" : "abc1"]
let dict2 = ["name" : "def" , "city" : "def1"]
let dict3 = ["name" : "ghi" , "city" : "ghi1"]
let dict4 = ["name" : "jkl" , "city" : "jkl1"]
//Appending dictionaries to array
arrayOfDict.append(dict1)
arrayOfDict.append(dict2)
arrayOfDict.append(dict3)
arrayOfDict.append(dict4)
//accessing each and every element of the arrayOfDictionary
for dict in arrayOfDict{
for (key, value) in dict {
print("the value for \(key) is = \(value)")
}
}
Hope it helps you!
The problem with my shiny app is that it does not start anymore. It can be accessed by this link: https://buchmann.shinyapps.io/RevealedTechnologicalAdvantage/
It always worker properly but when I tried to start it today I got the error message:
An error has occurred
The application failed to start.
Loading required package: RCurl
Loading required package: bitops
Error in guess(varying) :
failed to guess time-varying variables from their names
Calls: local ... source -> withVisible -> eval -> eval -> reshape -> guess
Execution halted
I get the same when I start it in RStudio
I deployed it again but the result was the same.
Can anybody tell me what the error message means?
Thanks a lot!
Tobias
#
here is the code:
ui.r
require(rCharts)
options(RCHART_LIB = 'polycharts')
l = c("Electrical machinery, apparatus, energy",
"Audio-visual technology",
"Telecommunications",
"Digital communication",
"Basic communication processes",
"Computer technology",
"IT methods for management",
"Semiconductors",
"Optics",
"Measurement",
"Analysis of biological materials",
"Control",
"Medical technology",
"Organic fine chemistry",
"Biotechnology",
"Pharmaceuticals",
"Macromolecular chemistry, polymers",
"Food chemistry",
"Basic materials chemistry",
"Materials, metallurgy",
"Surface technology, coating",
"Micro-structural and nano-technology",
"Chemical engineering",
"Environmental technology",
"Handling",
"Machine tools",
"Engines, pumps, turbines",
"Textile and paper machines",
"Other special machines",
"Thermal processes and apparatus",
"Mechanical elements",
"Transport",
"Furniture, games",
"Other consumer goods",
"Civil engineering")
shinyUI(pageWithSidebar(
headerPanel("RTA by 'Schmoch field' Germany and Turkey"),
sidebarPanel(
selectInput(inputId = "field",
label = "Select field",
choices = l,
selected = "Electrical machinery, apparatus, energy")
),
mainPanel(
showOutput("chart2", "polycharts"),
showOutput("chart1", "polycharts")
)
))
server.r
share_share_de <- read.csv("share_share_de.csv", header = TRUE, sep = ",", check.names = FALSE)
share_share_tr <- read.csv("share_share_tr.csv", header = TRUE, sep = ",", check.names = FALSE)
require(rCharts)
options(RCHART_WIDTH = 800)
l = c("Electrical machinery, apparatus, energy",
"Audio-visual technology",
"Telecommunications",
"Digital communication",
"Basic communication processes",
"Computer technology",
"IT methods for management",
"Semiconductors",
"Optics",
"Measurement",
"Analysis of biological materials",
"Control",
"Medical technology",
"Organic fine chemistry",
"Biotechnology",
"Pharmaceuticals",
"Macromolecular chemistry, polymers",
"Food chemistry",
"Basic materials chemistry",
"Materials, metallurgy",
"Surface technology, coating",
"Micro-structural and nano-technology",
"Chemical engineering",
"Environmental technology",
"Handling",
"Machine tools",
"Engines, pumps, turbines",
"Textile and paper machines",
"Other special machines",
"Thermal processes and apparatus",
"Mechanical elements",
"Transport",
"Furniture, games",
"Other consumer goods",
"Civil engineering")
shinyServer(function(input, output) {
output$chart2 <- renderChart({
FIELD = as.character (which (l == input$field))
sub <- share_share_tr [, c ("Prio_Year", FIELD)]
colnames (sub) <- c("Prio_Year", "value")
p2 <- rPlot(value ~ Prio_Year, type = 'line', data = sub)
p2$guides(y = list(min = 0, title = ""))
p2$guides(y = list(title = ""))
p2$addParams(height = 300, dom = 'chart2', title = "Turkey")
return(p2)
})
output$chart1 <- renderChart({
FIELD = as.character (which (l == input$field))
sub <- share_share_de [, c ("Prio_Year", FIELD)]
colnames (sub) <- c("Prio_Year", "value")
p2 <- rPlot(value ~ Prio_Year, type = 'line', data = sub)
p2$guides(y = list(min = 0, title = ""))
p2$guides(y = list(title = ""))
p2$addParams(height = 300, dom = 'chart1', title = "Germany")
return(p2)
})
})
I wrote a program that selects random words from lists to make a sentence. I want to write grammar rules for this. Right now I am working on plurals.
I want to add an s or es to the end of the selected word in 'nouns' if the word "those" is selected from list5.
import random
class Wrds(object):
verbs = [
"walk", "run"
]
pronouns = [
"I", "you"
]
help_verbs = [
"will", "might"
]
nouns = [
"boy", "girl"
]
list5 = [
"the", "that", "this", "a", "those"
]
punctuation = [
".", "?", "!"
]
def result(self):
a = random.choice(Wrds.verbs)
b = random.choice(Wrds.pronouns)
c = random.choice(Wrds.help_verbs)
d = random.choice(Wrds.nouns)
e = random.choice(Wrds.list5)
f = random.choice(Wrds.punctuation)
print "%s %s %s %s %s%s" % (b, c, a, e, d, f)
def ask():
a = raw_input("> ")
if a == "go":
w = Wrds()
return w.result()
elif a == "exit":
exit()
while True:
ask()
Before the print statement in the result method, add:
if e == 'those':
d += 's'