Ur/Web Examples
Check-in [904ba75c5b]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:in composableSignals, use better examples / make examples more consistent, particuarly showing that pure/return can be used as a constructor for signals
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:904ba75c5bafafc050f4e5552d38c73bc20f2eb0
User & Date: beyert 2016-08-17 07:23:19
Context
2016-08-17
07:33
further improve examples in composableSignals check-in: 598aee9f68 user: beyert tags: trunk
07:23
in composableSignals, use better examples / make examples more consistent, particuarly showing that pure/return can be used as a constructor for signals check-in: 904ba75c5b user: beyert tags: trunk
05:59
make composableSignals demo interactive to better illustrate the benefits of signal lifting check-in: c4437d6ed0 user: beyert tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to composableSignals/composableSignals.ur.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
52
53
54
55
56
57
58

59
60
61
62
63
64
65
..
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92




93
94
95
96
97
98
99
100
101
102
103
104


105
106
107
(* composableSignals *)

(* lift and map operations defined for signals in Ur/Web
   similar to liftA / liftA2 in Haskell, which are used to lift regular
   functions to be used as applicative functors *)

(* incidental helper functions *)
fun s [t] (a: source t) : signal t = signal a
fun pure [a] (s: a) : signal a = return s

(* typical lifting functions, modeled after the equivalent in Haskell *)
fun mapS [a] [b] (f: a -> b) (s: signal a) : signal b =
  s' <- s;
  pure (f s')

................................................................................
  srcX <- source (3: int);
  srcY <- source (2: int);

  return <xml><head>
    <title>Test of map and lifted signals</title>
    </head>
    <body>

      <h2>source values</h2>
      <dyn signal={ n <- s srcN; x <- s srcX; y <- s srcY;
                    return <xml>N = {[n]}, X = {[x]}, Y = {[y]}</xml>}/>
      <br/>
      <button value="Increment N" onclick={fn _ => n <- get srcN;
                                                   set srcN (n + 1)}/>
      <button value="Decrement N" onclick={fn _ => n <- get srcN;
................................................................................
                                                   set srcX (x - 1)}/>
      <button value="Increment Y" onclick={fn _ => y <- get srcY;
                                                   set srcY (y + 1)}/>
      <button value="Decrement Y" onclick={fn _ => y <- get srcY;
                                                   set srcY (y - 1)}/>

      <h2>signal values: part one</h2>
      <dyn signal={s' <- mapS' (fn x => y <- x; pure (y + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS' (fn x => y <- x; pure (y + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={x' <- liftS2' (fn sx sy => x <- sx; y <- sy; pure (x + y))
                                 (s srcX) (s srcY);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={x' <- liftS2' (fn sx sy => x <- sx; y <- sy;
                                   pure (x * y)) (s srcX) (s srcY);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={x' <- liftS3' (fn sx sy sz => x <- sx; y <- sy; z <- sz;
                                   pure (x * y * z)) (s srcX) (s srcY)
                                 (s srcN);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={s' <- liftS' (fn x => y <- x; pure (y + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>




      <h2>signal values: part two</h2>
      <dyn signal={w' <- liftS2 (fn x y => x * y) (s srcX) (s srcY);
                   x' <- liftS (fn x => x * 2) (pure w');
                   y' <- liftS3 (fn x y z => x * y * z)
                                (pure w') (s srcX) (pure x');
                   z' <- liftS (fn x => x + 11) (pure y');
                   return <xml>{[w']} {[x']} {[y']} {[z']}</xml>}/>
      <dyn signal={s' <- liftS (fn x => x + 1) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- mapS (fn x => x - 1) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS (fn x => x + 1) (s srcN);


                   return <xml>{[s']}</xml>}/>
    </body>
    </xml>







|







 







>







 







|

|

|









|

>
>
>
>




|







>
>



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
..
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(* composableSignals *)

(* lift and map operations defined for signals in Ur/Web
   similar to liftA / liftA2 in Haskell, which are used to lift regular
   functions to be used as applicative functors *)

(* incidental helper functions *)
fun s [a] (s': source a) : signal a = signal s'
fun pure [a] (s: a) : signal a = return s

(* typical lifting functions, modeled after the equivalent in Haskell *)
fun mapS [a] [b] (f: a -> b) (s: signal a) : signal b =
  s' <- s;
  pure (f s')

................................................................................
  srcX <- source (3: int);
  srcY <- source (2: int);

  return <xml><head>
    <title>Test of map and lifted signals</title>
    </head>
    <body>

      <h2>source values</h2>
      <dyn signal={ n <- s srcN; x <- s srcX; y <- s srcY;
                    return <xml>N = {[n]}, X = {[x]}, Y = {[y]}</xml>}/>
      <br/>
      <button value="Increment N" onclick={fn _ => n <- get srcN;
                                                   set srcN (n + 1)}/>
      <button value="Decrement N" onclick={fn _ => n <- get srcN;
................................................................................
                                                   set srcX (x - 1)}/>
      <button value="Increment Y" onclick={fn _ => y <- get srcY;
                                                   set srcY (y + 1)}/>
      <button value="Decrement Y" onclick={fn _ => y <- get srcY;
                                                   set srcY (y - 1)}/>

      <h2>signal values: part one</h2>
      <dyn signal={s' <- mapS' (fn sx => x <- sx; return (x + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS' (fn sx => x <- sx; return (x + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={x' <- liftS2' (fn sx sy => x <- sx; y <- sy; return (x + y))
                                 (s srcX) (s srcY);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={x' <- liftS2' (fn sx sy => x <- sx; y <- sy;
                                   pure (x * y)) (s srcX) (s srcY);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={x' <- liftS3' (fn sx sy sz => x <- sx; y <- sy; z <- sz;
                                   pure (x * y * z)) (s srcX) (s srcY)
                                 (s srcN);
                   return <xml>{[x']}</xml>}/>
      <dyn signal={s' <- liftS' (fn sx => x <- sx; return (x + 1)) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS2' (fn sx sy => x <- sx; y <- sy;
                                  return (x + y)) (pure 1) (s srcN);
                   return <xml>{[s']}</xml>}/>

      <h2>signal values: part two</h2>
      <dyn signal={w' <- liftS2 (fn x y => x * y) (s srcX) (s srcY);
                   x' <- liftS (fn x => x * 2) (pure w');
                   y' <- liftS3 (fn x y z => x * y * z)
                                (pure w') (s srcX) (pure 3);
                   z' <- liftS (fn x => x + 11) (pure y');
                   return <xml>{[w']} {[x']} {[y']} {[z']}</xml>}/>
      <dyn signal={s' <- liftS (fn x => x + 1) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- mapS (fn x => x - 1) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS (fn x => x + 1) (s srcN);
                   return <xml>{[s']}</xml>}/>
      <dyn signal={s' <- liftS2 (fn x y => x + y) (pure 1) (pure 1);
                   return <xml>{[s']}</xml>}/>
    </body>
    </xml>