on ef35672 help sign in
patch/Quickref.md
285 lines 13 kB history raw
1# Reflex Quick(ish) Reference
3## Typeclasses
5Many Reflex functions operate in monadic context `m a`, where the monad 'm' supports various additional typeclasses such as MonadWidget, MonadHold, or MonadSample in addition to Monad itself. The actual 'm' in use will be determined by the top-level entry point of the FRP host (such as Reflex-Dom -- see the bottom of the Reflex-Dom quick reference for details).
7The function signatures here have been simplified by removing many typeclass constraints and adding simple annotations to each function. Also the ubiquitous 't' type parameter has been removed.
9Some of these functions are *pure*: They operate on Events, Behaviors, or Dynamics uniformly without regard to the "current time". Other functions must operate in some monadic context, because they produce a result "as of now" (e.g., any function that takes an "initial" value, or returns a "current" value). Annotations are used to distinguish these cases:
11```haskell
12[ ] -- Pure function
13[S] -- Function runs in any monad supporting MonadSample
14[H] -- Function runs in any monad supporting MonadHold
15```
16Since MonadHold depends on MonadSample, any [S] function also runs in [H] context.
18## Functions producing Event
20```haskell
21-- Trivial Event
22[ ] never :: Event a
24-- Extract Event from Dynamic
25[ ] updated :: Dynamic a -> Event a
27-- Transform Event to Event using function
28[ ] fmap :: (a -> b) -> Event a -> Event b
29[ ] fmapMaybe :: (a -> Maybe b) -> Event a -> Event b
30[ ] ffilter :: (a -> Bool) -> Event a -> Event a
31[ ] ffor :: Event a -> (a -> b) -> Event b
32[ ] fforMaybe :: Event a -> (a -> Maybe b) -> Event b
34-- Event to identical Event with debug trace. (Only prints if Event is ultimately used.)
35[ ] traceEvent :: Show a => String -> Event a -> Event a
36[ ] traceEventWith :: (a -> String) -> Event a -> Event a
38-- Transform Event to Event by sampling Behavior or Dynamic
39[ ] gate :: Behavior Bool -> Event a -> Event a
40[ ] tag :: Behavior a -> Event b -> Event a
41[ ] tagPromptlyDyn :: Dynamic a -> Event b -> Event a
42[ ] attach :: Behavior a -> Event b -> Event (a, b)
43[ ] attachPromptlyDyn :: Dynamic a -> Event b -> Event (a, b)
44[ ] attachWith :: (a -> b -> c) -> Behavior a -> Event b -> Event c
45[ ] attachPromptlyDynWith :: (a -> b -> c) -> Dynamic a -> Event b -> Event c
46[ ] attachWithMaybe :: (a -> b -> Maybe c) -> Behavior a -> Event b -> Event c
47[ ] attachPromptlyDynWithMaybe :: (a -> b -> Maybe c) -> Dynamic a -> Event b -> Event c
48[ ] <@> :: Behavior (a -> b) -> Event a -> Event b
49[ ] <@ :: Behavior b -> Event a -> Event b
51-- Combine multiple Events
52[ ] <> :: Semigroup a => Event a -> Event a -> Event a
53[ ] difference :: Event a -> Event b -> Event a
54[ ] align :: Event a -> Event b -> Event (These a b)
55[ ] alignWith :: (These a b -> c) -> Event a -> Event b -> Event c
56[ ] mergeWith :: (a -> a -> a) -> [Event a] -> Event a
57[ ] leftmost :: [Event a] -> Event a
58[ ] mergeList :: [Event a] -> Event (NonEmpty a)
59[ ] merge :: GCompare k => DMap (WrapArg Event k) -> Event (DMap k)
60[ ] mergeMap :: Ord k => Map k (Event a) -> Event (Map k a)
62-- Efficient one-to-many fanout
63[ ] fanMap :: Ord k => Event (Map k a) -> EventSelector (Const2 k a)
64[ ] fan :: GCompare k => Event (DMap k) -> EventSelector k
65[ ] select :: EventSelector k -> k a -> Event a
66[ ] fanEither :: Event (Either a b) -> (Event a, Event b)
67[ ] fanThese :: Event (These a b) -> (Event a, Event b)
69-- Event to Event via function that can sample current values
70[ ] push :: (a -> m (Maybe b)) -> Event a -> Event b
71[ ] pushAlways :: (a -> m b ) -> Event a -> Event b
72 -- Note supplied function operates in [H] context
74-- Split Event into next occurrence and all other future occurrences, as of now.
75[H] headE :: Event a -> m (Event a)
76[H] tailE :: Event a -> m (Event a)
77[H] headTailE :: Event a -> m (Event a, Event a)
78```
80## Functions producing Behavior
82```haskell
83-- Trivial Behavior
84[ ] constant :: a -> Behavior a
86-- Extract Behavior from Dynamic
87[ ] current :: Dynamic a -> Behavior a
89-- Create Behavior with given initial value, updated when the Event fires.
90[H] hold :: a -> Event a -> m (Behavior a)
92-- Transform Behavior to Behavior using function
93[ ] fmap :: (a -> b) -> Behavior a -> Behavior b
94[ ] ffor :: Behavior a -> (a -> b) -> Behavior b
95[ ] <*> :: Behavior (a -> b) -> Behavior a -> Behavior b
96[ ] >>= :: Behavior a -> (a -> Behavior b) -> Behavior b
97[ ] <> :: Monoid a => Behavior a -> Behavior a -> Behavior a
98-- ... plus many more due to typeclass membership
100-- Behavior to Behavior by sampling current values
101[S] sample :: Behavior a -> m a
102[ ] pull :: m a -> Behavior a
103 -- Note supplied value is in [S] context
104```
106## Functions producing Dynamic
108```haskell
109-- Trivial Dynamic
110[ ] constDyn :: a -> Dynamic a
112-- Create Dynamic with given initial value, updated (various ways) when the Event fires.
113[H] holdDyn :: a -> Event a -> m (Dynamic a)
114[H] foldDyn :: (a -> b -> b ) -> b -> Event a -> m (Dynamic b)
115[H] foldDynMaybe :: (a -> b -> Maybe b ) -> b -> Event a -> m (Dynamic b)
116[H] foldDynM :: (a -> b -> m' b ) -> b -> Event a -> m (Dynamic b)
117[H] foldDynMaybeM :: (a -> b -> m' (Maybe b)) -> b -> Event a -> m (Dynamic b)
118 -- Note m' supplies [H] context
120-- Initial value is 0; counts Event firings from now.
121[H] count :: Num b => Event a -> m (Dynamic b)
122-- Initial Bool value is supplied; toggles at each Event firing.
123[H] toggle :: Bool -> Event a -> m (Dynamic Bool)
125-- Transform Dynamic to Dynamic using function
126[ ] ffor :: Dynamic a -> (a -> b) -> Dynamic b
127[ ] fmap :: (a -> b) -> Dynamic a -> Dynamic b
128[ ] splitDynPure :: Dynamic (a, b) -> (Dynamic a, Dynamic b)
130-- Combine multiple Dynamics
131[ ] mconcat :: Monoid a => [Dynamic a] -> Dynamic a
132[ ] distributeDMapOverDynPure :: GCompare k => DMap (WrapArg Dynamic k) -> Dynamic (DMap k)
133[ ] <*> :: Dynamic (a -> b) -> Dynamic a -> Dynamic b
134[ ] >>= :: Dynamic a -> (a -> Dynamic b) -> Dynamic b
135[ ] zipDynWith :: (a -> b -> c) -> Dynamic a -> Dynamic b -> Dynamic c
137-- Efficient one-to-many fanout
138[ ] demux :: Ord k => Dynamic k -> Demux k
139[ ] demuxed :: Eq k => Demux k -> k -> Dynamic Bool
141-- Dynamic to Dynamic, removing updates w/o value change
142[H] holdUniqDyn :: Eq a => Dynamic a -> m (Dynamic a)
143[H] holdUniqDynBy :: (a -> a -> Bool) -> Dynamic t a -> m (Dynamic t a)
145-- Dynamic to identical Dynamic with debug trace. (Only prints if Dynamic is ultimately used.)
146[ ] traceDyn :: Show a => String -> Dynamic a -> Dynamic a
147[ ] traceDynWith :: (a -> String) -> Dynamic a -> Dynamic a
148```
150## Flattening functions
152These functions flatten nested types such as Event-of-Event, Behavior-of-Event, Event-of-Behavior etc, removing the outer wrapper.
154For Events, the returned Event fires whenever the latest Event supplied by the wrapper fires. There are differences in how the functions handle *coincidences* -- situations where the old or new Event fires at the same instant that we switch from old to new. In some cases, the output Event tracks the old Event at the instant of switchover, while in other cases it tracks the new Event.
156```haskell
157-- Flatten Behavior-of-Event to Event. Old Event is used during switchover.
158[ ] switch :: Behavior (Event a) -> Event a
160-- Flatten Dyanmic-of-Event to Event. New Event is used immediately.
161[ ] switchPromptlyDyn :: Dynamic (Event a) -> Event a
163-- Flatten Event-of-Event to Event that fires when both wrapper AND new Event fire.
164[ ] coincidence :: Event (Event a) -> Event a
166-- Flatten Behavior-of-Behavior to Behavior. Behavior is an instance of Monad,
167-- so the 'join' function from 'Control.Monad' suffices. The output of 'join' is
168-- a Behavior whose value is equal to the current value of the current value of
169-- the input.
170[ ] join :: Behavior (Behavior a) -> Behavior a
173-- Flatten Dynamic-of-Dynamic to Dynamic. New Dynamic is used immediately.
174-- Output updated whenever inner OR outer Dynamic updates.
175[ ] join :: Dynamic (Dynamic a) -> Dynamic a
176[ ] joinDynThroughMap :: Ord k => Dynamic (Map k (Dynamic a)) -> Dynamic (Map k a)
178-- Analogous to 'hold': Create a Behavior that is initially identical to the
179-- supplied Behavior. Updated to track a new Behavior whenever the Event fires.
180[H] switcher :: Behavior a -> Event (Behavior a) -> m (Behavior a)
182-- Similar to above, for Events. Created Event initially tracks the first argument.
183-- At switchover, the output Event immediately tracks the new Event.
184[H] switchPromptly :: Event a -> Event (Event a) -> m (Event a)
185```
187## Typeclasses to introspect and modify an FRP network.
189The functions mentioned above are used to create a static FRP network.
190There are additional typeclasses that can be used to modify the FRP network, to have it interact with IO action, or to introspect the building of the network.
192Th typeclasses and their associated annotations include:
194- `PostBuild`
195 Fire an Event when an FRP network has been set up.
196 ```haskell
197 [B] -- Function runs in any monad supporting PostBuild
198 ```
200- `Adjustable`
201 Use Events to add or remove pieces of an FRP network.
202 ```haskell
203 [A] -- Function runs in any monad supporting Adjustable
204 ```
206- `TriggerEvent`
207 Create new externally-fired Events from within an FRP network.
208 ```haskell
209 [T] -- Function runs in any monad supporting TriggerEvent
210 ```
212- `PerformEvent`
213 Use Events to trigger IO actions and to collect their results.
214 ```haskell
215 [P] -- Function runs in any monad supporting PerformEvent
216 ```
218## Startup
220```haskell
221-- One-shot Event that is triggered once the FRP network has been built
222[B] getPostBuild :: m (Event ())
223```
225## Collection management functions
227```haskell
228-- Turn a Dynamic key/value map into a set of dynamically-changing widgets.
229[H,A,B] listWithKey :: Ord k =>
230 Dynamic (Map k v) -> (k -> Dynamic v -> m a ) -> m (Dynamic (Map k a))
232-- Same as above where the widget constructor doesn't care about the key.
233[H,A,B] list :: Ord k =>
234 Dynamic (Map k v) -> ( Dynamic v -> m a ) -> m (Dynamic (Map k a))
236-- Even simpler version where there are no keys and we just use a list.
237[H,A,B] simpleList ::
238 Dynamic [v] -> ( Dynamic v -> m a ) -> m (Dynamic [a])
240-- Like listWithKey specialized for widgets returning (Event a).
241[H,A,B] listViewWithKey :: Ord k =>
242 Dynamic (Map k v) -> (k -> Dynamic v -> m (Event a)) -> m (Event (Map k a))
244-- Create a dynamically-changing set of widgets, one of which is selected at any time.
245[H,A,B] selectViewListWithKey_ :: Ord k => Dynamic k ->
246 Dynamic (Map k v) -> (k -> Dynamic v -> Dynamic Bool -> m (Event a)) -> m (Event k)
248-- Same as listWithKey, but takes initial values and an updates Event instead of a Dynamic.
249[H,A,B] listWithKey' :: Ord k =>
250 Map k v -> Event (Map k (Maybe v)) -> (k -> v -> Event v -> m a) -> m (Dynamic (Map k a))
251```
253## Creating new events from within an FRP network.
255```haskell
256-- Create a new Event and a function that will cause the Event to fire, from within an FRP network
257[T] newTriggerEvent :: m (Event a, a -> IO ())
258```
260## Connecting to the real world (I/O)
262```haskell
263-- Run side-effecting actions in Event when it occurs; returned Event contains
264-- results. Side effects run in the (Performable m) monad which is associated with
265-- the (PerformEvent t m) typeclass constraint.
266-- This allows for working with IO when a ((MonadIO (Performable m)) constraint is available.
267[P] performEvent :: Event (Performable m a ) -> m (Event a)
269-- Just run side-effects; no return Event
270[P] performEvent_ :: Event (Performable m ()) -> m ()
272-- Actions run asynchronously; actions are given a callback to send return values
273[P,T] performEventAsync :: Event ((a -> IO ()) -> Performable m ()) -> m (Event a)
274```
276### Time
278```haskell
279-- Create Event at given interval with given basis time.
280[P,T] tickLossy :: NominalDiffTime -> UTCTime -> m (Event t TickInfo)
282-- Delay an Event's occurrences by a given amount in seconds.
283[P,T] delay :: NominalDiffTime -> Event t a -> m (Event t a)
284```