2. Introduce FRP
• FRP is a subset of Functional Programming
and Reactive Programming
3. What is FRP
• Use observer pattern.
• Modular way to code event-driven.
• Expressed as a reaction to its inputs.
• Expressed as a flow of data.
• Management of program state.
• Replaces the listeners and callbacks.
4. Why FRP?
• Compositionality.
• Declaratively programming style.
• Code structure like directed graph, and FRP
engine execution order automatically.
• FRP allows functional programming to be used
as a meta-language for writing event-based
logic
5. Why FRP?
• Interactive applications without the bugs
– Unpredictable order
– Missed first event
– Messy state
– Threading issues
– Leaking callbacks
– Accidental recursion
6. Life cycle of FRP
• Stage 1 : Initialization–Typically during
program start-up, FRP code statements are
converted into a directed graph in memory.
7. Life cycle of FRP
• Stage 2: Running–For the rest of the program
execution we feed values in, turn the crank
handle, and the FRP engine produces output.
8. Thinking in FRP
• Try to make things declaratively
• This style is referred to as declarative
programming
– We are telling the machine what the program is,
not what it does.
9. Sodium - FRP Library
• Sodium is a push-based system.
• Not all FRP systems use listeners internally.
• Push-based FRP systems typically do, but
there are also pull-based systems.
10. Fundamental data types
• Cell
– Represents a value that changes over time.
• Definition of Cell
– A container for a value that changes over time.
– Some FRP systems call it a behavior, property or a
signal
11. Fundamental data types
• Stream
– Represents a stream of events.
• Definition of Stream
– A stream of discrete events.
– It is also known in other FRP systems as event,
event stream, observable or signal.
– When an event propagates through a stream, we
sometimes say that the stream has fired.
12. Fundamental data types
• Arrows represent streams.
• Boxes represent transformations.
• Conceptual modules shown as clouds.
13. Lambda Syntax
• The new lambda syntax in Java 8 lets us write
this much shorter, with no fewer than six
variations of the syntax:
– (Integer k) -> { return k+1; }
– (k) -> { return k+1; }
– k -> { return k+1; }
– (Integer k) -> k+1
– (k) -> k+1
– k -> k+1
14. FRP Programming Tips
• Do not perform any I/O.
• Do not throw any exceptions unless they are
caught and handled within the function.
• Do not read the value of any external variable
if its value can change, but constants are
allowed and encourage.
• Do not modify any externally visible state.
15. FRP Programming Tips
• Do not keep any state between invocations of
the function.
• The function must have no external effects
other than through the returned value
• It must not be affected by any external state.
16. FRP primitives overview
Output as Stream Output as Cell Output as value
Stream map()
merge()/orElse()
snapshot()
filter()
never/new
Stream()
hold()
Cell switchS() map()
lift()
new Cell(constant)
switchC()
sample()
17. FRP Primitives
• map
– map transforms a Stream into a new Stream of
the same type or a different type.
18. FRP Primitives
• merge
– The merge primitives puts the events from two
streams together into a single stream.
19. FRP Primitives
• hold
– The hold primitive converts a stream into a cell in
such a way that the cell's value is that of the most
recent event received.
– Also named stepper or toProperty.
20. FRP Primitives
• snapshot
– The snapshot primitive captures the value of a cell
at the time when a stream event fires, and it can
then combine the stream and cell events together
with a supplied function.
– Also named withLatest, attach, or tag.
22. FRP Primitives
• lift
– Lift is a functional programming term meaning
(more generally) to make a function that operates
on values into a function that operates on some
type of container of those values.
23. FRP Primitives
• never
– In FRP a never stream is one that can't ever fire.
This name is universal in FRP systems.
– Only use in Stream.
24. FRP Primitives
• constant
– Cells usually change over time, but it's also
possible to construct them with a constant value.
– There is no way to modify a cell after it is created,
so its value is guaranteed to be constant forever.
– Only use in Cell.