Tech Talk #4 : Functional Reactive Programming - Đặng Thái Sơn

Post on 16-Apr-2017

282 views 2 download

Transcript of Tech Talk #4 : Functional Reactive Programming - Đặng Thái Sơn

Reactive Thinking

fb.me/dangthaison.91 | @dangthaison91

FunctionalReactive

Programming

1. What is FRP?

Functional+

Reactive

Microsoft Excel

a = b + c

FRP: Streams of values over time

Core of FRP is Signal

Signal emits value over the

time

FRPis

Declarative

Imperative

Write correct sequence of codes in the correct order. Step by step. (How)

Declarative

Write code to describes what you want to do.

2. Why is FRP?

Example 1

Search API as you type but:

- Don't spam API server every seconds- Only make request if keyword changed- Cancel previous requests

Imperative

Declarative

Example 2

When the user starts simultaneously panning and rotating an object, start a countdown from 3.

Stop the timer either when the countdown ends or when the user stops the gestures.

Imperative

Imperative

Imperative

- When the user touches the screen - Check if a user is panning an object, store that information - Check if both gestures are running simultaneously - start a timer, from 3, counting down. - Check if the user is rotating an object, store that information - Check if both gestures are running simultaneously - start a timer, from 3, counting down. - Check if the user stopped panning - Stop the timer if needed - Check if the user stopped rotating - Stop the timer if needed

- When the timer ticks, decrease the number of seconds left - If the number of seconds left is zero, stop the timer*

There is a Better Way!

Declarative

Declarative

Declarative

Declarative

Declarative

Declarative

Define condition “simultaneously `panning` and `rotating`”Define condition “start a countdown from `3`”Define condition “when the user stops the gestures”Define what a timer is

When the user starts simultaneously `panning` and `rotating` an object, start a countdown from `3`.Stop the timer either when the countdown ends or when the user stops the gestures.”

Advantages• Code more concise & clear, easy to

understand without context

• Readability, highly express

• Make Asynchronous easier

• UI Binding

• Maintainability, Extensibility...

Imperative

Declarative

What we can do• Observables

• Transformations

• Composition

• Combination

• Filter

• Buffer...

Denial• FRP is hard, imperative is more easier

• FRP just is trend!

• Just another Framework

• Unnecessarily complex

3. Thinking in Reactive

FRP is hard, but it is SIMPLE

Imperative is EASY

Simplevs

Easy

Complexity

Simple made Easy— Rick Hickey

FRP is good Trend

Functional Reactive will be

RADIOACTIVE

More: https://goo.gl/lfNWlJBlog: https://dangthaison91.wordpress.com

Thank you!