Working with SartApp and Signals

84 προβολές
Παράβλεψη και μετάβαση στο πρώτο μη αναγνωσμένο μήνυμα

Alex Spurling

μη αναγνωσμένη,
30 Ιουλ 2015, 7:08:05 π.μ.30/7/15
ως Elm Discuss
I'm very much still learning Elm and I'm not yet familiar with Signals so my approach here may well be not the right one. Ok so here's the question.

I've written an app that uses StartApp. I also want to wire in an initial Random.Seed based on the application start time. I've attempted to use this example for doing the random seed:


This code creates a Signal of Seeds:

seed : Signal Random.Seed
seed = (\ (t, _) -> Random.initialSeed <| round t) <~ Time.timestamp (Signal.constant ())


My application uses StartApp.start which returns a Signal of Html. I thought perhaps I could combine the two Signals together but it doesn't seem like there is any mapping function that can do this. Here is my attempt to use Signal.map:


main : Signal Html
main = Signal.map (\seed -> StartApp.start (mainApp seed)) seedSignal



And this gives the compile error:

The type annotation for `main` does not match its definition.

59| main : Signal Html
           ^^^^^^^^^^^
As I infer the type of values flowing through your program, I see a conflict
between these two types:

    Html

    Signal Html


Ultimately I'd like to be able to receive a Seed inside my initialModel function and continue to use StartApp. Is this possible to do?

Alex

Jeff Smits

μη αναγνωσμένη,
30 Ιουλ 2015, 7:20:07 μ.μ.30/7/15
ως elm-discuss

StartApp is meant to give you a boost to quickly get a little app. But if you want to grow bigger than that and deal with your own signals, you’ll need to write the glue code yourself.
I advise you to take the code from StartApp.start, copy-paste that into your project, and adapt it to also add in the random seed. You can wrap the seed in an Action and merge that signal into actions.signal for example.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

μη αναγνωσμένη,
30 Ιουλ 2015, 11:07:40 μ.μ.30/7/15
ως Elm Discuss, jeff....@gmail.com
That being said, grabbing a new random seed from the timestamp is not particularly random. I've seen sequential seeds give almost the same results. So, if it matters to you -- and it may not -- the proper way to do this is to use only one initial seed, threading and saving all the other seeds. Getting that initial seed involves either hard-coding an integer, or learning about ports to accept a random value from JavaScript. So, yes: time to move beyond StartApp.

And again, maybe this is helpful, maybe it's a distraction, so please prioritize for yourself.
Απάντηση σε όλους
Απάντηση στον συντάκτη
Προώθηση
0 νέα μηνύματα