SlideShare ist ein Scribd-Unternehmen logo
1 von 80
Downloaden Sie, um offline zu lesen
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
An Introduction to Functional Programming
Andreas Pauley – @apauley
Lambda Luminaries
DeveloperUG
March 11, 2014
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
@lambdaluminary
We meet once a month, on the second Monday of the month.
http://www.meetup.com/lambda-luminaries/
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Jemstep
Retirement portfolio analysis in Scala.
http://www.jemstep.com/
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
“ No matter what language you work in,
programming in a functional style provides benefits.
You should do it whenever it is convenient, and you
should think hard about the decision when it isn’t
convenient. ”
— John Carmack, ID Software [2]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Quake
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
But what exactly is
“Functional Programming”?
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Functional Programming, noun:
Functional Programming is a
list of things you CAN’T do.
[7]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
You can’t vary your variables
1> X = 42.
42
2> X = X + 1.
** exception error:
no match of right hand
side value 43
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
No while/for loops. Sorry :-(
int i;
for (i=1; i<=3; i++) {
System.out.println(i);
}
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
You can’t mutate/change your data structures
Python
>>> list1 = [1,2,3]
>>> list2 = list1
>>> print list1.reverse()
None
>>> list1
[3, 2, 1]
>>> list2
[3, 2, 1]
Haskell
> let list1 = [1,2,3]
> let list2 = list1
> reverse(list1)
[3,2,1]
> list1
[1,2,3]
> list2
[1,2,3]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
You can’t have any side effects
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Are you kidding me?
How can anyone program like this???
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
GOTO 10
This sounds like
“You can’t have GOTO statements”
See Hughes and Dijkstra [1, 3]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
It’s not about what we cannot
do.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
We need a better definition of
Functional Programming.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Programming Paradigms
(Very Simplified)
Imperative Declarative
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Functional Programming, noun:
“ Functional programming is so called because a
program consists entirely of functions. ”
— John Hughes, Why Functional Programming Matters [1, p. 1]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
OK... so what exactly is a function?
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
An example function
f(x) = 2x2 − 2x + 3
−1 1 2 3
4
6
8
10
12
14
x
y
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Variables in functions
f(x) = 2x2 − 2x + 3
When we evaluate the function:
f(2) = 8 − 4 + 3 = 7
• The value of x will not change inside the function body.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Variables in functions
f(x) = 2x2 − 2x + 3
When we evaluate the function:
f(2) = 8 − 4 + 3 = 7
• The value of x will not change inside the function body.
• Same input, same output. Every time. (Referential
Transparency)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Variables in functions
f(x) = 2x2 − 2x + 3
When we evaluate the function:
f(2) = 8 − 4 + 3 = 7
• The value of x will not change inside the function body.
• Same input, same output. Every time. (Referential
Transparency)
• We can call f multiple times without any side effects
(Idempotence).
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Variables in functions
f(x) = 2x2 − 2x + 3
When we evaluate the function:
f(2) = 8 − 4 + 3 = 7
• The value of x will not change inside the function body.
• Same input, same output. Every time. (Referential
Transparency)
• We can call f multiple times without any side effects
(Idempotence).
• We don’t have to recalculate f(2), we can replace any
occurrence of f(2) with 7 (Memoization).
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Varying variables does not make sense
x = x + 1
x − x = 1
0 = 1
∴ x = x + 1
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Functions can call other functions
g(x) = f(x) + 1
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Values are functions
Constant values are just functions with no input parameters
x = 42
Python function definition:
def x():
return 42
Haskell function definition:
x = 42
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Functions can be composed
h(x) = (f ◦ g)(x) = f(g(x))
or
h = f ◦ g
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Higher-order Functions
Functions can take functions as input.
Functions can return functions as the result.
h(f, g, x) = f(x) + g(2)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Higher-order Functions
The derivative of f(x) returns another function.
f(x) = 2x2
− 2x + 3
d
dxf(x) = 4x − 2
−1 1 2 3
5
10
15
x
y
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
A functional program consists entirely of functions
def main():
time = datetime.now()
args = sys.argv[1:]
print outputString(time, args)
def outputString(time, args):
return str(time) + " " + joinArgs(args)
def joinArgs(args):
return "-".join(args)
$ ./justfunctions.py Hello from Python
2014-02-15 10:36:42.062697 Hello-from-Python
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
A functional program consists entirely of functions
main :: IO()
main = do
time <- getCurrentTime
args <- getArgs
putStrLn (outputString time args)
outputString :: UTCTime -> [String] -> String
outputString time args =
show(time) ++ " " ++ joinArgs(args)
joinArgs :: [String] -> String
joinArgs = intercalate "-"
$ ./justfunctions Hello from Haskell
2014-02-15 08:36:50.822728 UTC Hello-from-Haskell
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Some Haskell Syntax
Python:
def outputString(time, args):
return str(time) + " " + joinArgs(args)
Haskell:
outputString :: UTCTime -> [String] -> String
outputString time args =
show(time) ++ " " ++ joinArgs(args)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Recursive function: Haskell
doubleAll :: [Int] -> [Int]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Example use in the interactive interpreter:
Prelude Main> doubleAll [8,2,3]
[16,4,6]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Recursive function expanded
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
doubleAll [8,2,3]
16 : (doubleAll [2,3])
16 : 4 : (doubleAll [3])
16 : 4 : 6 : (doubleAll [])
16 : 4 : 6 : []
16 : 4 : [6]
16 : [4,6]
[16,4,6]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Recursive function: Python
def doubleAll(numbers):
if numbers == []:
return []
else:
first = numbers[0]
rest = numbers[1:]
return [first * 2] + doubleAll(rest)
Example use in the interactive interpreter:
>>> doubleAll([8,2,3])
[16, 4, 6]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Pattern Matching: Haskell
doubleAll :: [Int] -> [Int]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Higher-order Functions
3 Basic List Operations
Map Convert each element of a list into some other value.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Higher-order Functions
3 Basic List Operations
Map Convert each element of a list into some other value.
Filter Get a subset of a list based on some condition.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Higher-order Functions
3 Basic List Operations
Map Convert each element of a list into some other value.
Filter Get a subset of a list based on some condition.
Fold Reduce a list of items to a single value.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
Apply a function to each element of a list, and you get a new list.
a1 a2 a3 ... an
b1 b2 b3
... bn
f(a1) f(an)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
The built-in Haskell map function:
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
The builtin Haskell map function:
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (x:xs) = f x : map f xs
Hmmm, looks very similar to our previous doubleAll function:
doubleAll :: [Int] -> [Int]
doubleAll [] = []
doubleAll (x:xs) = x*2 : doubleAll xs
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
So our doubleAll can actually be simplified.
In Haskell:
doubleAll = map (*2)
In Python:
def doubleAll(numbers):
return map(lambda x: x * 2, numbers)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
doubleAll :: Num a => [a] -> [a]
doubleAll = map (*2)
8 2 3
16 4 6
∗2 ∗2 ∗2
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Account Data
data Bank = ABSA | Capitec | FNB | Nedbank | SBSA
data Account = Account {bank :: Bank,
accNum :: String,
owner :: String,
balance :: Amount}
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Account Data
[Account {accNum="4076814233",
owner="J. Doe",
balance=(Amount 123000.23),
bank=ABSA},
Account {accNum="6868773585",
owner="J. Black",
balance=(Amount 5782347.99),
bank=FNB},
Account {accNum="4055892156",
owner="A. Kay",
balance=(Amount 100),
bank=ABSA},
Account {accNum="6584539813",
owner="S. Jones",
balance=(Amount 2937361.45),
bank=FNB}]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Map
Map on account data:
balances :: [Account] -> [Amount]
balances accounts = map balance accounts
acc1 acc2 acc3 acc4 acc5
bal1 bal2 bal3 bal4 bal5
balance(acc1) balance(acc5)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Filter
acc1 acc2 acc3 acc4 acc5
acc1 acc3 acc5
in? in? in? in? in?
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Filter
Filter on account data:
topAccounts :: [Account] -> [Account]
topAccounts accounts = filter isRich accounts
isRich :: Account -> Bool
isRich acc = balance acc >= (Amount 1000000)
Output:
*Main> topAccounts accounts
[FNB 6868773585 (J. Black) R5782347.99,
FNB 6584539813 (S. Jones) R2937361.45]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Fold/Reduce/Inject
foldl (+) 0 [8,2,3]
13
0 8 2 3
8 2 3
10 3
13
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Fold/Reduce/Inject
balanceSum :: [Account] -> Amount
balanceSum accounts = foldl (+) 0 (balances accounts)
0 bal1 bal2 bal3
sum bal2 bal3
sum bal3
sum
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Function Composition
balanceSum :: [Account] -> Amount
balanceSum accounts = foldl (+) 0 (balances accounts)
0 bal1 bal2 bal3
sum bal2 bal3
sum bal3
sum
s2 :: [Account] -> Amount
s2 = balances |> (foldl(+)0)
h(x) = (f ◦ g)(x) = f(g(x))
h = f ◦ g
s3 = f1 ◦ balances
s3 :: [Account] -> Amount
s3 = (foldl(+)0) . balances
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
balancesPerBank
foldl insertBalance Map.empty accounts
{} acc1 acc2 acc3
map acc2 acc3
map acc3
map
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Fold
type BankMap = Map Bank Amount
balancesPerBank :: [Account] -> BankMap
balancesPerBank = foldl insertBalance Map.empty
Output:
*Main> balancesPerBank accounts
fromList [(ABSA,R123100.23),(FNB,R8719709.44)]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Fold
type BankMap = Map Bank Amount
balancesPerBank :: [Account] -> BankMap
balancesPerBank = foldl insertBalance Map.empty
insertBalance :: BankMap -> Account -> BankMap
insertBalance bankmap account =
Map.insert key value bankmap
where key = bank account
value = addBalance bankmap account
addBalance :: BankMap -> Account -> Amount
addBalance bankmap account =
case (Map.lookup (bank account) bankmap) of
Nothing -> balance account
Just bal -> (balance account) + bal
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Programming Paradigms
(Very Simplified)
Imperative Declarative
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
• Your data structures are mutable!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
• Your data structures are mutable!
• You have to defensively make copies of data to prevent bugs!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
• Your data structures are mutable!
• You have to defensively make copies of data to prevent bugs!
• You have to defensively check for null values!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
• Your data structures are mutable!
• You have to defensively make copies of data to prevent bugs!
• You have to defensively check for null values!
• You have to think about implicit state! (this, self)
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
In imperative programming:
• Your variables can vary any time!
• You have to use locks to be thread-safe!
• You have to write your own loops for the most basic list
operations!
• Your data structures are mutable!
• You have to defensively make copies of data to prevent bugs!
• You have to defensively check for null values!
• You have to think about implicit state! (this, self)
• Code is generally riddled with side effects!
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Are you kidding me?
How can anyone program like this???
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Join the anti-for campaign
Less loops, more
map/filter/fold
http://weblogs.asp.net/podwysocki/archive/2009/06/26/
the-anti-for-campaign.aspx
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Treat side effects as a first-class concern
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Learn a functional language
“ A language that doesn’t affect the way you think
about programming, is not worth knowing. ”
— Alan Perlis[5]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Disclaim your inheritance
Write non-trivial code without
using objects and inheritance.
Get re-usability with higher-order functions.
Try to minimise moving parts instead of
encapsulating moving parts. [4]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Join our group
@lambdaluminary
We meet once a month, on the second Monday of the month.
http://www.meetup.com/lambda-luminaries/
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Get out of your comfort zone
Functional Programming is unfamiliar territory for
most.
“ If you want everything to be familiar you will
never learn anything new. ”
— Rich Hickey, author of Clojure[6]
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Companies In South Africa
Jemstep, Sandton Using Scala for Fund Analysis
Allan Gray, Cape Town Using Scala for backend logic and system
integration.
Yuppiechef, Cape Town Using Clojure for their Warehouse
Management System.
Cognician, Cape Town Using Clojure to create coaching/learning
modules.
Eldo Energy, Johannesburg Using Clojure for automated meter
reading and intelligent monitoring of consumer
energy.
Rheo Systems, Pretoria Using Clojure for supply chain integration.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Companies In South Africa
Pattern Matched Technologies, Midrand Using Erlang for all
systems, eg. processing high volumes of financial
transactions.
Effective Control Systems, Kyalami Using Erlang for printer
management.
Mira Networks, Somerset West Using Erlang for billing
administration and mobile development.
Kotive Using Scala for designing workflow processes.
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Online Courses
Functional Thinking by Neal Ford
O’ Reilly
http://shop.oreilly.com/product/0636920030393.do
Functional Programming Principles in Scala
EPFL University
https://www.coursera.org/course/progfun
School of Haskell
FP Complete
https://www.fpcomplete.com/school
Programming Languages
University of Washington
https://www.coursera.org/course/proglang
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
Books
Miran Lipovaˇca
Learn You a Haskell for Great Good!
http://learnyouahaskell.com/
Fred H´ebert
Learn You Some Erlang for Great Good!
http://learnyousomeerlang.com/
Yaron Minski, Anil Madhavapeddy, Jason Hickey
Real World OCaml
https://realworldocaml.org/
Paul Chiusano, R´unar Bjarnason
Functional Programming in Scala
http://www.manning.com/bjarnason/
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
References I
John Hughes
Why Functional Programming Matters
http://www.cs.kent.ac.uk/people/staff/dat/miranda/
whyfp90.pdf
John Carmack
Functional Programming in C++
http://www.altdevblogaday.com/2012/04/26/
functional-programming-in-c/
Edsger W. Dijkstra
Go To Statement Considered Harmful
http://www.u.arizona.edu/~rubinson/copyright_
violations/Go_To_Considered_Harmful.html
Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
References II
Tweet by Michael Feathers
https://twitter.com/mfeathers/status/29581296216
Alan Jay Perlis
http://www.cs.yale.edu/quotes.html
Rich Hickey
http:
//www.infoq.com/presentations/Simple-Made-Easy
Andreas Pauley
An Introduction to Functional Programming
https://github.com/apauley/fp_presentation

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Omar Abdelhafith
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog3Pillar Global
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsScott Wlaschin
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingJordan Parmer
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingHugo Firth
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programmingnewmedio
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Vadim Dubs
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in PythonColin Su
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Philip Schwarz
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionEelco Visser
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1Abdul Haseeb
 
Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Scott Wlaschin
 

Was ist angesagt? (19)

Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
 
The lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of testsThe lazy programmer's guide to writing thousands of tests
The lazy programmer's guide to writing thousands of tests
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
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
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
Functional programming in Python
Functional programming in PythonFunctional programming in Python
Functional programming in Python
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)Functional Design Patterns (DevTernity 2018)
Functional Design Patterns (DevTernity 2018)
 

