Last active
July 2, 2017 02:00
-
-
Save jbrains/a913064a932c7cc6c3a5424957f7c409 to your computer and use it in GitHub Desktop.
REVIEW: creating optional HTML from a Result in Elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This becomes the typical pop-up error message at the top of an HTML page | |
failureView : Result String Int -> List (Html Update) | |
failureView possibleNumericInput = | |
case possibleNumericInput of | |
Err message -> | |
[ text message ] | |
Ok _ -> | |
[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- I like this version, but wonder if there's a more direct way. | |
-- I need the exact opposite of Result.withDefault that provides a default value for Ok instead of Err. | |
failureView : Result String Int -> List (Html Update) | |
failureView possibleNumericInput = | |
either | |
(text >> List.singleton) | |
(\_ -> []) | |
possibleNumericInput |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is pretty close to what I thought I wanted. | |
failureView : Result String Int -> List (Html Update) | |
failureView = | |
Exts.Result.fromErr >> Exts.List.maybeSingleton >> List.map Html.text |
Of course! Now I see Result.fromErr
. Can I do better?
Now that I've refactored a little, it's pretty good. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would prefer a more direct route: something that just turns
Result String Int
intoMaybe String
(not the more usualMaybe Int
) or that lets memap
a function over theErr
s and not theOk
s. Haskell hasEither.lefts
, which would give me a path:(I think that's right. I'm still a beginner here.)
Suggestions?!