SlideShare uma empresa Scribd logo
1 de 31
Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com
Functional Programming
High Level Introduction
elada@sela.co.il
http://il.linkedin.com/in/eladavneri/
http://blogs.microsoft.co.il/elada
Elad Avneri
Architect, ALM and Software
Development
Agenda
The Basics
Principles
FP Languages
Features
Comparison
Demo: Imperative to FP Evolution
3
Software Development Challenges
Modern software becomes larger and more
complex
We’d like to reduce the time and cost of software
development
We’d like to increase the quality of the
developed software
FP to the Rescue
Promotes composition and
modularity
Promotes simpler code
Concise, promotes smaller code
Concurrency is not an issue
Dramatically reduces number of
bugs
The Goal of this Presentation
Is not to convince to abandon OOP
Is to open your mind for additional
paradigm
Learn techniques which you can
utilize in your favorite
programming language
http://theburningmonk.com/2015/04/dont-learn-a-syntax-learn-to-change-the-way-you-think/
A
A
An appetizer:
sorted :: [Integer] -> Bool
sorted [] = True
sorted [_] = True
sorted (x:y:zs) = x < y && sorted (y:zs)
So, What Is It All About?
Functional ProgrammingOther programming styles
Classes, proceduresBuilding Blocks
N/AA mustState changes
Function calls,Loops, conditionals, and
function (method) calls
Main control flow
FP Principles
Immutability
Pure functions
Modularity and composition
Declarative
Immutability
Thread safe
Data flow is explicit
Cacheable objects
"How many Haskell programmers does it take to
change a lightbulb?“ "Haskell programmers don't
"change" lightbulbs, they "replace" them. And you
must also replace the whole house at the same
time.”
Idempotence: Get the same results no
matter how many times a function is called
We gain the ability to cache results for
function calls
Insanity: doing the same thing over and over
again and expecting different results.
Albert Einstein
Pure Functions
A function has no side effect
 We gain clarity
 We gain “laziness”:
int foo()
{
bar();
return 0;
}
Pure Functions
Function calls are independent
Can be parallelized
result = func1(x,y) + func2(x,z)
Pure Functions
Pure Functions
Encapsulation is in the function level
Easier to debug
Easier to test in isolation
Modularity and Composability
A function’s output is only its return value
We gain:
Modularity with light coupling
Composability
Declarative
The focus is on what the computer should do
rather than how it should do it
Think of a spreadsheet:
We don’t specify the order of calculations
We don’t deal with variables
We deal with expressions and not with steps
We tell it what we want not how to achieve it
Think of SQL…
FP is the same but for general purpose
Imperative
List<int> results = new List<int>();
foreach(var num in collection)
{
if (num % 2 != 0)
results.Add(num);
}
Declarative vs Imperative
Declarative
var results = collection.Where( num => num % 2 != 0);
List<int> collection = new List<int> { 1, 2, 3, 4, 5 };
http://stackoverflow.com/a/1784702/871579
Myths and Misconceptions
It’s difficult
It’s not difficult it’s “just” unfamiliar
We can do it also in XXX (= your favorite
language)
Maybe, but “real” FP language is “pushing” you there
It’s not practical for real-world applications
Simply wrong
FP has poor performance
Actually some of the features gives great
optimization opportunities
The quality code vs optimized code tradeoff
FP is here from the ‘50s
At the beginning computers were slow
Not enough power to process abstractions
Now it’s back!
The Retro of FP
Functional Programming/Language
Functional programming is style of
programming
A functional language is one that supports and
encourages the functional style
(Some) Functional Language Features
Separate data from behavior
Functions as First-Class Values
High-Order Functions
Recursion
Pattern matching
Algebraic data types
Lazy evaluation
Separate Data from Behavior
No classes and objects
Data passes between functions as parameters
and return values
Functions as First-Class Values
You can do with functions whatever you can do
with other data-types. E.g.:
Bind identifier to a function
Store a function in a data structure
let squareIt = fun n -> n * n
let doubleIt = fun n -> 2 * n
let funList = [ squareIt; doubleIt ]
Functions as First-Class Values
High order functions
let compose =
fun op1 op2 ->
// Use a let expression to build the function that will
be returned.
let funToReturn = fun n ->
op1 (op2 n)
// Then just return it.
funToReturn
let squareAndDouble = compose doubleIt squareIt
// The following expression squares 3, doubles 9, returns 18, and
// then displays 18.
System.Console.WriteLine(squareAndDouble 3)
Recursion
No loops
factorial n = if n < 2 then 1 else n * factorial (n-1)
Pattern Matching
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs
Algebraic Data Types
data Bool = False | True
data Point = Point Float Float deriving (Show)
data Shape =
Circle Point Float | Rectangle Point Point deriving (Show)
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 -
x1) * (abs $ y2 - y1)
Lazy Evaluation
Evaluation is deferred until their results are
needed
Allows using infinite listsnats = [0..]
odds = [1,3..]
ones = 1 : ones
-- What will happen when executing the following?
length odds
take 5 odds
Language Comparison
F#ScalaHaskell
CLR (.Net)JVMNativeRuntime
Medium1High (-)HighFP Adherence
Default (but may
be not)
Default (but may
be not)
YesImmutability
YesNot by default (but
can be)
YesLazy Evaluation
YesYesYesAlgebraic Data
Types
1) E.g. functions are not guaranteed to be pure: let tm = DateTime.Now
Demo
Imperative to FP Evolution
What should you Take?
Don’t learn a syntax, learn to change the way
you think
Add new paradigms to your toolbox
Get out of your comfort zone
Questions
Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com
Thank You
elada@sela.co.il
http://il.linkedin.com/in/eladavneri/
http://blogs.microsoft.co.il/elada
Elad Avneri
Architect, ALM and Software
Development

