CIT 590 Intro to Programming Functional Programming

advertisement
CIT 590
Intro to Programming
Functional Programming
What is a function?
• f(x) = 2x
• Very crudely put function turns inputs into outputs
• Can I pass a function as an argument to another function
• YES!
• The first time you see this, it looks crazy
• Just start thinking of a function as a machine that turns
something into something else
Is functional programming actually used?
• YES YES YES
• C# uses it like you wouldn’t believe. Even more so if you
use LINQ
• Scala, being taught in 591 uses it a lot!
• It offers way more flexibility
• Do not dismiss it the way I once did
Functional programming
• Functions taking other functions as an argument are
called higher order functions
• We have already seen an example of this – sort
• Check out example of complex number sorting
Lambda functions
• Lambda x : x + 2
• Lambda x, y : x + y
think of lambda’s as ‘I am lazy and I do not want to write a
function so let us do this instead’.
Map, filter and reduce
Python provides this as
• Map – transform everything in the iterable (things you can
loop through)
• Filter – filter those that return the value true
• Reduce – a two argument function
ls1 = map(lambda x: x +2, a)
ls2 = filter(lambda x: x%2 == 0, a)
ls3 = reduce(lambda x, y : x+y, a )
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
n1 = filter(lambda x: len(x) > 8, names)
What is n1?
a) [‘Kevin Lee’, ‘Arvind Bhusnurmath’]
b) ‘Arvind Bhusnurmath’
c) [‘Arvind Bhusnurmath’]
d) set([‘Kevin Lee’, ‘Arvind Bhusnurmath’])
e) (‘Kevin Lee’, ‘Arvind Bhusnurmath’)
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
n2 = reduce(lambda x,y : x + y[0], names)
What is n2?
a) [‘KADC’]
b) ‘KADC’
c) ‘Kevin Arvind Di Ce’
d) ‘Kevin LeeADC’
e) [‘Kevin’, ‘A’, ‘D’, ‘C’]
Map filter reduce on strings
• names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce
Wang']
n3 = map(lambda x : x.split()[-1] + x.split()[0], names)
What is n3?
a) [‘Lee Kevin’, ‘Bhusnurmath Arvind’, ‘Lu Di’, ‘Wang Ce’]
b) [‘Lee’, ‘Bhusnurmath’, ‘Lu’, ‘Wang’]
c) [‘Klee’, ‘Abhusnurmath’, ‘Dlu’, ‘Cwang’]
d) None
e) [‘LeeKevin’ ‘BhusnurmathArvind’, ‘LiDu’, ‘WangCe’]
List comprehensions
• [expr for var in list if expr]
• Basically a cool way of combining the map and filter
functions
• [x*2 for x in a if x<2]
• These, thankfully, do not work by side effect
Lst = [x*2 for x in a if x < 2]
d = {'fred' : 1, 'arv': 5, 'li': 7, 'aash': 4, 'bye': 5}
reduce(lambda x,y: (x+y)/2, [d[x]/2 for x in d.keys() if len(x)%2 == 0])
Sieve example
• Functional programming lends itself to recursion really
•
•
•
•
•
easily
Erasthothenes algorithm for getting rid of composite
numbers in a list of increasing numbers beginning with the
number 2
An ancient algorithm for finding prime numbers
From any list of increasing numbers remove the multiples
of any number in the list
Easy for list of size 1
Can I do this recursively???
The 3 argument version of the reduce
function
• We have already seem reduce(function, list)
• There is a 3 argument variant which is
• Reduce(function, list, identity element/first element for the process
• Sorting examples
• Define insertion of an element into a sorted list
• Now use a reduce operation
Quicksort
• List comprehension = badass 
• Quicksort.py
Common super useful reductions
• Reduce(lambda x,y : x+y ,a)
• Sigma/summation
• Reduce(lambda x,y: x*y , a, 1)
• product
• Reduce(lambda x,y: x + 1, a, 0)
• Num elements
• Reduce(lambda x,y: [y] + x, a, [])
• reverse
• the usage of reductions when dealing with lists
Download