Ur/Web Examples
Check-in [224be5ae9a]
Not logged in

Many hyperlinks are disabled.
 ```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 57 58 59 60 61 62 63 64 65 66 67 68 ``` ``` (* 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 fun mapS' [a] [b] (f: a -> b) (s: signal a) : signal b = s' <- s; pure (f s') fun mapS [a] [b] (f: signal a -> signal b) (s: signal a) : signal b = f s fun liftS [a] [b] (f: signal a -> signal b) (s: signal a) : signal b = f s fun liftS2 [a] [b] [c] (f: signal a -> signal b -> signal c) (s1: signal a) (s2: signal b) : signal c = f s1 s2 fun liftS' [a] [b] (f: a -> b) (s: signal a) : signal b = s' <- s; pure (f s') fun liftS2' [a] [b] [c] (f: a -> b -> c) (s1: signal a) (s2: signal b) : signal c = s1' <- s1; s2' <- s2; pure (f s1' s2') fun main () : transaction page = srcN <- source (0: int); srcX <- source (3: int); srcY <- source (2: int); return Test of map and lifted signals y <- x; pure (y + 1)) (s srcN); return {[s']}}/> y <- x; pure (y + 1)) (s srcN); return {[s']}}/> x <- sx; y <- sy; return (x + y)) (s srcX) (s srcY); return {[x']}}/> x <- sx; y <- sy; pure (x * y)) (s srcX) (s srcY); return {[x']}}/> y <- x; pure (y + 1)) (s srcN); return {[s']}}/> x * y) (s srcX) (s srcY); y' <- liftS' (fn x => x + 11) (pure x'); return {[x']} {[y']}}/> x + 1) (s srcN); return {[s']}}/> x - 1) (s srcN); return {[s']}}/> x + 1) (s srcN); return {[s']}}/> ``` ``` > < | > > > | < < < < < < < < < < < < < < | | > > > > > > > > > > > > | | | | | | | | | | ``` ```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 57 58 59 60 61 62 63 64 65 66 67 68 69 ``` ``` (* 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') fun liftS [a] [b] (f: a -> b) (s: signal a) : signal b = s' <- s; pure (f s') fun liftS2 [a] [b] [c] (f: a -> b -> c) (s1: signal a) (s2: signal b) : signal c = s1' <- s1; s2' <- s2; pure (f s1' s2') (* variations on the above functions with raw signals passed directly to (and returned by) the function argument *) fun mapS' [a] [b] (f: signal a -> signal b) (s: signal a) : signal b = f s fun liftS' [a] [b] (f: signal a -> signal b) (s: signal a) : signal b = f s fun liftS2' [a] [b] [c] (f: signal a -> signal b -> signal c) (s1: signal a) (s2: signal b) : signal c = f s1 s2 fun main () : transaction page = srcN <- source (0: int); srcX <- source (3: int); srcY <- source (2: int); return Test of map and lifted signals y <- x; pure (y + 1)) (s srcN); return {[s']}}/> y <- x; pure (y + 1)) (s srcN); return {[s']}}/> x <- sx; y <- sy; return (x + y)) (s srcX) (s srcY); return {[x']}}/> x <- sx; y <- sy; pure (x * y)) (s srcX) (s srcY); return {[x']}}/> y <- x; pure (y + 1)) (s srcN); return {[s']}}/> x * y) (s srcX) (s srcY); y' <- liftS (fn x => x + 11) (pure x'); return {[x']} {[y']}}/> x + 1) (s srcN); return {[s']}}/> x - 1) (s srcN); return {[s']}}/> x + 1) (s srcN); return {[s']}}/> ```