Last active
September 7, 2017 03:20
-
-
Save jhrcek/ca5d15140164eaa655ea122b3c3cfcd2 to your computer and use it in GitHub Desktop.
How to get info about which of the cases is currently "selected" more conscisely?
This file contains 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
type LoanTerm | |
= LessThan Int | |
| Between Int Int | |
| MoreThan Int | |
isLess : LoanTerm -> Bool | |
isLess amt = | |
case amt of | |
LessThan _ -> | |
True | |
_ -> | |
False | |
isBetween : LoanTerm -> Bool | |
isBetween amt = | |
case amt of | |
Between _ _ -> | |
True | |
_ -> | |
False | |
isMore : LoanTerm -> Bool | |
isMore amt = | |
case amt of | |
MoreThan _ -> | |
True | |
_ -> | |
False | |
-- I don't like the boilerplate above just to be able to determine boolean flag for radio buttons. Is there more conscise way | |
loanTermForm : LoanTerm -> Html Msg | |
loanTermForm term = | |
div [] | |
[ radio [enabled (isLess term)] [{- ..radio for Less than case-}] | |
, radio [enabled (isBetween term)] [{- ..radio for Between than case-}] | |
, radio [enabled (isMore term)] [{- ..radio for More than case-}] | |
] | |
-- EDIT 1: More conscise alternative to the three isLess, isBetween, isMore above: | |
whichEnabled : Amount -> ( Bool, Bool, Bool ) | |
whichEnabled amt = | |
case amt of | |
LessThan _ -> | |
( True, False, False ) | |
Between _ _ -> | |
( False, True, False ) | |
MoreThan _ -> | |
( False, False, True ) | |
-- EDIT 2: The best solution so far | |
type TermComp | |
= Less | |
| Betw | |
| More | |
toComp : LoanTerm -> TermComp | |
toComp term = | |
case term of | |
LessThan _ -> | |
Less | |
Between _ _ -> | |
Betw | |
MoreThan _ -> | |
More | |
loanTermForm : LoanTerm -> Html Msg | |
loanTermForm term = | |
div [] | |
[ radio [ enabled (toComp term == Less) ] [{- ..radio for Less than case -}] | |
, radio [ enabled (toComp term == Betw) ] [{- ..radio for Between than case -}] | |
, radio [ enabled (toComp term == More) ] [{- ..radio for More than case -}] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment