SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
FUNCTIONAL PROGRAMMING
for OO programmers (Part 1)
HELLO
I am Calvin (Cheng)
@calvinchengx
calvin.cheng.lc
calvinchengx
calvinx.com
WHY
functional programming?
WHY
• Different implementation paradigms for different
parts of your program (or system) to produce
high quality software with minimal bugs
• Leverage on immutability - shared mutable state is
the root of all evil (data race conditions, program
complexity etc)
functional programming?
WHY
functional programming?
• Function purity gives you these benefits
1. testable
2. portable
3. memoizable
4. parallelizable
WHY
functional programming?
• Real world software
1. Clojure in Akamai
2. Scala inTwitter
3. Haskell in AT&T and in Standard Chartered
4. Reactjs' immutable state and virtual dom in FB
A COMPARISON
procedural vs OO vs functional
• Programming paradigms are world views
1. most languages are on a sliding scale
2. we can apply the different paradigms on any
given turing-complete language
A COMPARISON
procedural (“imperative”)
• Code is organised into “small procedures” that use
and change our data
A COMPARISON
object-oriented
• Code is organised as
‘templates’ and instantiated
with objects with data, that
changes
A COMPARISON
functional
• Code is organised as composable functions and
given a particular input data, will always return the
same output data
A COMPARISON
procedural vs OO vs functional
• Testable
procedural
globally shared data makes it hard to
test.
OO
requires mock libraries which adds
complexity to testing.
functional
pure functions always return an
expected value given a particular
input. So testing becomes trivial.
A COMPARISON
procedural vs OO vs functional
• Portable
procedural procedures are often one-off.
OO
portability is good but complex
inheritance chains result in
unexpected behaviors.
functional
pure functions are completely
portable.
A COMPARISON
procedural vs OO vs functional
• Memoizable
procedural Not memoizable.
OO Not memoizable.
functional
pure functions are completely
memoizable.
A COMPARISON
procedural vs OO vs functional
• Parallelizable
procedural Rarely parallelizable.
OO
Rarely parallelizable due to shared
mutable state.
functional
pure functions are always
parallelizable.
WHAT
• First-class functions
• Functions can return functions
• Lexical closures
• Pure functions
• Safe recursion
• No mutating state
makes a language functional?
FIRST-CLASS
functions
FIRST-CLASS
var	
  f	
  =	
  function(a,	
  b)	
  {	
  
	
  	
  return	
  a	
  +	
  b;	
  
};	
  
var	
  callFunctionAndArgs	
  =	
  function(f,	
  a,	
  b)	
  {	
  
	
  	
  return	
  f(a,	
  b);	
  
};	
  
callFunctionAndArgs(f,	
  1,	
  2);	
  
functions
FUNCTIONS
can return functions
FUNCTIONS
var	
  f	
  =	
  function(a,	
  b)	
  {	
  
	
  	
  return	
  a	
  +	
  b;	
  
};	
  
var	
  applyFirst	
  =	
  function	
  (f,	
  a)	
  {	
  
	
  	
  return	
  function(b)	
  {	
  	
  
	
  	
  	
  	
  return	
  f(a,	
  b);	
  
	
  	
  };	
  
};	
  
//	
  applyFirst	
  returns	
  a	
  function	
  that	
  has	
  not	
  been	
  evaluated	
  
//	
  this	
  is	
  applicable	
  for	
  `currying`	
  in	
  functional	
  paradigm
can return functions
LEXICAL SCOPE
free variables belong to parents cope
LEXICAL SCOPE
function	
  add(x)	
  {	
  
	
  	
  console.log('x	
  is	
  '	
  +	
  x);	
  
	
  	
  return	
  function(y)	
  {	
  
	
  	
  	
  	
  return	
  x	
  +	
  y;	
  	
  
	
  	
  };	
  
}	
  
var	
  add10	
  =	
  add(10);	
  
console.log(add10);	
  
//	
  console.log('x	
  is	
  '	
  +	
  x);	
  x	
  is	
  not	
  defined	
  in	
  outside	
  of	
  the	
  `add`	
  function	
  
console.log(add10(20));	
  