Mais conteúdo relacionado

Mais procurados

Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
hermiraguilar
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
hccit
 
Pseudocode flowcharts
Pseudocode flowchartsPseudocode flowcharts
Pseudocode flowcharts
nicky_walters
 

Mais procurados (20)

Algorithm and psuedocode
Algorithm and psuedocodeAlgorithm and psuedocode
Algorithm and psuedocode
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
02 Control Structures - Loops & Conditions
02 Control Structures - Loops & Conditions02 Control Structures - Loops & Conditions
02 Control Structures - Loops & Conditions
 
Basic computer-programming-2
Basic computer-programming-2Basic computer-programming-2
Basic computer-programming-2
 
Unit 3
Unit 3Unit 3
Unit 3
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
ICP - Lecture 6
ICP - Lecture 6ICP - Lecture 6
ICP - Lecture 6
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
C programming enumeration
C programming enumerationC programming enumeration
C programming enumeration
 
Programing Fundamental
Programing FundamentalPrograming Fundamental
Programing Fundamental
 
Unit 3 Foc
Unit  3 FocUnit  3 Foc
Unit 3 Foc
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHMCLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
 
Pseudocode flowcharts
Pseudocode flowchartsPseudocode flowcharts
Pseudocode flowcharts
 
TechDaysNL 2015 - DDD with F#
TechDaysNL 2015 - DDD with F#TechDaysNL 2015 - DDD with F#
TechDaysNL 2015 - DDD with F#
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 

Semelhante a Functional pogramming hl overview

BP206 - Let's Give Your LotusScript a Tune-Up
BP206 - Let's Give Your LotusScript a Tune-Up BP206 - Let's Give Your LotusScript a Tune-Up
BP206 - Let's Give Your LotusScript a Tune-Up
Craig Schumann
 

Semelhante a Functional pogramming hl overview (20)

ForLoops.pptx
ForLoops.pptxForLoops.pptx
ForLoops.pptx
 
GSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.comGSP 215 RANK Introduction Education--gsp215rank.com
GSP 215 RANK Introduction Education--gsp215rank.com
 
