I think I have something close to what you're looking for, but my solution 
makes me wonder, *why store text in your model rather than the parsed 
float/int values?* It seems like you want the parsed values for a 
calculation, not to format a paragraph of text.

update : Msg -> Model -> Model
update msg model =
  case msg of
    ChangeDripText dripText ->
      { model
        | dripText = valueOrDefault model.dripText String.toFloat dripText
      }

    ChangeHoursText simulationHoursText ->
      { model
        | simulationHoursText = valueOrDefault model.simulationHoursText 
String.toInt simulationHoursText
      }

    ChangeMinutesText simulationMinutesText ->
      { model
        | simulationMinutesText = valueOrDefault 
model.simulationMinutesText String.toInt simulationMinutesText
      }


valueOrDefault : a -> (a -> Result x b) -> a -> a
valueOrDefault default test value =
  test value
    |> Result.map (always value)
    |> Result.withDefault default

On Wednesday, October 19, 2016 at 10:49:03 AM UTC-4, Brian Marick wrote:
>
> I have a model that looks like this:
>
>     type alias Model =
>       { ...
>       , dripText : String
>       , simulationHoursText : String
>       , simulationMinutesText : String
>       ...
>       }
>
> Those strings each correspond to a text field containing floating point 
> numbers. The whole thing looks like this:
>
>
> The text fields ensure that the values are all valid when characters are 
> typed, using `onInput` messages.  I would very much like to write something 
> like:
>
>     — Ignore the fact that this `update` doesn’t return
>     — a `Cmd Msg`. That’s coming.
>     update : Msg -> Model -> Model
>     update msg model =
>       case msg of
>         ChangedDripText string ->
>           updateWhen isValidFloatString dripText string
>         ChangedHoursText string ->
>           updateWhen isValidIntString simulationHoursText string
>         ChangedMinutesText string ->
>           updateWhen isValidIntString simulationMinutesText string       
>
> … because that shows what’s different about each case. (Especially useful 
> is highlighting which fields accept integers and which floats.) 
>
> However, I think - based on 
> https://lexi-lambda.github.io/blog/2015/11/06/functionally-updating-record-types-in-elm/
>  
> - there’s no way to do that. Which has me writing this copypasta: 
>
>     updateNextSpeed model nextString =
>       if isValidFloatString nextString then
>         {model | dripText = nextString }
>       else
>         model
>   
>     updateHours model nextString =
>       if isValidIntString nextString then
>         {model | simulationHoursText = nextString } 
>       else
>         model
>
>     updateMinutes model nextString = 
>       if isValidIntString nextString then
>         {model | simulationMinutesText = nextString }
>       else
>         model
>   
>
>     update : Msg -> Model -> Model
>     update msg model =
>       case msg of
>         ChangedDripText string ->
>           updateNextSpeed model string
>         ChangedHoursText string ->
>           updateHours model string
>         ChangedMinutesText string ->
>           updateMinutes model string
>
> Is there a better way to handle this?
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to