Ähnlich wie An Introduction to Functional Programming Concepts

Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad Fabernovel
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in RSoumendra Dhanee
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonCodemotion
 
Programming in c function
Programming in c functionProgramming in c function
Programming in c functionParvez Ahmed
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)Tech in Asia ID
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patternsOlivier Bacs
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Bryan O'Sullivan
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The BasicsRanel Padon
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In JavaAndrei Solntsev
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015senejug
 

Ähnlich wie An Introduction to Functional Programming Concepts (20)

Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
TWINS: OOP and FP - Warburton
TWINS: OOP and FP - WarburtonTWINS: OOP and FP - Warburton
TWINS: OOP and FP - Warburton
 
Programming in c function
Programming in c functionProgramming in c function
Programming in c function
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
Refactoring
RefactoringRefactoring
Refactoring
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 
Python basic
Python basicPython basic
Python basic
 
Python advance
Python advancePython advance
Python advance
 
Python Programming - II. The Basics
Python Programming - II. The BasicsPython Programming - II. The Basics
Python Programming - II. The Basics
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Refactoring
RefactoringRefactoring
Refactoring
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 

Kürzlich hochgeladen

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

An Introduction to Functional Programming Concepts

  • 1. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now An Introduction to Functional Programming Andreas Pauley – @apauley Lambda Luminaries DeveloperUG March 11, 2014
  • 2. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now @lambdaluminary We meet once a month, on the second Monday of the month. http://www.meetup.com/lambda-luminaries/
  • 3. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Jemstep Retirement portfolio analysis in Scala. http://www.jemstep.com/
  • 4. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
  • 5. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
  • 6. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now “ No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient. ” — John Carmack, ID Software [2]
  • 7. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Quake
  • 8. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now But what exactly is “Functional Programming”?
  • 9. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Functional Programming, noun: Functional Programming is a list of things you CAN’T do. [7]
  • 10. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now You can’t vary your variables 1> X = 42. 42 2> X = X + 1. ** exception error: no match of right hand side value 43
  • 11. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now No while/for loops. Sorry :-( int i; for (i=1; i<=3; i++) { System.out.println(i); }
  • 12. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now You can’t mutate/change your data structures Python >>> list1 = [1,2,3] >>> list2 = list1 >>> print list1.reverse() None >>> list1 [3, 2, 1] >>> list2 [3, 2, 1] Haskell > let list1 = [1,2,3] > let list2 = list1 > reverse(list1) [3,2,1] > list1 [1,2,3] > list2 [1,2,3]
  • 13. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now You can’t have any side effects
  • 14. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Are you kidding me? How can anyone program like this???
  • 15. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now GOTO 10 This sounds like “You can’t have GOTO statements” See Hughes and Dijkstra [1, 3]
  • 16. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now It’s not about what we cannot do.
  • 17. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now We need a better definition of Functional Programming.
  • 18. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Programming Paradigms (Very Simplified) Imperative Declarative
  • 19. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Functional Programming, noun: “ Functional programming is so called because a program consists entirely of functions. ” — John Hughes, Why Functional Programming Matters [1, p. 1]
  • 20. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now OK... so what exactly is a function?
  • 21. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now An example function f(x) = 2x2 − 2x + 3 −1 1 2 3 4 6 8 10 12 14 x y
  • 22. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Variables in functions f(x) = 2x2 − 2x + 3 When we evaluate the function: f(2) = 8 − 4 + 3 = 7 • The value of x will not change inside the function body.
  • 23. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Variables in functions f(x) = 2x2 − 2x + 3 When we evaluate the function: f(2) = 8 − 4 + 3 = 7 • The value of x will not change inside the function body. • Same input, same output. Every time. (Referential Transparency)
  • 24. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Variables in functions f(x) = 2x2 − 2x + 3 When we evaluate the function: f(2) = 8 − 4 + 3 = 7 • The value of x will not change inside the function body. • Same input, same output. Every time. (Referential Transparency) • We can call f multiple times without any side effects (Idempotence).
  • 25. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Variables in functions f(x) = 2x2 − 2x + 3 When we evaluate the function: f(2) = 8 − 4 + 3 = 7 • The value of x will not change inside the function body. • Same input, same output. Every time. (Referential Transparency) • We can call f multiple times without any side effects (Idempotence). • We don’t have to recalculate f(2), we can replace any occurrence of f(2) with 7 (Memoization).
  • 26. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Varying variables does not make sense x = x + 1 x − x = 1 0 = 1 ∴ x = x + 1
  • 27. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Functions can call other functions g(x) = f(x) + 1
  • 28. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Values are functions Constant values are just functions with no input parameters x = 42 Python function definition: def x(): return 42 Haskell function definition: x = 42
  • 29. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Functions can be composed h(x) = (f ◦ g)(x) = f(g(x)) or h = f ◦ g
  • 30. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Higher-order Functions Functions can take functions as input. Functions can return functions as the result. h(f, g, x) = f(x) + g(2)
  • 31. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Higher-order Functions The derivative of f(x) returns another function. f(x) = 2x2 − 2x + 3 d dxf(x) = 4x − 2 −1 1 2 3 5 10 15 x y
  • 32. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now A functional program consists entirely of functions def main(): time = datetime.now() args = sys.argv[1:] print outputString(time, args) def outputString(time, args): return str(time) + " " + joinArgs(args) def joinArgs(args): return "-".join(args) $ ./justfunctions.py Hello from Python 2014-02-15 10:36:42.062697 Hello-from-Python
  • 33. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now A functional program consists entirely of functions main :: IO() main = do time <- getCurrentTime args <- getArgs putStrLn (outputString time args) outputString :: UTCTime -> [String] -> String outputString time args = show(time) ++ " " ++ joinArgs(args) joinArgs :: [String] -> String joinArgs = intercalate "-" $ ./justfunctions Hello from Haskell 2014-02-15 08:36:50.822728 UTC Hello-from-Haskell
  • 34. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Some Haskell Syntax Python: def outputString(time, args): return str(time) + " " + joinArgs(args) Haskell: outputString :: UTCTime -> [String] -> String outputString time args = show(time) ++ " " ++ joinArgs(args)
  • 35. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Recursive function: Haskell doubleAll :: [Int] -> [Int] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs Example use in the interactive interpreter: Prelude Main> doubleAll [8,2,3] [16,4,6]
  • 36. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Recursive function expanded doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs doubleAll [8,2,3] 16 : (doubleAll [2,3]) 16 : 4 : (doubleAll [3]) 16 : 4 : 6 : (doubleAll []) 16 : 4 : 6 : [] 16 : 4 : [6] 16 : [4,6] [16,4,6]
  • 37. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Recursive function: Python def doubleAll(numbers): if numbers == []: return [] else: first = numbers[0] rest = numbers[1:] return [first * 2] + doubleAll(rest) Example use in the interactive interpreter: >>> doubleAll([8,2,3]) [16, 4, 6]
  • 38. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Pattern Matching: Haskell doubleAll :: [Int] -> [Int] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs
  • 39. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Higher-order Functions 3 Basic List Operations Map Convert each element of a list into some other value.
  • 40. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Higher-order Functions 3 Basic List Operations Map Convert each element of a list into some other value. Filter Get a subset of a list based on some condition.
  • 41. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Higher-order Functions 3 Basic List Operations Map Convert each element of a list into some other value. Filter Get a subset of a list based on some condition. Fold Reduce a list of items to a single value.
  • 42. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map Apply a function to each element of a list, and you get a new list. a1 a2 a3 ... an b1 b2 b3 ... bn f(a1) f(an)
  • 43. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map The built-in Haskell map function: map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs
  • 44. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map The builtin Haskell map function: map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs Hmmm, looks very similar to our previous doubleAll function: doubleAll :: [Int] -> [Int] doubleAll [] = [] doubleAll (x:xs) = x*2 : doubleAll xs
  • 45. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map So our doubleAll can actually be simplified. In Haskell: doubleAll = map (*2) In Python: def doubleAll(numbers): return map(lambda x: x * 2, numbers)
  • 46. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map doubleAll :: Num a => [a] -> [a] doubleAll = map (*2) 8 2 3 16 4 6 ∗2 ∗2 ∗2
  • 47. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Account Data data Bank = ABSA | Capitec | FNB | Nedbank | SBSA data Account = Account {bank :: Bank, accNum :: String, owner :: String, balance :: Amount}
  • 48. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Account Data [Account {accNum="4076814233", owner="J. Doe", balance=(Amount 123000.23), bank=ABSA}, Account {accNum="6868773585", owner="J. Black", balance=(Amount 5782347.99), bank=FNB}, Account {accNum="4055892156", owner="A. Kay", balance=(Amount 100), bank=ABSA}, Account {accNum="6584539813", owner="S. Jones", balance=(Amount 2937361.45), bank=FNB}]
  • 49. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Map Map on account data: balances :: [Account] -> [Amount] balances accounts = map balance accounts acc1 acc2 acc3 acc4 acc5 bal1 bal2 bal3 bal4 bal5 balance(acc1) balance(acc5)
  • 50. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Filter acc1 acc2 acc3 acc4 acc5 acc1 acc3 acc5 in? in? in? in? in?
  • 51. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Filter Filter on account data: topAccounts :: [Account] -> [Account] topAccounts accounts = filter isRich accounts isRich :: Account -> Bool isRich acc = balance acc >= (Amount 1000000) Output: *Main> topAccounts accounts [FNB 6868773585 (J. Black) R5782347.99, FNB 6584539813 (S. Jones) R2937361.45]
  • 52. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Fold/Reduce/Inject foldl (+) 0 [8,2,3] 13 0 8 2 3 8 2 3 10 3 13
  • 53. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Fold/Reduce/Inject balanceSum :: [Account] -> Amount balanceSum accounts = foldl (+) 0 (balances accounts) 0 bal1 bal2 bal3 sum bal2 bal3 sum bal3 sum
  • 54. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Function Composition balanceSum :: [Account] -> Amount balanceSum accounts = foldl (+) 0 (balances accounts) 0 bal1 bal2 bal3 sum bal2 bal3 sum bal3 sum s2 :: [Account] -> Amount s2 = balances |> (foldl(+)0) h(x) = (f ◦ g)(x) = f(g(x)) h = f ◦ g s3 = f1 ◦ balances s3 :: [Account] -> Amount s3 = (foldl(+)0) . balances
  • 55. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now balancesPerBank foldl insertBalance Map.empty accounts {} acc1 acc2 acc3 map acc2 acc3 map acc3 map
  • 56. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Fold type BankMap = Map Bank Amount balancesPerBank :: [Account] -> BankMap balancesPerBank = foldl insertBalance Map.empty Output: *Main> balancesPerBank accounts fromList [(ABSA,R123100.23),(FNB,R8719709.44)]
  • 57. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Fold type BankMap = Map Bank Amount balancesPerBank :: [Account] -> BankMap balancesPerBank = foldl insertBalance Map.empty insertBalance :: BankMap -> Account -> BankMap insertBalance bankmap account = Map.insert key value bankmap where key = bank account value = addBalance bankmap account addBalance :: BankMap -> Account -> Amount addBalance bankmap account = case (Map.lookup (bank account) bankmap) of Nothing -> balance account Just bal -> (balance account) + bal
  • 58. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Programming Paradigms (Very Simplified) Imperative Declarative
  • 59. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time!
  • 60. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe!
  • 61. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations!
  • 62. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations! • Your data structures are mutable!
  • 63. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations! • Your data structures are mutable! • You have to defensively make copies of data to prevent bugs!
  • 64. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations! • Your data structures are mutable! • You have to defensively make copies of data to prevent bugs! • You have to defensively check for null values!
  • 65. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations! • Your data structures are mutable! • You have to defensively make copies of data to prevent bugs! • You have to defensively check for null values! • You have to think about implicit state! (this, self)
  • 66. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now In imperative programming: • Your variables can vary any time! • You have to use locks to be thread-safe! • You have to write your own loops for the most basic list operations! • Your data structures are mutable! • You have to defensively make copies of data to prevent bugs! • You have to defensively check for null values! • You have to think about implicit state! (this, self) • Code is generally riddled with side effects!
  • 67. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Are you kidding me? How can anyone program like this???
  • 68. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now
  • 69. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Join the anti-for campaign Less loops, more map/filter/fold http://weblogs.asp.net/podwysocki/archive/2009/06/26/ the-anti-for-campaign.aspx
  • 70. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Treat side effects as a first-class concern
  • 71. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Learn a functional language “ A language that doesn’t affect the way you think about programming, is not worth knowing. ” — Alan Perlis[5]
  • 72. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Disclaim your inheritance Write non-trivial code without using objects and inheritance. Get re-usability with higher-order functions. Try to minimise moving parts instead of encapsulating moving parts. [4]
  • 73. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Join our group @lambdaluminary We meet once a month, on the second Monday of the month. http://www.meetup.com/lambda-luminaries/
  • 74. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Get out of your comfort zone Functional Programming is unfamiliar territory for most. “ If you want everything to be familiar you will never learn anything new. ” — Rich Hickey, author of Clojure[6]
  • 75. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Companies In South Africa Jemstep, Sandton Using Scala for Fund Analysis Allan Gray, Cape Town Using Scala for backend logic and system integration. Yuppiechef, Cape Town Using Clojure for their Warehouse Management System. Cognician, Cape Town Using Clojure to create coaching/learning modules. Eldo Energy, Johannesburg Using Clojure for automated meter reading and intelligent monitoring of consumer energy. Rheo Systems, Pretoria Using Clojure for supply chain integration.
  • 76. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Companies In South Africa Pattern Matched Technologies, Midrand Using Erlang for all systems, eg. processing high volumes of financial transactions. Effective Control Systems, Kyalami Using Erlang for printer management. Mira Networks, Somerset West Using Erlang for billing administration and mobile development. Kotive Using Scala for designing workflow processes.
  • 77. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Online Courses Functional Thinking by Neal Ford O’ Reilly http://shop.oreilly.com/product/0636920030393.do Functional Programming Principles in Scala EPFL University https://www.coursera.org/course/progfun School of Haskell FP Complete https://www.fpcomplete.com/school Programming Languages University of Washington https://www.coursera.org/course/proglang
  • 78. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now Books Miran Lipovaˇca Learn You a Haskell for Great Good! http://learnyouahaskell.com/ Fred H´ebert Learn You Some Erlang for Great Good! http://learnyousomeerlang.com/ Yaron Minski, Anil Madhavapeddy, Jason Hickey Real World OCaml https://realworldocaml.org/ Paul Chiusano, R´unar Bjarnason Functional Programming in Scala http://www.manning.com/bjarnason/
  • 79. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now References I John Hughes Why Functional Programming Matters http://www.cs.kent.ac.uk/people/staff/dat/miranda/ whyfp90.pdf John Carmack Functional Programming in C++ http://www.altdevblogaday.com/2012/04/26/ functional-programming-in-c/ Edsger W. Dijkstra Go To Statement Considered Harmful http://www.u.arizona.edu/~rubinson/copyright_ violations/Go_To_Considered_Harmful.html
  • 80. Introduction Definition Function Recap Common Idioms Imperative Comparison Challenges! Industry Use Now References II Tweet by Michael Feathers https://twitter.com/mfeathers/status/29581296216 Alan Jay Perlis http://www.cs.yale.edu/quotes.html Rich Hickey http: //www.infoq.com/presentations/Simple-Made-Easy Andreas Pauley An Introduction to Functional Programming https://github.com/apauley/fp_presentation