free variables belong to parent scope
belongs to parent scope
returns [Function]
returns 30
PURE FUNCTIONS
with no side effects
PURE FUNCTIONS
• Functions that don’t change anything out-of-scope
and don’t depend on anything out-of-scope are
called “pure”
• A pure function always gives the same result given
the same parameters; independent of program/
system state
with no side effects
SAFE RECURSION
with proper tail call
SAFE RECURSION
function	
  factorial(n,	
  accumulator)	
  {	
  
	
  	
  if	
  (n	
  ===	
  0)	
  {	
  
	
  	
  	
  	
  return	
  accumulator;	
  
	
  	
  }	
  
	
  	
  return	
  factorial(n	
  -­‐	
  1,	
  n	
  *	
  accumulator);	
  
}	
  
//	
  recursively	
  call	
  itself	
  from	
  5	
  to	
  0,	
  breaking	
  at	
  n	
  ===	
  0	
  
factorial(5,	
  1)	
  	
  
tail call
with proper tail call
SAFE RECURSION
• Proper tail call is mechanism by which language
compiler/interpreter recognises tail calls and reuses
call frames so call stack does not grow
• JavaScript does not use proper tail call (so use with
care for recurse depth of no more than 10,000)
• ES6 JavaScript does
with proper tail call
NO MUTATING STATE
so everything is predictable
NO MUTATING STATE
• Using pure functions to compose your program
result in a program that does not have shared
mutable state.
• JavaScript - writing pure functions or programs
with no shared mutation state is possible; but not
guaranteed.
so everything is predictable
WHAT
makes a language functional?
First-class
functions
Higher-order
functions
Lexical
closures
Pure
functions
Safe
recursion
No mutating
state
JS
Haskell

Mais conteúdo relacionado

Mais procurados

Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesHùng Nguyễn Huy
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascriptEman Mohamed
 
Pure functions
Pure functionsPure functions
Pure functionsPer Arneng
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmTech Triveni
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8JavaBrahman
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改Jeff Lee
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional styleNiranjan Sarade
 
Functional programming
Functional programmingFunctional programming
Functional programmingijcd
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous JavascriptGarrett Welson
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
lazy evaluation
lazy evaluationlazy evaluation
lazy evaluationRajendran
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
C sharp fundamentals Part I
C sharp fundamentals Part IC sharp fundamentals Part I
C sharp fundamentals Part IDevMix
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function typeChang John
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix CoevalKnoldus Inc.
 
Mathematical Methods in Physics-2 Pages 505-606
Mathematical Methods in Physics-2 Pages 505-606Mathematical Methods in Physics-2 Pages 505-606
Mathematical Methods in Physics-2 Pages 505-606Rajput Abdul Waheed Bhatti
 

Mais procurados (20)

Callback Function
Callback FunctionCallback Function
Callback Function
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
Asynchronous javascript
 Asynchronous javascript Asynchronous javascript
Asynchronous javascript
 
Pure functions
Pure functionsPure functions
Pure functions
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
lazy evaluation
lazy evaluationlazy evaluation
lazy evaluation
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
C sharp fundamentals Part I
C sharp fundamentals Part IC sharp fundamentals Part I
C sharp fundamentals Part I
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function type
 
Functional Go
Functional GoFunctional Go
Functional Go
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix Coeval
 
Mathematical Methods in Physics-2 Pages 505-606
Mathematical Methods in Physics-2 Pages 505-606Mathematical Methods in Physics-2 Pages 505-606
Mathematical Methods in Physics-2 Pages 505-606
 

Destaque

HDFS & MapReduce
HDFS & MapReduceHDFS & MapReduce
HDFS & MapReduceSkillspeed
 
Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Erhwen Kuo
 
Spark手把手:[e2-spk-s03]
Spark手把手:[e2-spk-s03]Spark手把手:[e2-spk-s03]
Spark手把手:[e2-spk-s03]Erhwen Kuo
 
Spark手把手:[e2-spk-s01]
Spark手把手:[e2-spk-s01]Spark手把手:[e2-spk-s01]
Spark手把手:[e2-spk-s01]Erhwen Kuo
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languagespy7rjs
 
Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Calvin Cheng
 
Chapter 13 programming languages and program development
Chapter 13   programming languages and program developmentChapter 13   programming languages and program development
Chapter 13 programming languages and program developmenthaider ali
 
