forked from HeinrichApfelmus/threepenny-gui
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CurrencyConverter.hs
40 lines (31 loc) · 1.16 KB
/
CurrencyConverter.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Control.Monad (void)
import Data.Maybe
import Text.Printf
import Safe (readMay)
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
{-----------------------------------------------------------------------------
Main
------------------------------------------------------------------------------}
main :: IO ()
main = startGUI defaultConfig setup
setup :: Window -> UI ()
setup window = void $ do
return window # set title "Currency Converter"
dollar <- UI.input
euro <- UI.input
getBody window #+ [
column [
grid [[string "Dollar:", element dollar]
,[string "Euro:" , element euro ]]
, string "Amounts update while typing."
]]
euroIn <- stepper "0" $ UI.valueChange euro
dollarIn <- stepper "0" $ UI.valueChange dollar
let
rate = 0.7 :: Double
withString f = maybe "-" (printf "%.2f") . fmap f . readMay
dollarOut = withString (/ rate) <$> euroIn
euroOut = withString (* rate) <$> dollarIn
element euro # sink value euroOut
element dollar # sink value dollarOut