Skip to content

Commit 6940894

Browse files
committed
Add foldp and a new example
1 parent d522a9f commit 6940894

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

docs/Flare.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Applicative (UI e)
5050
(BooleanAlgebra a) => BooleanAlgebra (UI e a)
5151
```
5252

53+
#### `wrap`
54+
55+
``` purescript
56+
wrap :: forall e a. Signal a -> UI e a
57+
```
58+
59+
Encapsulate a `Signal` within a `UI` component.
60+
5361
#### `lift`
5462

5563
``` purescript
@@ -58,13 +66,15 @@ lift :: forall e a. Eff (chan :: Chan, dom :: DOM | e) (Signal a) -> UI e a
5866

5967
Lift a `Signal` inside the `Eff` monad to a `UI` component.
6068

61-
#### `wrap`
69+
#### `foldp`
6270

6371
``` purescript
64-
wrap :: forall e a. Signal a -> UI e a
72+
foldp :: forall a b e. (a -> b -> b) -> b -> UI e a -> UI e b
6573
```
6674

67-
Encapsulte a `Signal` within a `UI` component.
75+
Create a past dependent component. The fold-function takes the current
76+
value of the component and the previous value of the output to produce
77+
the new value of the output.
6878

6979
#### `number`
7080

html/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,25 @@ <h3>Example 8: Lists and sliders</h3>
9494

9595
<h3>Example 9: BooleanAlgebra instance</h3>
9696
<pre>
97-
and (boolean_ <$> [true, false, true])
97+
boolean_ false && boolean_ true
9898
</pre>
9999
<div id="controls9"></div>
100100
<div id="output9"></div>
101101

102+
<h3>Example 10: Folding over the past</h3>
103+
<pre>
104+
graph xs width = outlined (outlineColor black <> lineWidth width)
105+
(path points)
106+
where points = zipWith point xs (1 .. length xs)
107+
point x y = { x, y: toNumber y }
108+
109+
ui = graph <$> foldp cons [] (numberRange "Position" 0.0 150.0 1.0 75.0)
110+
<*> numberRange "Width" 1.0 5.0 0.1 1.0
111+
</pre>
112+
<div id="controls10"></div>
113+
<canvas id="output10" width="150" height="100"
114+
style="border: 1px solid black; margin-left: 150px"></canvas>
115+
102116
<script type="text/javascript" src="main.js"></script>
103117
</body>
104118
</html>

html/main.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ h3 {
3030
width: 80px;
3131
}
3232

33+
.flare-input-number-range {
34+
width: 150px;
35+
padding: 0px;
36+
margin: 0px;
37+
}
38+
3339
label {
3440
width: 150px;
3541
display: inline-block;

src/Flare.purs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ module Flare
22
( Flare(..)
33
, UI(..)
44
, ElementId()
5-
, lift
65
, wrap
6+
, lift
7+
, foldp
78
, number
89
, number_
910
, numberRange
@@ -33,15 +34,15 @@ import Control.Monad.Eff
3334
import DOM
3435
import DOM.Node.Types (Element())
3536

36-
import Signal
37+
import qualified Signal as S
3738
import Signal.Channel
3839

3940
type ElementId = String
4041
type Label = String
4142

4243
-- | A `Flare` is a `Signal` with a corresponding list of HTML elements
4344
-- | for the user interface components.
44-
data Flare a = Flare (Array Element) (Signal a)
45+
data Flare a = Flare (Array Element) (S.Signal a)
4546

4647
instance functorFlare :: Functor Flare where
4748
map f (Flare cs sig) = Flare cs (map f sig)
@@ -98,15 +99,23 @@ instance booleanAlgebraUI :: (BooleanAlgebra a) => BooleanAlgebra (UI e a) where
9899
disj = lift2 disj
99100
not = map not
100101

102+
-- | Encapsulate a `Signal` within a `UI` component.
103+
wrap :: forall e a. (S.Signal a) -> UI e a
104+
wrap sig = UI $ return $ Flare [] sig
105+
101106
-- | Lift a `Signal` inside the `Eff` monad to a `UI` component.
102-
lift :: forall e a. Eff (chan :: Chan, dom :: DOM | e) (Signal a) -> UI e a
107+
lift :: forall e a. Eff (chan :: Chan, dom :: DOM | e) (S.Signal a) -> UI e a
103108
lift msig = UI $ do
104109
sig <- msig
105110
return $ Flare [] sig
106111

107-
-- | Encapsulte a `Signal` within a `UI` component.
108-
wrap :: forall e a. (Signal a) -> UI e a
109-
wrap sig = UI $ return $ Flare [] sig
112+
-- | Create a past dependent component. The fold-function takes the current
113+
-- | value of the component and the previous value of the output to produce
114+
-- | the new value of the output.
115+
foldp :: forall a b e. (a -> b -> b) -> b -> UI e a -> UI e b
116+
foldp f x0 (UI setup) = UI $ do
117+
(Flare comp sig) <- setup
118+
return $ Flare comp (S.foldp f x0 sig)
110119

111120
-- | Append a child element to the parent with the specified ID
112121
foreign import appendComponent :: forall e. ElementId
@@ -219,4 +228,4 @@ runFlare :: forall e a. (Show a)
219228
runFlare controls target (UI setupUI) = do
220229
(Flare components signal) <- setupUI
221230
appendComponents controls components
222-
runSignal (signal ~> show >>> renderString target)
231+
S.runSignal (map (show >>> renderString target) signal)

src/Flare/Drawing.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Graphics.Canvas (getCanvasElementById, getContext2D, Canvas(),
1515

1616
import DOM
1717

18-
import Signal
18+
import qualified Signal as S
1919
import Signal.Channel
2020

2121
import Flare
@@ -41,4 +41,4 @@ runFlareDrawing controls canvasID (UI setupUI) = do
4141
clearRect ctx { x: 0.0, y: 0.0, w, h }
4242
render ctx drawing
4343

44-
runSignal (signal ~> render')
44+
S.runSignal (map render' signal)

test/Main.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Prelude
44

55
import Data.Array
66
import Data.Foldable
7+
import Data.Int
78
import Data.Traversable
89
import Math (pow, cos)
910

@@ -63,4 +64,13 @@ main = do
6364
traverse (intRange_ 1 5) (1..5)
6465

6566
runFlare "controls9" "output9" $
66-
and (boolean_ <$> [true, false, true])
67+
boolean_ false && boolean_ true
68+
69+
let graph xs width = outlined (outlineColor black <> lineWidth width)
70+
(path points)
71+
where points = zipWith point xs (1 .. length xs)
72+
point x y = { x, y: toNumber y }
73+
74+
runFlareDrawing "controls10" "output10" $
75+
graph <$> foldp cons [] (numberRange "Position" 0.0 150.0 1.0 75.0)
76+
<*> numberRange "Width" 1.0 5.0 0.1 1.0

0 commit comments

Comments
 (0)