This kind of problem (access to arbitrary elements) is usually better
solved by a Dict than a record. In this case, you could try something like:
type NumberText = FloatText String | IntText String
type alias Model = Dict Key NumberText
type Msg = UpdateField Key String
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateField key string ->
updateIfValid key string model
updateIfValid : Key -> String -> Model -> Model
updateIfValid key newString model =
case Dict.get key model of
Nothing ->
model
Just (IntText _) ->
if isValidIntString newString then
Dict.insert key (IntText newString) model
else
model
Just (FloatText _) ->
if isValidIntString newString then
Dict.insert key (FloatText newString) model
else
model
There is probably a clever way of removing the duplication in
updateIfValid. But the important thing is, this is not going to grow larger
the more fields that get added!
On Wed, Oct 19, 2016 at 8:18 AM, Brian Marick <[email protected]> wrote:
> I see I left out a required argument from what I want, which leads me to
> this:
>
>
> update : Msg -> Model -> Model
> update msg model =
> case msg of
> ChangedDripText string ->
> updateWhen model isValidFloatString dripText string
> ChangedHoursText string ->
> updateWhen model isValidIntString simulationHoursText string
> ChangedMinutesText string ->
> updateWhen model isValidIntString simulationMinutesText string
>
> dripText model val =
> { model | dripText = val }
> simulationHoursText model val =
> { model | simulationHoursText = val }
> simulationMinutesText model val =
> { model | simulationMinutesText = val }
>
> updateWhen : Model -> (String -> Bool) -> (Model -> String -> Model)
> -> String -> Model
> updateWhen model pred updater candidate =
> if pred candidate then
> updater model candidate
> else
> model
>
>
>
> … which is OK, but doesn’t take me to my happy place.
>
> --
> 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.
>
--
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.