Usa mappa:
answer = Result.map2 (+) (String.toInt "1") (String.toInt "2")
map2:
Apply a function to two results, if both results are Ok. If not, the first argument which is an Err will propagate through.
di avere il risultato add
come una stringa
resultAsString r =
case r of
Err msg -> msg
Ok value -> toString value
resultAsString answer
Per rendere le cose più facili è possibile creare una funzione addStrings
:
addStrings : String -> String -> Result String Int
addStrings a b =
Result.map2 (+) (String.toInt a) (String.toInt b)
È possibile anche ottenere via con il tipo Result
del tutto:
addStrings : String -> String -> String
addStrings a b =
let
r =
Result.map2 (+) (String.toInt a) (String.toInt b)
in
case r of
Err msg ->
msg
Ok value ->
toString value
Testing
import Html exposing (Html, text)
main : Html msg
main =
text (addStrings "1" "2")
output 3