/
CurrencyConverter.hs
54 lines (42 loc) · 1.74 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-----------------------------------------------------------------------------
reactive-banana-wx
Example: Currency Converter
------------------------------------------------------------------------------}
import Data.Maybe
import Text.Printf
import Graphics.UI.WX hiding (Event)
import Reactive.Banana
import Reactive.Banana.WX
{-----------------------------------------------------------------------------
Main
------------------------------------------------------------------------------}
main :: IO ()
main = start $ do
f <- frame [ text := "Currency Converter", tabTraversal := True ]
p <- panel f [] -- Use panel for tab traversal
dollar <- entry p []
euro <- entry p []
set p [layout := margin 10 $
column 10 [
grid 10 10 [[label "Dollar:", widget dollar],
[label "Euro:" , widget euro ]]
, label "Amounts update while typing."
]]
set f [layout := widget p]
focusOn dollar
let networkDescription :: MomentIO ()
networkDescription = do
euroIn <- behaviorText euro "0"
dollarIn <- behaviorText dollar "0"
let rate = 0.7 :: Double
withString f s
= maybe "-" (printf "%.2f") . fmap f
$ listToMaybe [x | (x,"") <- reads s]
-- define output values in terms of input values
dollarOut, euroOut :: Behavior String
dollarOut = withString (/ rate) <$> euroIn
euroOut = withString (* rate) <$> dollarIn
sink euro [text :== euroOut ]
sink dollar [text :== dollarOut]
network <- compile networkDescription
actuate network