functional reactive programming
TRANSCRIPT
// Imperative programming
var x = 1, y = 2, z = y + x;console.log(z); //3x = 2;console.log(z); //3
// Reactive programming
var x = 1, y = 2, z = y + x;console.log(z); //3x = 2;console.log(z); //4
FUNCTIONAL PROGRAMMING
DECLARATIVE EVALUATION OF PURE FUNCTIONS TO CREATE IMMUTABLE
PROGRAMS AND SO AVOID SIDE EFFECTS THAT ARE EXTERNALLY
OBSERVED.
PURE FUNCTIONS
DECLARATIVE PROGRAMMING
REFERENTIAL TRANSPARENCY
INMUTABILITY
// Imperative programming
var arrayFoo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];for (var i = 0; i < arrayFoo.length; i++) { arrayFoo[i] = Math.pow(arrayFoo[i], 2);}array; // [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
// Declarative programming
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(num => Math.pow(num, 2));// [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
PURE FUNCTIONS
DECLARATIVE PROGRAMMING
REFERENTIAL TRANSPARENCY
INMUTABILITY
// Impure function
var likes = 0;function addLike() { return ++likes;}
// Pure function
function addLike(likes) { return ++likes;}addLike(addLike(0)); // 2
DECLARATIVE PROGRAMMING
REFERENTIAL TRANSPARENCY
INMUTABILITY
PURE FUNCTIONS// Referential transparent function
int plusOne(int x){ return x+1;}
// Referential opaqueness function
int G = 10;
int plusG(int x){//G can be modified externally returning different values. return x + G;}
DECLARATIVE PROGRAMMINGREFERENTIAL TRANSPARENCY
INMUTABILITY
PURE FUNCTIONS
// Referential transparent function
int plusOne(int x){ return x+1;}
// Referential opaqueness function
int G = 10;
int plusG(int x){//G can be modified externally returning different values. return x + G;}
: https://goo.gl/NaMmJ
: https://goo.gl/sGSwMS
Reactive programming is programming with asynchronous data streams.
// RxSwift LoginViewModel
struct LoginViewModel { let usernameBGColor: Driver<UIColor> let passwordBGColor: Driver<UIColor> let credentialsValid: Driver<Bool> init(usernameText: Driver<String>, passwordText: Driver<String>) { let usernameValid = usernameText .distinctUntilChanged() .throttle(0.3) .map { $0.utf8.count > 3 } let passwordValid = passwordText .distinctUntilChanged() .throttle(0.3) .map { $0.utf8.count > 3 } usernameBGColor = usernameValid .map { $0 ? BG_COLOR : UIColor.white } passwordBGColor = passwordValid .map { $0 ? BG_COLOR : UIColor.white } credentialsValid = Driver.combineLatest(usernameValid, passwordValid) { $0 && $1 } } func login(_ username: String, password: String) -> Observable<AutenticationStatus> { return AuthManager.sharedManager.login(username, password: password) }