frp

27
Functional Reactive Programming 指指指指 指指 指指 指指指

Upload: phyrex-tsai

Post on 14-Apr-2017

125 views

Category:

Science


0 download

TRANSCRIPT

Page 1: Frp

Functional Reactive Programming

指導老師:陳恭學生:蔡詠捷

Page 2: Frp

Introduce FRP

• FRP is a subset of Functional Programming and Reactive Programming

Page 3: Frp

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.

Page 4: Frp

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

Page 5: Frp

Why FRP?

• Interactive applications without the bugs– Unpredictable order– Missed first event– Messy state– Threading issues– Leaking callbacks– Accidental recursion

Page 6: Frp

Life cycle of FRP

• Stage 1 : Initialization–Typically during program start-up, FRP code statements are converted into a directed graph in memory.

Page 7: Frp

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.

Page 8: Frp

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.

Page 9: Frp

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.

Page 10: Frp

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

Page 11: Frp

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.

Page 12: Frp

Fundamental data types

• Arrows represent streams.• Boxes represent transformations.• Conceptual modules shown as clouds.

Page 13: Frp

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

Page 14: Frp

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.

Page 15: Frp

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.

Page 16: Frp

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()

Page 17: Frp

FRP Primitives

• map– map transforms a Stream into a new Stream of

the same type or a different type.

Page 18: Frp

FRP Primitives

• merge– The merge primitives puts the events from two

streams together into a single stream.

Page 19: Frp

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.

Page 20: Frp

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.

Page 21: Frp

FRP Primitives

• filter– To filter a stream is to let values through only

sometimes.

Page 22: Frp

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.

Page 23: Frp

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.

Page 24: Frp

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.

Page 25: Frp

FRP Primitives

• sample– Return the value of the cell.

Page 26: Frp

Link

• Example of the book : – https://github.com/SodiumFRP

• Example of my own view : – https://github.com/PhyrexTsai/SodiumFRPExampl

e/

Page 27: Frp

Reference

• S. Blackheath, A. Jones. Functional Reactive Programming