Hi Austin, I would suggest creating an ArpsParameters module with its own
Model and Msg types, update function and so on, then nesting that module
within your main model. Then repeat the same process and create a Forecast
module. This way you won't need to access deeply nested fields and your
modules will be reusable elsewhere in your app.
Check out this section of the guide for explanation of nesting and reusing
modules in Elm
<http://guide.elm-lang.org/architecture/modularity/counter_pair.html>
вторник, 2 августа 2016 г., 3:18:17 UTC+3 пользователь Austin B написал:
>
> Hi,
>
> I come from an OOP background and trying to make the leap to keeping all
> state in a single immutable data store, which has shown to be amazing as
> long as the system is small, but I am having trouble scaling up to anything
> beyond a toy app.
>
> One big challenge is trying to update nested fields. For example if I want
> to change a forecast parameter in the following code:
>
> -- Model
> type alias Forecast =
> { parameters: Pf.ArpsParameters
> , production: List Float
> }
> type alias Model =
> { primaryForecast: Forecast
> , secondaryForecast: Forecast
> }
>
> -- Init
>
> init : (Model, Cmd Msg)
> init =
> (
> { primaryForecast =
> { parameters =
> { q = Nothing
> , de = Nothing
> , c = Nothing
> , months = [0..48]
> }
> , production = []
> }
> }
> , Cmd.none
> )
>
>
> I end up with code like this:
>
> update : Msg -> Model -> (Model, Cmd Msg)
> update msg model =
> let
> primaryForecast = model.primaryForecast
> parameters = model.primaryForecast.parameters
> in
> case msg of
> Q string -> ({ model | primaryForecast = { primaryForecast |
> parameters = { parameters | q = toMaybe (String.toFloat string) } } },
> message UpdateProduction)
> De string -> ({ model | primaryForecast = { primaryForecast |
> parameters = { parameters | de = toMaybe (String.toFloat string) } } },
> message UpdateProduction)
> C string -> ({ model | primaryForecast = { primaryForecast |
> parameters = { parameters | c = toMaybe (String.toFloat string) } }},
> message UpdateProduction)
> UpdateProduction -> ...
>
>
> This seems unnecessarily verbose, and I'm unsure whether I am turning
> wrong with how I am structuring data or if I am trying to modify data
> incorrectly.
>
> Thanks for any help!
>
> Austin
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.