GSP 215 RANK Education Counseling--gsp215rank.com
 GSP 215 RANK Education Counseling--gsp215rank.com GSP 215 RANK Education Counseling--gsp215rank.com
GSP 215 RANK Education Counseling--gsp215rank.com
 
GSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.comGSP 215 RANK Education Planning--gsp215rank.com
GSP 215 RANK Education Planning--gsp215rank.com
 
Computer Programming Computer Programming
Computer Programming Computer ProgrammingComputer Programming Computer Programming
Computer Programming Computer Programming
 
GSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.comGSP 215 RANK Education Counseling -- gsp215rank.com
GSP 215 RANK Education Counseling -- gsp215rank.com
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
BP206 - Let's Give Your LotusScript a Tune-Up
BP206 - Let's Give Your LotusScript a Tune-Up BP206 - Let's Give Your LotusScript a Tune-Up
BP206 - Let's Give Your LotusScript a Tune-Up
 
GSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.comGSP 215 RANK Education Your Life--gsp215rank.com
GSP 215 RANK Education Your Life--gsp215rank.com
 
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.comGSP 215 RANK Lessons in Excellence-- gsp215rank.com
GSP 215 RANK Lessons in Excellence-- gsp215rank.com
 
GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com GSP 215 RANK Inspiring Innovation--gsp215rank.com
GSP 215 RANK Inspiring Innovation--gsp215rank.com
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
ELAVARASAN.pdf
ELAVARASAN.pdfELAVARASAN.pdf
ELAVARASAN.pdf
 
structured programming Introduction to c fundamentals
structured programming Introduction to c fundamentalsstructured programming Introduction to c fundamentals
structured programming Introduction to c fundamentals
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
What every beginning developer should know
What every beginning developer should knowWhat every beginning developer should know
What every beginning developer should know
 
Computation Chapter 4
Computation Chapter 4Computation Chapter 4
Computation Chapter 4
 

