Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Programming Functional Programming

Similar presentations


Presentation on theme: "Intro to Programming Functional Programming"— Presentation transcript:

1 Intro to Programming Functional Programming
CIT 590 Intro to Programming Functional Programming

2 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

3 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

4 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

5 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’.

6 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 )

7 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? [‘Kevin Lee’, ‘Arvind Bhusnurmath’] ‘Arvind Bhusnurmath’ [‘Arvind Bhusnurmath’] set([‘Kevin Lee’, ‘Arvind Bhusnurmath’]) (‘Kevin Lee’, ‘Arvind Bhusnurmath’)

8 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? [‘KADC’] ‘KADC’ ‘Kevin Arvind Di Ce’ ‘Kevin LeeADC’ [‘Kevin’, ‘A’, ‘D’, ‘C’]

9 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? [‘Lee Kevin’, ‘Bhusnurmath Arvind’, ‘Lu Di’, ‘Wang Ce’] [‘Lee’, ‘Bhusnurmath’, ‘Lu’, ‘Wang’] [‘Klee’, ‘Abhusnurmath’, ‘Dlu’, ‘Cwang’] None [‘LeeKevin’ ‘BhusnurmathArvind’, ‘LiDu’, ‘WangCe’]

10 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])

11 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???

12 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

13 Quicksort List comprehension = badass  Quicksort.py

14 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 ppt "Intro to Programming Functional Programming"

Similar presentations


Ads by Google