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 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()
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/SodiumFRPExample/
Reference
• S. Blackheath, A. Jones. Functional Reactive
Programming

Frp

  • 1.
  • 2.
    Introduce FRP • FRPis 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? • Interactiveapplications without the bugs – Unpredictable order – Missed first event – Messy state – Threading issues – Leaking callbacks – Accidental recursion
  • 6.
    Life cycle ofFRP • Stage 1 : Initialization–Typically during program start-up, FRP code statements are converted into a directed graph in memory.
  • 7.
    Life cycle ofFRP • 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 - FRPLibrary • 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 • Thenew 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 Outputas 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.
  • 21.
    FRP Primitives • filter –To filter a stream is to let values through only sometimes.
  • 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.
  • 25.
    FRP Primitives • sample –Return the value of the cell.
  • 26.
    Link • Example ofthe book : – https://github.com/SodiumFRP • Example of my own view : – https://github.com/PhyrexTsai/SodiumFRPExample/
  • 27.
    Reference • S. Blackheath,A. Jones. Functional Reactive Programming

Editor's Notes

  • #4 使用觀察者模式 用模組化方式撰寫事件驅動程式 表示輸入的反應 表示資料的流程 程序狀態管理 取代傳統的 listeners 與 callbacks
  • #5 程式可組合性 聲明式的程式撰寫方式 程式結構類似圖形,並且FRP的引擎會自動執行 FRP 可以使用 functional programming 來寫 event-base 的邏輯
  • #6 順序不可預期性 Miss 第一個 event 麻煩的 state
  • #7 Initialize 的時候會將 FRP code 都轉成圖形化存在記憶體內
  • #8 在執行階段的時候會將輸入的值透過FRP引擎轉成輸出值
  • #9 聲明式的表達一件事情
  • #10 Sodium 使用 Push-based
  • #12 Discrete 分離