Elm's Time is not Precise, is it? - time

I've been toying with Effects.tick and Time in one of my apps, and I can't seem to get my delays to be around the Time.second that it claims to be. It's nearly immediate. I understand that type alias Time = Float, and from my logs it seems that second = 1000, but this just burns so quickly, even with logging. Is there a clear explanation for this?

Effects.tick causes an action to be called nearly instantaneously, but the action that is being called gets passed a value of the current time of the tick. If you want to delay something by a second while using Effects.tick, you'll have to keep track of a starting point and compare it to the time of the current tick, and that's where you can add in Time.second.
Take this arbitrary example (you can paste it into http://elm-lang.org/try):
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import StartApp
import Effects exposing (Never)
import Task
import Signal
import Time exposing (..)
app =
StartApp.start
{ init = init
, view = view
, update = update
, inputs = [ ]
}
main =
app.html
type alias Model =
{ lastTick : Maybe Time
, tickEverySecond : Maybe Time
}
init =
({ lastTick = Nothing, tickEverySecond = Nothing }, Effects.tick MyTicker)
view address model =
div []
[ div [] [ text <| "Current tick: " ++ (toString model.lastTick) ]
, div [] [ text <| "Updated every second: " ++ (toString model.tickEverySecond) ]
]
type Action
= MyTicker Time
update action model =
let
everySecond = Maybe.withDefault 0 model.tickEverySecond
getTickEverySecond time =
if time > everySecond + Time.second then
Just time
else
Just everySecond
in
case action of
MyTicker time -> (
{ model | lastTick = Just time
, tickEverySecond = getTickEverySecond time
}, Effects.tick MyTicker)
port tasks : Signal (Task.Task Never ())
port tasks =
app.tasks
Every update call requests a new tick, so it will just spin forever. The important part is that tickEverySecond is only updated if the last time it was updated is greater than one second ago.
You ask about the time precision. If you run that example, you'll notice that the increment every second is somewhat approximate; it will drift greater than a second. That isn't due to any kind of underlying imprecision. Remember that Elm's Time functionality is a thin veneer over Javascript's time and timer functionality. That drift is merely an artifact of all the tiny little delays caused by running code that responds to a timer signal.

Related

can we modify value in django queryset using Django "F" expression . Query optimisation

now = datetime.utcnow().replace(tzinfo=utc)
.annotate(
age=F(int((now - 'ended_at').total_seconds() / (60 * 60)))
I want to add logic like above in Django query.
basically i want to calculate "age" that's a reason need to perform operation ORM.
having large data and its taking time if I do this operation using for loop.
First define a Func to extract the number of seconds since the UNIX epoch.
from django.db.models import Func, IntegerField
class UnixTime (Func):
"""
Extract the number of seconds since January 1, 1970.
"""
arity = 1
output_field = IntegerField()
# for PostgreSQL
def as_sql(self, compiler, connection, **extra_context):
return super().as_sql(
compiler, connection,
template="EXTRACT(EPOCH FROM %(expressions)s)",
**extra_context)
def as_mysql(self, compiler, connection, **extra_context):
return super().as_sql(
compiler, connection,
template="UNIX_TIMESTAMP(%(expressions)s)",
**extra_context)
def as_sqlite(self, compiler, connection, **extra_context):
return super().as_sql(
compiler, connection,
template="CAST(strftime('%%%%s', %(expressions)s) AS INTEGER)",
**extra_context)
Then make a query like this:
from django.db.models import F
from django.db.models.functions import Now
YourObject.objects.annotate(
age=(UnixTime(Now()) - UnixTime(F('ended_at'))) / 3600
)

Follow up question: Modelica total time calculation of simulation and equation initialization

I am writing this question related to this. In his reply, Marco gave me an excellent answer but, unfortunately, I am new with OpenModelica so I would need some further help.
I am actually using OpenModelica and not Dymola so unfortunately I have to build the function that does it for me and I am very new with OpenModelica language.
So far, I have a model that simulates the physical behavior based on a DAEs. Now, I am trying to build what you suggest here:
With get time() you can build a function that: reads the system time as t_start translates the model and simulate for 0 seconds reads the system time again and as t_stop computes the difference between t_start and t_stop.
Could you please, give me more details: Which command can I use to read the system at time t_start and to simulate it for 0 seconds? To do this for both t_start and t_stop do I need to different function?
Once I have done this, do I have to call the function (or functions) inside the OpenModelica Model of which I want to know its time?
Thank you so much again for your precious help!
Very best regards, Gabriele
From the other question:
I noticed in Modelica there are different flags for the simulation time but actually the time I get is very small compared to the time that elapses since I press the simulation button to the end of the simulation (approximately measured with the clock of my phone).
The time that is reported is correct. Most of the time taken is not initialisation or simulation, but compilation. If you use the re-simulate option in OMEdit (right-click a result-file in the plot view for variables), you will notice the simulation is very fast.
$ cat e.mos
loadString("model M
Real r(fixed=true, start=2.0);
equation
der(r) = time;
end M;");getErrorString();
simulate(M);getErrorString();
$ omc e.mos
true
""
record SimulationResult
resultFile = "/mnt/data/#Mech/martin/tmp/M_res.mat",
simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'M', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
LOG_SUCCESS | info | The simulation finished successfully.
",
timeFrontend = 0.004114061,
timeBackend = 0.00237546,
timeSimCode = 0.0008126780000000001,
timeTemplates = 0.062749837,
timeCompile = 0.633754155,
timeSimulation = 0.006627571000000001,
timeTotal = 0.7106012479999999
end SimulationResult;
""
OMEdit does not report these other numbers (time to translate and compile the model) as far as I know. On Windows, these times are quite big because linking takes longer.

Set current time in init ELM 0.19

I get input JSON data from JS. This is a simple object, among which there is a date in the format "DD.MM.YYYY" - just a string.
If there is no dateStart in the object, I have to replace it with the current date (in withDefault).
paramsDecoder : Decode.Decoder Params
paramsDecoer =
Decode.succeed Params
|> Decode.andMap (Decode.field "dateStart" (Decode.string) |> (Decode.withDefault) "")
|> Decode.andMap (Decode.field "dateEnd" (Decode.string) |> (Decode.withDefault) "")
|> Decode.andMap (Decode.field "country" (Decode.string) |> (Decode.withDefault) "spain")
How can I do this in ELM?
Timezone is not important and is always equal to the same region.
I found an example of Time.now Time.zone usage,
but there time is getting in Update and its too late.
I solved this in two parts:
Part 1
Send the current time in to Elm when it initializes, using flags:
Elm.Main.init({flags: Date.now()});
And put it in your model:
import Time exposing (Posix, millisToPosix)
type alias Model = { now : Time.Posix, ... }
init : Int -> (Model, Cmd Msg)
init time = (Model (millisToPosix time), Cmd.none)
For your use case, you can then use withDefault model.now.
Part 2
The solution in Part 1 will only set now to the time when the page is loaded.
If you want to keep an up to date time you can use Time.every to update your model:
import Time exposing (Posix, millisToPosix, every)
timeOutCheck : Sub Msg
timeOutCheck = Time.every 250 UpdateTimeNow
type Msg = UpdateTimeNow Posix | ...
update msg model = case msg of
UpdateTimeNow time = { model | now = time }
...
This will ensure now is never more than 250 ms behind the current time. You can change 250 to suit your need.

Elm - Random Number on init strange behaviour

Just working through the samples, and got the exercise about creating 2 random dice and rolling them with a button.
http://guide.elm-lang.org/architecture/effects/random.html
So I thought I would create the dice as a module, remove the roll action, and just have it create a D6 value on init.
So my code is now as follows (should open direct in elm-reactor)
module Components.DiceRoller exposing (Model, Msg, init, update, view)
import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Random
import String exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ dieFace : Int
}
init : ( Model, Cmd Msg )
init =
( Model 0, (Random.generate NewFace (Random.int 1 6)) )
-- UPDATE
type Msg
= NewFace Int
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewFace newFace ->
( Model newFace, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
dieFaceImage : Int -> String
dieFaceImage dieFace =
concat [ "/src/img/40px-Dice-", (toString dieFace), ".svg.png" ]
view : Model -> Html Msg
view model =
let
imagePath =
dieFaceImage model.dieFace
in
div []
[ img [ src imagePath ] []
, span [] [ text imagePath ]
]
The problem with this is that it is always producing the same value. I thought I had a problem with the seed to begin with, but if you change
init =
( Model 0, (Random.generate NewFace (Random.int 1 6)) )
init =
( Model 0, (Random.generate NewFace (Random.int 1 100)) )
it works exactly as intended. So it looks like the default generator is not working with small values, seems to work as low down as 10.
The odd thing is this, in this example (which i started with) http://guide.elm-lang.org/architecture/effects/random.html , it works fine with 1-6 when it's not in init.
So my question is, am I doing something wrong, or is this just a wrinkle in elm? Is my usage of the command in init ok?
In the end, I put this in to get the desired effect, which feels wonky.
init =
( Model 0, (Random.generate NewFace (Random.int 10 70)) )
with
NewFace newFace ->
( Model (newFace // 10), Cmd.none )
This must to have something to do with seeding. You're not specifying any value for seed so the generator is defaulting to use the current time.
I think you tried to refresh your page for a few times in a few seconds and you didn't see the value change. If you wait for longer (roughly a minute) you'll see your value change.
I had a look at the source code of Random and I suspect that for seed values that are close enough the first value generated in the range [1,6] doesn't change. I'm not sure whether this is expected or not, probably it's worth raising an issue on GitHub

Fields and Events in Elm 0.13

This question somehow relates to How to use Fields in Elm 0.13 (and we are working on the same assignment).
In my case, everything that changes in the game should generate a Event:
data Event = NewGame
| PauseGame
| Turn [KeyCode]
| PlayerActive [Bool]
| PlayerChanged [PlayerSettings]
| Tick Time
The PlayerChanged event, for example, is generated as follows:
settingsSignal : Signal Event
settingsSignal =
lift PlayerChanged <|
combine [ pOneSettingsSignal, pTwoSettingsSignal
, pThreeSettingsSignal, pFourSettingsSignal]
pOneSettingsSignal : Signal PlayerSettings
pOneSettingsSignal =
PlayerSettings <~ (Controls <~ lift toCode pOneUp.signal
~ lift toCode pOneDown.signal
~ lift toCode pOneLeft.signal
~ lift toCode pOneRight.signal)
~ (pOneColor.signal)
~ (lift contentToString pOneName.signal)
where pOne<Direction> and pOneColor are inputs for dropDowns and pOneName is an Input for a Input.Field:
pOneName : Input Content
pOneName = input (Content nameOne (Selection 0 0 Forward))
I hereby assume that when I update the text field (or a dropdown), a PlayerChanged [PlayerSettings] event will be generated. All event get combined as follows:
eventSignal : Signal Event
eventSignal = merges [ clockSignal
, newGameSignal
, pauseGameSignal
, turnSignal
, activeSignal
, settingsSignal ]
and finally the event signal is lifted onto an update function:
gameState : Signal Game
gameState =
foldp update initialGame eventSignal
main = lift2 view gameState Window.dimensions
However, when the game is paused, it seems that all input is blocked and no signals propagate anymore. Also, the text fields are uneditable. Each Input.Field for the player names is defined as follows:
playerOneName : String -> Element
playerOneName name =
field defaultStyle pOneName.handle identity nameOne
(Content name (Selection 0 0 Forward))
and then, in the view function
pOneNameField = playerOneName playerOne.settings.name
To me it seems like everything is correct. When editing the displayed field, pOneName.signal is changed, which causes (trough a series of liftings) a PlayerChanged event to be generated, which causes the model to be redrawn, which should show the new update. Somehow the Input.Field is still uneditable, however. Worse: even a simple NewGame event isn't generated anymore and events from the dropdowns are also not propagated: nothing changes.
newGameSignal : Signal Event
newGameSignal =
always NewGame <~ (keepIf identity False <| space)
If I start the game in playing mode (thus no input forms are shown), this works fine and I can reset the game. It seems like the inputs are blocking the event stream, but I can't figure out where its going wrong.
So, I fixed it.
Appearantly the
case (event, game.state) of
(NewGame, _) ->
...
(Tick t, Playing) ->
statement is blocking, so it didn't match Tick Time signals sent in the Paused and Ended states and blocked there. A simple
(_, _) -> game
case fixed everything, so my 960 lines of Elm don't go to waste!

Resources