Spark手把手:[e2-spk-s04]
Spark手把手:[e2-spk-s04]Spark手把手:[e2-spk-s04]
Spark手把手:[e2-spk-s04]二文 郭
 
Run Your First Hadoop 2.x Program
Run Your First Hadoop 2.x ProgramRun Your First Hadoop 2.x Program
Run Your First Hadoop 2.x ProgramSkillspeed
 
Getting started with Apache Spark
Getting started with Apache SparkGetting started with Apache Spark
Getting started with Apache SparkHabib Ahmed Bhutto
 
Software Engineering - chp1- software dev methodologies
Software Engineering - chp1- software dev methodologiesSoftware Engineering - chp1- software dev methodologies
Software Engineering - chp1- software dev methodologiesLilia Sfaxi
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Netguru
 
Principles Of Programing Languages
Principles Of Programing LanguagesPrinciples Of Programing Languages
Principles Of Programing LanguagesMatthew McCullough
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Pritom Chaki
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In JavaAndrei Solntsev
 
Software Engineering - chp6- development phase
Software Engineering - chp6- development phaseSoftware Engineering - chp6- development phase
Software Engineering - chp6- development phaseLilia Sfaxi
 

Destaque (20)

HDFS & MapReduce
HDFS & MapReduceHDFS & MapReduce
HDFS & MapReduce
 
Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]Spark手把手:[e2-spk-s02]
Spark手把手:[e2-spk-s02]
 
Spark手把手:[e2-spk-s03]
Spark手把手:[e2-spk-s03]Spark手把手:[e2-spk-s03]
Spark手把手:[e2-spk-s03]
 
Spark手把手:[e2-spk-s01]
Spark手把手:[e2-spk-s01]Spark手把手:[e2-spk-s01]
Spark手把手:[e2-spk-s01]
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
 
Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)Functional Programming for OO Programmers (part 2)
Functional Programming for OO Programmers (part 2)
 
Chapter 13 programming languages and program development
Chapter 13   programming languages and program developmentChapter 13   programming languages and program development
Chapter 13 programming languages and program development
 
Spark手把手:[e2-spk-s04]
Spark手把手:[e2-spk-s04]Spark手把手:[e2-spk-s04]
Spark手把手:[e2-spk-s04]
 
Scala+RDD
Scala+RDDScala+RDD
Scala+RDD
 
Run Your First Hadoop 2.x Program
Run Your First Hadoop 2.x ProgramRun Your First Hadoop 2.x Program
Run Your First Hadoop 2.x Program
 
Getting started with Apache Spark
Getting started with Apache SparkGetting started with Apache Spark
Getting started with Apache Spark
 
ScalaTrainings
ScalaTrainingsScalaTrainings
ScalaTrainings
 
Scala+spark 2nd
Scala+spark 2ndScala+spark 2nd
Scala+spark 2nd
 
Software Engineering - chp1- software dev methodologies
Software Engineering - chp1- software dev methodologiesSoftware Engineering - chp1- software dev methodologies
Software Engineering - chp1- software dev methodologies
 
General OOP concept [by-Digvijay]
General OOP concept [by-Digvijay]General OOP concept [by-Digvijay]
General OOP concept [by-Digvijay]
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?
 
Principles Of Programing Languages
Principles Of Programing LanguagesPrinciples Of Programing Languages
Principles Of Programing Languages
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
Software Engineering - chp6- development phase
Software Engineering - chp6- development phaseSoftware Engineering - chp6- development phase
Software Engineering - chp6- development phase
 

Semelhante a Functional Programming for OO Programmers (part 1)

Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
Functional programming
Functional programmingFunctional programming
Functional programmingPiumiPerera7
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Functional Swift
Functional SwiftFunctional Swift
Functional SwiftGeison Goes
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptxManas40552
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programmingsamthemonad
 
Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functionssparkfabrik
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional ProgrammingDave Fancher
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRutvik Pensionwar
 
Functional programming principles and Java 8
Functional programming principles and Java 8Functional programming principles and Java 8
Functional programming principles and Java 8Dragos Balan
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
introtofunctionalprogramming2-170301075633.pdf
introtofunctionalprogramming2-170301075633.pdfintrotofunctionalprogramming2-170301075633.pdf
introtofunctionalprogramming2-170301075633.pdfRodulfoGabrito
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in RubyAlex Teut
 

