frp
TRANSCRIPT
Functional Reactive Programming
指導老師:陳恭學生:蔡詠捷
Introduce FRP
• FRP is a subset of Functional Programming and Reactive Programming
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.
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
Why FRP?
• Interactive applications without the bugs– Unpredictable order– Missed first event– Messy state– Threading issues– Leaking callbacks– Accidental recursion
Life cycle of FRP
• Stage 1 : Initialization–Typically during program start-up, FRP code statements are converted into a directed graph in memory.
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.
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.
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.
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
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.
Fundamental data types
• Arrows represent streams.• Boxes represent transformations.• Conceptual modules shown as clouds.
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
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.
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.
FRP primitives overviewOutput 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()
FRP Primitives
• map– map transforms a Stream into a new Stream of
the same type or a different type.
FRP Primitives
• merge– The merge primitives puts the events from two
streams together into a single stream.
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.
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.
FRP Primitives
• filter– To filter a stream is to let values through only
sometimes.
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.
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.
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.
FRP Primitives
• sample– Return the value of the cell.
Link
• Example of the book : – https://github.com/SodiumFRP
• Example of my own view : – https://github.com/PhyrexTsai/SodiumFRPExampl
e/
Reference
• S. Blackheath, A. Jones. Functional Reactive Programming