on workflow-monad help sign in
reflex/Quickref.md
286 lines 14 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
33[ ] <$ :: a -> Event b -> Event a
35-- Event to identical Event with debug trace. (Only prints if Event is ultimately used.)
36[ ] traceEvent :: Show a => String -> Event a -> Event a
37[ ] traceEventWith :: (a -> String) -> Event a -> Event a
39-- Transform Event to Event by sampling Behavior or Dynamic
40[ ] gate :: Behavior Bool -> Event a -> Event a
41[ ] tag :: Behavior a -> Event b -> Event a
42[ ] tagPromptlyDyn :: Dynamic a -> Event b -> Event a
43[ ] attach :: Behavior a -> Event b -> Event (a, b)
44[ ] attachPromptlyDyn :: Dynamic a -> Event b -> Event (a, b)
45[ ] attachWith :: (a -> b -> c) -> Behavior a -> Event b -> Event c
46[ ] attachPromptlyDynWith :: (a -> b -> c) -> Dynamic a -> Event b -> Event c
47[ ] attachWithMaybe :: (a -> b -> Maybe c) -> Behavior a -> Event b -> Event c
48[ ] attachPromptlyDynWithMaybe :: (a -> b -> Maybe c) -> Dynamic a -> Event b -> Event c
49[ ] <@> :: Behavior (a -> b) -> Event a -> Event b
50[ ] <@ :: Behavior a -> Event b -> Event a
52-- Combine multiple Events
53[ ] <> :: Semigroup a => Event a -> Event a -> Event a
54[ ] difference :: Event a -> Event b -> Event a
55[ ] align :: Event a -> Event b -> Event (These a b)
56[ ] alignWith :: (These a b -> c) -> Event a -> Event b -> Event c
57[ ] mergeWith :: (a -> a -> a) -> [Event a] -> Event a
58[ ] leftmost :: [Event a] -> Event a
59[ ] mergeList :: [Event a] -> Event (NonEmpty a)
60[ ] merge :: GCompare k => DMap (WrapArg Event k) -> Event (DMap k)
61[ ] mergeMap :: Ord k => Map k (Event a) -> Event (Map k a)
63-- Efficient one-to-many fanout
64[ ] fanMap :: Ord k => Event (Map k a) -> EventSelector (Const2 k a)
65[ ] fan :: GCompare k => Event (DMap k) -> EventSelector k
66[ ] select :: EventSelector k -> k a -> Event a
67[ ] fanEither :: Event (Either a b) -> (Event a, Event b)
68[ ] fanThese :: Event (These a b) -> (Event a, Event b)
70-- Event to Event via function that can sample current values
71[ ] push :: (a -> m (Maybe b)) -> Event a -> Event b
72[ ] pushAlways :: (a -> m b ) -> Event a -> Event b
73 -- Note supplied function operates in [H] context
75-- Split Event into next occurrence and all other future occurrences, as of now.
76[H] headE :: Event a -> m (Event a)
77[H] tailE :: Event a -> m (Event a)
78[H] headTailE :: Event a -> m (Event a, Event a)
79```
81## Functions producing Behavior
83```haskell
84-- Trivial Behavior
85[ ] constant :: a -> Behavior a
87-- Extract Behavior from Dynamic
88[ ] current :: Dynamic a -> Behavior a
90-- Create Behavior with given initial value, updated when the Event fires.
91[H] hold :: a -> Event a -> m (Behavior a)
93-- Transform Behavior to Behavior using function
94[ ] fmap :: (a -> b) -> Behavior a -> Behavior b
95[ ] ffor :: Behavior a -> (a -> b) -> Behavior b
96[ ] <*> :: Behavior (a -> b) -> Behavior a -> Behavior b
97[ ] >>= :: Behavior a -> (a -> Behavior b) -> Behavior b
98[ ] <> :: Monoid a => Behavior a -> Behavior a -> Behavior a
99-- ... plus many more due to typeclass membership
101-- Behavior to Behavior by sampling current values
102[S] sample :: Behavior a -> m a
103[ ] pull :: m a -> Behavior a
104 -- Note supplied value is in [S] context
105```
107## Functions producing Dynamic
109```haskell
110-- Trivial Dynamic
111[ ] constDyn :: a -> Dynamic a
113-- Create Dynamic with given initial value, updated (various ways) when the Event fires.
114[H] holdDyn :: a -> Event a -> m (Dynamic a)
115[H] foldDyn :: (a -> b -> b ) -> b -> Event a -> m (Dynamic b)
116[H] foldDynMaybe :: (a -> b -> Maybe b ) -> b -> Event a -> m (Dynamic b)
117[H] foldDynM :: (a -> b -> m' b ) -> b -> Event a -> m (Dynamic b)
118[H] foldDynMaybeM :: (a -> b -> m' (Maybe b)) -> b -> Event a -> m (Dynamic b)
119 -- Note m' supplies [H] context
121-- Initial value is 0; counts Event firings from now.
122[H] count :: Num b => Event a -> m (Dynamic b)
123-- Initial Bool value is supplied; toggles at each Event firing.
124[H] toggle :: Bool -> Event a -> m (Dynamic Bool)
126-- Transform Dynamic to Dynamic using function
127[ ] ffor :: Dynamic a -> (a -> b) -> Dynamic b
128[ ] fmap :: (a -> b) -> Dynamic a -> Dynamic b
129[ ] splitDynPure :: Dynamic (a, b) -> (Dynamic a, Dynamic b)
131-- Combine multiple Dynamics
132[ ] mconcat :: Monoid a => [Dynamic a] -> Dynamic a
133[ ] distributeDMapOverDynPure :: GCompare k => DMap (WrapArg Dynamic k) -> Dynamic (DMap k)
134[ ] <*> :: Dynamic (a -> b) -> Dynamic a -> Dynamic b
135[ ] >>= :: Dynamic a -> (a -> Dynamic b) -> Dynamic b
136[ ] zipDynWith :: (a -> b -> c) -> Dynamic a -> Dynamic b -> Dynamic c
138-- Efficient one-to-many fanout
139[ ] demux :: Ord k => Dynamic k -> Demux k
140[ ] demuxed :: Eq k => Demux k -> k -> Dynamic Bool
142-- Dynamic to Dynamic, removing updates w/o value change
143[H] holdUniqDyn :: Eq a => Dynamic a -> m (Dynamic a)
144[H] holdUniqDynBy :: (a -> a -> Bool) -> Dynamic t a -> m (Dynamic t a)
146-- Dynamic to identical Dynamic with debug trace. (Only prints if Dynamic is ultimately used.)
147[ ] traceDyn :: Show a => String -> Dynamic a -> Dynamic a
148[ ] traceDynWith :: (a -> String) -> Dynamic a -> Dynamic a
149```
151## Flattening functions
153These functions flatten nested types such as Event-of-Event, Behavior-of-Event, Event-of-Behavior etc, removing the outer wrapper.
155For 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.
157```haskell
158-- Flatten Behavior-of-Event to Event. Old Event is used during switchover.
159[ ] switch :: Behavior (Event a) -> Event a
161-- Flatten Dyanmic-of-Event to Event. New Event is used immediately.
162[ ] switchDyn :: Dynamic (Event a) -> Event a
164-- Flatten Event-of-Event to Event that fires when both wrapper AND new Event fire.
165[ ] coincidence :: Event (Event a) -> Event a
167-- Flatten Behavior-of-Behavior to Behavior. Behavior is an instance of Monad,
168-- so the 'join' function from 'Control.Monad' suffices. The output of 'join' is
169-- a Behavior whose value is equal to the current value of the current value of
170-- the input.
171[ ] join :: Behavior (Behavior a) -> Behavior a
174-- Flatten Dynamic-of-Dynamic to Dynamic. New Dynamic is used immediately.
175-- Output updated whenever inner OR outer Dynamic updates.
176[ ] join :: Dynamic (Dynamic a) -> Dynamic a
177[ ] joinDynThroughMap :: Ord k => Dynamic (Map k (Dynamic a)) -> Dynamic (Map k a)
179-- Analogous to 'hold': Create a Behavior that is initially identical to the
180-- supplied Behavior. Updated to track a new Behavior whenever the Event fires.
181[H] switcher :: Behavior a -> Event (Behavior a) -> m (Behavior a)
183-- Similar to above, for Events. Created Event initially tracks the first argument.
184-- At switchover, the output Event immediately tracks the new Event.
185[H] switchHold :: Event a -> Event (Event a) -> m (Event a)
186```
188## Typeclasses to introspect and modify an FRP network.
190The functions mentioned above are used to create a static FRP network.
191There 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.
193Th typeclasses and their associated annotations include:
195- `PostBuild`
196 Fire an Event when an FRP network has been set up.
197 ```haskell
198 [B] -- Function runs in any monad supporting PostBuild
199 ```
201- `Adjustable`
202 Use Events to add or remove pieces of an FRP network.
203 ```haskell
204 [A] -- Function runs in any monad supporting Adjustable
205 ```
207- `TriggerEvent`
208 Create new externally-fired Events from within an FRP network.
209 ```haskell
210 [T] -- Function runs in any monad supporting TriggerEvent
211 ```
213- `PerformEvent`
214 Use Events to trigger IO actions and to collect their results.
215 ```haskell
216 [P] -- Function runs in any monad supporting PerformEvent
217 ```
219## Startup
221```haskell
222-- One-shot Event that is triggered once the FRP network has been built
223[B] getPostBuild :: m (Event ())
224```
226## Collection management functions
228```haskell
229-- Turn a Dynamic key/value map into a set of dynamically-changing widgets.
230[H,A,B] listWithKey :: Ord k =>
231 Dynamic (Map k v) -> (k -> Dynamic v -> m a ) -> m (Dynamic (Map k a))
233-- Same as above where the widget constructor doesn't care about the key.
234[H,A,B] list :: Ord k =>
235 Dynamic (Map k v) -> ( Dynamic v -> m a ) -> m (Dynamic (Map k a))
237-- Even simpler version where there are no keys and we just use a list.
238[H,A,B] simpleList ::
239 Dynamic [v] -> ( Dynamic v -> m a ) -> m (Dynamic [a])
241-- Like listWithKey specialized for widgets returning (Event a).
242[H,A,B] listViewWithKey :: Ord k =>
243 Dynamic (Map k v) -> (k -> Dynamic v -> m (Event a)) -> m (Event (Map k a))
245-- Create a dynamically-changing set of widgets, one of which is selected at any time.
246[H,A,B] selectViewListWithKey_ :: Ord k => Dynamic k ->
247 Dynamic (Map k v) -> (k -> Dynamic v -> Dynamic Bool -> m (Event a)) -> m (Event k)
249-- Same as listWithKey, but takes initial values and an updates Event instead of a Dynamic.
250[H,A] listWithKeyShallowDiff :: Ord k =>
251 Map k v -> Event (Map k (Maybe v)) -> (k -> v -> Event v -> m a) -> m (Dynamic (Map k a))
252```
254## Creating new events from within an FRP network.
256```haskell
257-- Create a new Event and a function that will cause the Event to fire, from within an FRP network
258[T] newTriggerEvent :: m (Event a, a -> IO ())
259```
261## Connecting to the real world (I/O)
263```haskell
264-- Run side-effecting actions in Event when it occurs; returned Event contains
265-- results. Side effects run in the (Performable m) monad which is associated with
266-- the (PerformEvent t m) typeclass constraint.
267-- This allows for working with IO when a ((MonadIO (Performable m)) constraint is available.
268[P] performEvent :: Event (Performable m a ) -> m (Event a)
270-- Just run side-effects; no return Event
271[P] performEvent_ :: Event (Performable m ()) -> m ()
273-- Actions run asynchronously; actions are given a callback to send return values
274[P,T] performEventAsync :: Event ((a -> IO ()) -> Performable m ()) -> m (Event a)
275```
277### Time
279```haskell
280-- Create Event at given interval with given basis time.
281[P,T] tickLossy :: NominalDiffTime -> UTCTime -> m (Event t TickInfo)
283-- Delay an Event's occurrences by a given amount in seconds.
284[P,T] delay :: NominalDiffTime -> Event t a -> m (Event t a)
285```