Semelhante a Functional Programming for OO Programmers (part 1) (20)

Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Functional Swift
Functional SwiftFunctional Swift
Functional Swift
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Function
Function Function
Function
 
Functional programming is the most extreme programming
Functional programming is the most extreme programmingFunctional programming is the most extreme programming
Functional programming is the most extreme programming
 
Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functions
 
Basic c++
Basic c++Basic c++
Basic c++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Functional programming principles and Java 8
Functional programming principles and Java 8Functional programming principles and Java 8
Functional programming principles and Java 8
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
introtofunctionalprogramming2-170301075633.pdf
introtofunctionalprogramming2-170301075633.pdfintrotofunctionalprogramming2-170301075633.pdf
introtofunctionalprogramming2-170301075633.pdf
 
Functional Programming in Ruby
Functional Programming in RubyFunctional Programming in Ruby
Functional Programming in Ruby
 

Mais de Calvin Cheng

FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinFOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinCalvin Cheng
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4Calvin Cheng
 
iOS Beginners Lesson 3
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3Calvin Cheng
 
iOS Beginners Lesson 2
iOS Beginners Lesson 2iOS Beginners Lesson 2
iOS Beginners Lesson 2Calvin Cheng
 
iOS Beginners Lesson 1
iOS Beginners Lesson 1iOS Beginners Lesson 1
iOS Beginners Lesson 1Calvin Cheng
 
So, you want to build a Bluetooth Low Energy device?
So, you want to build a Bluetooth Low Energy device?So, you want to build a Bluetooth Low Energy device?
So, you want to build a Bluetooth Low Energy device?Calvin Cheng
 
Learning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksLearning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksCalvin Cheng
 
Django101 geodjango
Django101 geodjangoDjango101 geodjango
Django101 geodjangoCalvin Cheng
 
Saving Gaia with GeoDjango
Saving Gaia with GeoDjangoSaving Gaia with GeoDjango
Saving Gaia with GeoDjangoCalvin Cheng
 
Agile Apps with App Engine
Agile Apps with App EngineAgile Apps with App Engine
Agile Apps with App EngineCalvin Cheng
 

Mais de Calvin Cheng (14)

FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/SovrinFOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
FOSSASIA 2018 Self-Sovereign Identity with Hyperledger Indy/Sovrin
 
Hashgraph as Code
Hashgraph as CodeHashgraph as Code
Hashgraph as Code
 
iOS Beginners Lesson 4
iOS Beginners Lesson 4iOS Beginners Lesson 4
iOS Beginners Lesson 4
 
iOS Beginners Lesson 3
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3
 
iOS Beginners Lesson 2
iOS Beginners Lesson 2iOS Beginners Lesson 2
iOS Beginners Lesson 2
 
iOS Beginners Lesson 1
iOS Beginners Lesson 1iOS Beginners Lesson 1
iOS Beginners Lesson 1
 
So, you want to build a Bluetooth Low Energy device?
So, you want to build a Bluetooth Low Energy device?So, you want to build a Bluetooth Low Energy device?
So, you want to build a Bluetooth Low Energy device?
 
Fabric
FabricFabric
Fabric
 
Learning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeksLearning iOS and hunting NSZombies in 3 weeks
Learning iOS and hunting NSZombies in 3 weeks
 
Ladypy 01
Ladypy 01Ladypy 01
Ladypy 01
 
zhng your vim
zhng your vimzhng your vim
zhng your vim
 
Django101 geodjango
Django101 geodjangoDjango101 geodjango
Django101 geodjango
 
Saving Gaia with GeoDjango
Saving Gaia with GeoDjangoSaving Gaia with GeoDjango
Saving Gaia with GeoDjango
 
Agile Apps with App Engine
Agile Apps with App EngineAgile Apps with App Engine
Agile Apps with App Engine
 

Último

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 