Último

Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Último (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
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-...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%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
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%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
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
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...
 
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
 
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
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
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
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Functional pogramming hl overview

  • 1. Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com Functional Programming High Level Introduction elada@sela.co.il http://il.linkedin.com/in/eladavneri/ http://blogs.microsoft.co.il/elada Elad Avneri Architect, ALM and Software Development
  • 3. 3 Software Development Challenges Modern software becomes larger and more complex We’d like to reduce the time and cost of software development We’d like to increase the quality of the developed software
  • 4. FP to the Rescue Promotes composition and modularity Promotes simpler code Concise, promotes smaller code Concurrency is not an issue Dramatically reduces number of bugs
  • 5. The Goal of this Presentation Is not to convince to abandon OOP Is to open your mind for additional paradigm Learn techniques which you can utilize in your favorite programming language http://theburningmonk.com/2015/04/dont-learn-a-syntax-learn-to-change-the-way-you-think/
  • 6. A A An appetizer: sorted :: [Integer] -> Bool sorted [] = True sorted [_] = True sorted (x:y:zs) = x < y && sorted (y:zs) So, What Is It All About? Functional ProgrammingOther programming styles Classes, proceduresBuilding Blocks N/AA mustState changes Function calls,Loops, conditionals, and function (method) calls Main control flow
  • 8. Immutability Thread safe Data flow is explicit Cacheable objects "How many Haskell programmers does it take to change a lightbulb?“ "Haskell programmers don't "change" lightbulbs, they "replace" them. And you must also replace the whole house at the same time.”
  • 9. Idempotence: Get the same results no matter how many times a function is called We gain the ability to cache results for function calls Insanity: doing the same thing over and over again and expecting different results. Albert Einstein Pure Functions
  • 10. A function has no side effect  We gain clarity  We gain “laziness”: int foo() { bar(); return 0; } Pure Functions
  • 11. Function calls are independent Can be parallelized result = func1(x,y) + func2(x,z) Pure Functions
  • 12. Pure Functions Encapsulation is in the function level Easier to debug Easier to test in isolation
  • 13. Modularity and Composability A function’s output is only its return value We gain: Modularity with light coupling Composability
  • 14. Declarative The focus is on what the computer should do rather than how it should do it Think of a spreadsheet: We don’t specify the order of calculations We don’t deal with variables We deal with expressions and not with steps We tell it what we want not how to achieve it Think of SQL… FP is the same but for general purpose
  • 15. Imperative List<int> results = new List<int>(); foreach(var num in collection) { if (num % 2 != 0) results.Add(num); } Declarative vs Imperative Declarative var results = collection.Where( num => num % 2 != 0); List<int> collection = new List<int> { 1, 2, 3, 4, 5 }; http://stackoverflow.com/a/1784702/871579
  • 16. Myths and Misconceptions It’s difficult It’s not difficult it’s “just” unfamiliar We can do it also in XXX (= your favorite language) Maybe, but “real” FP language is “pushing” you there It’s not practical for real-world applications Simply wrong FP has poor performance Actually some of the features gives great optimization opportunities The quality code vs optimized code tradeoff
  • 17. FP is here from the ‘50s At the beginning computers were slow Not enough power to process abstractions Now it’s back! The Retro of FP
  • 18. Functional Programming/Language Functional programming is style of programming A functional language is one that supports and encourages the functional style
  • 19. (Some) Functional Language Features Separate data from behavior Functions as First-Class Values High-Order Functions Recursion Pattern matching Algebraic data types Lazy evaluation
  • 20. Separate Data from Behavior No classes and objects Data passes between functions as parameters and return values
  • 21. Functions as First-Class Values You can do with functions whatever you can do with other data-types. E.g.: Bind identifier to a function Store a function in a data structure let squareIt = fun n -> n * n let doubleIt = fun n -> 2 * n let funList = [ squareIt; doubleIt ]
  • 22. Functions as First-Class Values High order functions let compose = fun op1 op2 -> // Use a let expression to build the function that will be returned. let funToReturn = fun n -> op1 (op2 n) // Then just return it. funToReturn let squareAndDouble = compose doubleIt squareIt // The following expression squares 3, doubles 9, returns 18, and // then displays 18. System.Console.WriteLine(squareAndDouble 3)
  • 23. Recursion No loops factorial n = if n < 2 then 1 else n * factorial (n-1)
  • 24. Pattern Matching factorial :: Integer -> Integer factorial 0 = 1 factorial n = n * factorial (n - 1) fib :: Integer -> Integer fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2) length' :: (Num b) => [a] -> b length' [] = 0 length' (_:xs) = 1 + length' xs
  • 25. Algebraic Data Types data Bool = False | True data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) surface :: Shape -> Float surface (Circle _ r) = pi * r ^ 2 surface (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
  • 26. Lazy Evaluation Evaluation is deferred until their results are needed Allows using infinite listsnats = [0..] odds = [1,3..] ones = 1 : ones -- What will happen when executing the following? length odds take 5 odds
  • 27. Language Comparison F#ScalaHaskell CLR (.Net)JVMNativeRuntime Medium1High (-)HighFP Adherence Default (but may be not) Default (but may be not) YesImmutability YesNot by default (but can be) YesLazy Evaluation YesYesYesAlgebraic Data Types 1) E.g. functions are not guaranteed to be pure: let tm = DateTime.Now
  • 29. What should you Take? Don’t learn a syntax, learn to change the way you think Add new paradigms to your toolbox Get out of your comfort zone
  • 31. Copyright © SELA Software & Education Labs, Ltd. | 14-18 Baruch Hirsch St., Bnei Brak 51202, Israel | www.selagroup.com Thank You elada@sela.co.il http://il.linkedin.com/in/eladavneri/ http://blogs.microsoft.co.il/elada Elad Avneri Architect, ALM and Software Development