-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.dy
56 lines (47 loc) · 965 Bytes
/
Calc.dy
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
54
55
56
module Calc
import Core.Function
import Core.IO
import Core.IO.DOM
import Core.IO.Unsafe
import Core.List
import Core.Math
import Core.String
setupBtn id text =
do r <- byId "result"
then el <- byId $ "b" + id
then onClick el $ updateValue r \v -> v + text
and
setupDigitBtn = show; join setupBtn and
setupDelBtn =
do r <- byId "result"
then el <- byId "bdel"
then onClick el $ updateValue r pop
and
setMsg text =
do msg <- byId "msg"
then setInner msg text
and
compute =
do r <- byId "result"
then v <- value r
then tryIO (unsafeEvalJS v) >>= \case
| Left e -> setMsg e
| Right n -> setValue r n *> setMsg ""
and
setupComputeBtn =
do el <- byId "beq"
then onClick el compute
and
symbols =
[ ("add", "+")
, ("sub", "-")
, ("mul", "*")
, ("div", "/")
, ("dot", ".")
]
and
main =
do setupDigitBtn `forEach` 0 .. 9
then uncurry setupBtn `forEach` symbols
then setupDelBtn
then setupComputeBtn