Último (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Functional Programming for OO Programmers (part 1)

  • 1. FUNCTIONAL PROGRAMMING for OO programmers (Part 1)
  • 2. HELLO I am Calvin (Cheng) @calvinchengx calvin.cheng.lc calvinchengx calvinx.com
  • 4. WHY • Different implementation paradigms for different parts of your program (or system) to produce high quality software with minimal bugs • Leverage on immutability - shared mutable state is the root of all evil (data race conditions, program complexity etc) functional programming?
  • 5. WHY functional programming? • Function purity gives you these benefits 1. testable 2. portable 3. memoizable 4. parallelizable
  • 6. WHY functional programming? • Real world software 1. Clojure in Akamai 2. Scala inTwitter 3. Haskell in AT&T and in Standard Chartered 4. Reactjs' immutable state and virtual dom in FB
  • 7. A COMPARISON procedural vs OO vs functional • Programming paradigms are world views 1. most languages are on a sliding scale 2. we can apply the different paradigms on any given turing-complete language
  • 8. A COMPARISON procedural (“imperative”) • Code is organised into “small procedures” that use and change our data
  • 9. A COMPARISON object-oriented • Code is organised as ‘templates’ and instantiated with objects with data, that changes
  • 10. A COMPARISON functional • Code is organised as composable functions and given a particular input data, will always return the same output data
  • 11. A COMPARISON procedural vs OO vs functional • Testable procedural globally shared data makes it hard to test. OO requires mock libraries which adds complexity to testing. functional pure functions always return an expected value given a particular input. So testing becomes trivial.
  • 12. A COMPARISON procedural vs OO vs functional • Portable procedural procedures are often one-off. OO portability is good but complex inheritance chains result in unexpected behaviors. functional pure functions are completely portable.
  • 13. A COMPARISON procedural vs OO vs functional • Memoizable procedural Not memoizable. OO Not memoizable. functional pure functions are completely memoizable.
  • 14. A COMPARISON procedural vs OO vs functional • Parallelizable procedural Rarely parallelizable. OO Rarely parallelizable due to shared mutable state. functional pure functions are always parallelizable.
  • 15. WHAT • First-class functions • Functions can return functions • Lexical closures • Pure functions • Safe recursion • No mutating state makes a language functional?
  • 17. FIRST-CLASS var  f  =  function(a,  b)  {      return  a  +  b;   };   var  callFunctionAndArgs  =  function(f,  a,  b)  {      return  f(a,  b);   };   callFunctionAndArgs(f,  1,  2);   functions
  • 19. FUNCTIONS var  f  =  function(a,  b)  {      return  a  +  b;   };   var  applyFirst  =  function  (f,  a)  {      return  function(b)  {            return  f(a,  b);      };   };   //  applyFirst  returns  a  function  that  has  not  been  evaluated   //  this  is  applicable  for  `currying`  in  functional  paradigm can return functions
  • 20. LEXICAL SCOPE free variables belong to parents cope
  • 21. LEXICAL SCOPE function  add(x)  {      console.log('x  is  '  +  x);      return  function(y)  {          return  x  +  y;        };   }   var  add10  =  add(10);   console.log(add10);   //  console.log('x  is  '  +  x);  x  is  not  defined  in  outside  of  the  `add`  function   console.log(add10(20));   free variables belong to parent scope belongs to parent scope returns [Function] returns 30
  • 22. PURE FUNCTIONS with no side effects
  • 23. PURE FUNCTIONS • Functions that don’t change anything out-of-scope and don’t depend on anything out-of-scope are called “pure” • A pure function always gives the same result given the same parameters; independent of program/ system state with no side effects
  • 25. SAFE RECURSION function  factorial(n,  accumulator)  {      if  (n  ===  0)  {          return  accumulator;      }      return  factorial(n  -­‐  1,  n  *  accumulator);   }   //  recursively  call  itself  from  5  to  0,  breaking  at  n  ===  0   factorial(5,  1)     tail call with proper tail call
  • 26. SAFE RECURSION • Proper tail call is mechanism by which language compiler/interpreter recognises tail calls and reuses call frames so call stack does not grow • JavaScript does not use proper tail call (so use with care for recurse depth of no more than 10,000) • ES6 JavaScript does with proper tail call
  • 27. NO MUTATING STATE so everything is predictable
  • 28. NO MUTATING STATE • Using pure functions to compose your program result in a program that does not have shared mutable state. • JavaScript - writing pure functions or programs with no shared mutation state is possible; but not guaranteed. so everything is predictable
  • 29. WHAT makes a language functional? First-class functions Higher-order functions Lexical closures Pure functions Safe recursion No mutating state JS Haskell