I'm doing the Elm Guide exercise from Random Section and got stuck in step 5, where I have to flip around the dices before they settle on a final Value. I'm trying to do this calling the Cmd, that updates the view with the new values of the dices, 10 times with just one click of the button "Roll!" and put some kind of sleep function before each iteraction. For what I've read, Elm does not have a for, loop or while statement. The only way to loop is through recursion, but I'm not being able to adapt my code for it. My code below, does anyone have a suggestion?
import Browser
import Html exposing (..)
import Html.Events exposing (..)
import Svg exposing (..)
import Svg.Attributes as SvgAttr exposing (..)
import Random
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ dieFace : Int
, dieFace2 : Int
}
init : () -> (Model, Cmd Msg)
init _ =
( Model 1 1
, Cmd.none
)
-- UPDATE
type Msg
= Roll
| NewFace (Int, Int)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
( model
, Random.generate NewFace randomPair
)
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Cmd.none
)
weightedRoll : Random.Generator Int
weightedRoll =
Random.weighted
(1, 1)
[ (10, 2)
, (10, 3)
, (10, 4)
, (20, 5)
, (40, 6)
]
randomPair : Random.Generator (Int, Int)
randomPair =
Random.pair (Random.int 1 6) (Random.int 1 6)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [] [ Html.text (String.fromInt model.dieFace) ]
, svg
[ width "120"
, height "120"
, viewBox "0 0 120 120"
]
(List.append
[ rect
[ x "10"
, y "10"
, width "100"
, height "100"
, rx "15"
, ry "15"
]
[]
]
(dicesSVG model.dieFace)
)
, div []
[ h1 [] [Html.text (String.fromInt model.dieFace2) ]
, svg
[ width "120"
, height "120"
, viewBox "0 0 120 120"
]
(List.append
[ rect
[ x "10"
, y "10"
, width "100"
, height "100"
, rx "15"
, ry "15"
]
[]
]
(dicesSVG model.dieFace2)
)]
, button [onClick Roll] [Html.text "Roll!"]
]
dicesSVG : Int -> List (Svg Msg)
dicesSVG number =
case number of
1 ->
[ circle [cx "60", cy "60", r "10", fill "white" ] [] ]
2 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
3 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "60", cy "60", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
4 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
5 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "60", cy "60", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
6 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "35", cy "60", r "10", fill "white" ] []
, circle [ cx "85", cy "60", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
_ ->
[]
The trick is mostly in asking the runtime to send you messages.
The first thing you could try is changing
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Cmd.none
)
to
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Random.generate NewFace randomPair
)
This has the problem of never stopping, making your program spin around forever...
So you will need to track some stopping condition. Perhaps add a field to your model that tracks how many times it has rolled already? Then switching between Random.generate and Cmd.none based on that field.
Finally if you want a time delay between the rolls, then
import Task
wait : Float -> msg -> Cmd msg
wait delay msgToCallWhenDone =
Task.perform (always msg) (Process.sleep delay)
will give you a Cmd that will call whatever msg you give it after delay milliseconds. As a hint, you might want to introduce another msg for this one.
Related
=============== MyManualController.php ===============
$mCC = $this->getDoctrine()->getManager();
$qCC = "CONFLICT QUERY HERE...";
$xCC = $mCC->getConnection()->prepare($qCC);
$xCC->execute();
$sCC =
$xCC->fetchAll();
=============== Current Output ===============
[{
"emp_name": "Sample"
"adjustment": "0",
"by_days": "10",
"mos": "323",
"by_mos": "0",
"yrs": "27",
"by_yrs": "0"
}]
=============== Expected Output ===============
[{
"emp_name": "Sample"
"adjustment": 0,
"by_days": 10,
"mos": 323,
"by_mos": 0,
"yrs": 27,
"by_yrs": 0
}]
follow link https://symfony.com/doc/current/doctrine.html
you can create entity, or, follow doc for create manually class with example
This is my script:
def pick_partOf($uids):
(reduce $uids[] as $uid (
{};
. + ($uid | {(.oid1): .id})
)) as $dict
| map(. + { partOf: $dict[."parent-identifier-value"] } | del(..|nulls));
def pick_organization:
{
resourceType: "Organization",
active: true,
partOf: .partOf?
};
pick_partOf($identifiers) | pick_organization
Problem here, is that sometimes some input objects doesn't contains ."parent-identifier-value", and I'm getting:
jq: error (at rsan.json:7): Cannot index object with null
Those are my input objects:
{
"identifier-value": "61",
"name": "name61"
}
{
"id": "62",
"name": "name62",
"parent-identifier-value": "61"
}
Currently, I'm launching jq using empty array as $identifiers arg:
jq -s -f build-organization-bundle.jq --argjson identifiers '[]' rsan.json
However, my identifiers is an other json files like this:
{
"id": "be8a02b6-54b9-450f-bf45-f28ab7ebf2dd",
"oid1": "61"
}
{
"id": "f1652f3a-bca9-433c-a2c4-c924811196f6",
"oid1": "62"
}
I've tried with:
def pick_partOf($uids):
(reduce $uids[] as $uid (
{};
. + ($uid | {(.oid1): .id})
)) as $dict
| map(. + (
if (.|has("parent-identifier-value")) then
{ partOf: $dict[."parent-identifier-value"] }
else null end
))
| del(..|nulls));
But here I'm facing with some syntax issue I don't quite figure out how to fix it.
You did not specify why you want pick_partOf to achieve.
If its output should be
{
"identifier-value": "61",
"name": "name61"
}
{
"id": "62",
"name": "name62",
"parent-identifier-value": "61",
"partOf": "be8a02b6-54b9-450f-bf45-f28ab7ebf2dd"
}
then you can use
if ."parent-identifier-value" then
.partOf = $dict[."parent-identifier-value"]
else
.
end
Demo on jqplay
Repeating ."parent-identifier-value" was annoying me.
."parent-identifier-value" as $piv |
if $piv then .partOf = $dict[$piv] else . end
Demo on jqplay
I am using nvd3 to create a historical barchart and it's working fine. There are two things I would like to do with the bars.
style the bars with rounded edges instead of square.
colour the bars based on their values
For the second case, I tried a function as follows and it doesn't work.
function(d,i) {
return d.y > 50? "red":"blue";
}
Updated:
This is the data that I am using. I am just trying to colour the bars based on the value. So if the data value is more than 50, it should colour the bar red. As for the other question, I just want to style them with rounded edges.
data = [{
"values" : [
[ 1136005200000 , 17.0] , [ 1138683600000 , 12.0] , [ 1141102800000 , 12.0] , [ 1143781200000 , 14] ,
[ 1146369600000 , 20] , [ 1149048000000 , 21] , [ 1151640000000 , 17] , [ 1154318400000 , 34] , [ 1156996800000 , 10] ,
[ 1159588800000 , 8.0] , [ 1162270800000 , 38.0] , [ 1164862800000 , 38.0] , [ 1167541200000 , 35.0] ,
[ 1170219600000 , 55.0] , [ 1172638800000 , 35.0] , [ 1175313600000 , 26.0] , [ 1177905600000 , 26.0] ,
[ 1180584000000 , 26.0] , [ 1183176000000 , 25.0] , [ 1185854400000 , 25.0] , [ 1188532800000 , 25.0] ,
[ 1191124800000 , 29.0] , [ 1193803200000 , 29.0] , [ 1196398800000 , 29.0] , [ 1199077200000 , 52.0] ,
[ 1201755600000 , 22.0] , [ 1204261200000 , 22.0] , [ 1206936000000 , 22.0] , [ 1209528000000 , 22.0]
}];
For example, this code draws a different shade of blue depending on the x-value, and grey if the y-value is zero:
d3.selectAll("rect.nv-bar").style("fill", function(d,i){
var colorCode = "#1f77b4";
var date = new Date(d.x * 1000);
var hours = date.getUTCHours();
if(hours < 6 || hours >= 18){
colorCode = "#355880";
}
if(d.y === 0){
colorCode = "grey";
}
return colorCode;
});
In QML I have a QComboBox:
ComboBox {
id: myCBox
model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]
}
When I try to find an element in the ComboBox by text it returns -1.
Log with this:
console.log(myCBox.find("5"))
And it outputs -1 (which means not found).
QML QComboBox documentation
You should check, when do you the call myCBox.find, look at this code:
ComboBox {
id: myCBox
model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]
Component.onCompleted: {
console.log("After this line it should work fine.");
}
Item {
Component.onCompleted: {
console.log("myCBox is not completed:", myCBox.find("5"));
}
}
}
Component.onCompleted: {
console.log("myCBox here must be completed:", myCBox.find("5"));
}
and the output:
myCBox is not completed: -1
After this line it should work fine.
myCBox must be completed: 6
Uppon creation, Component creates all items and then arranging them in a tree. From the most inner object, it updates properties and bindings to the most overrided value and calls attached method Component.onCompleted.
I have a 2-dimensional array of string I need to add items to this array based on certain conditions.
Dim mainColumnsSummary(,) As String
mainColumnsSummary = { _
{"slNo", "#", "Number", "30", True, ""}, _
{"assessmentDate", "Assessment Date", "DateTime", "100", True, ""}, _
{"assetDescription", "Description and function of asset", "String", "100", True, ""}, _
{"assetScope", "Scope of assessment", "String", "100", True, ""}, _
{"assetHazards", "Hazard identification", "String", "100", True, ""} _
} if dtTable.rows.count>0 then
' I need to add dtTable.rows(x)("Question") to this array. where x should take values from 0 to dtTable.row.count-1
How can i get this result.
Please help me with code in vb.net.
A .NET array is a data structure with a fixed length. Once created, the .Length property tells you how much elements there are in the array, and the length is constant. You cannot add to an array.
You need to look at System.Collections.Generic.List(Of T).
My VB is not very good, but try something like this:
Dim mainColumnsSummary As New List(Of String())
mainColumnsSummary.Add({"slNo", "#", "Number", "30", True, ""})
You should then be able to add as many "rows" to this list as you want.