Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python.

Similar presentations


Presentation on theme: "CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python."— Presentation transcript:

1 CIT 590 Intro to Programming Lecture 10

2 Agenda Functional programming Functional programming as it is done in Python

3 The three things I will never give an assignment on …. Religion Politics and …. Harry Potter! https://canvas.upenn.edu/courses/1164397/announcemen ts https://canvas.upenn.edu/courses/1164397/announcemen ts

4 What is a function? f(x) = 2x A function works on sets What is a set?.. Is it like lists?? Can I think of a function the way that I would think of lists and dictionaries etc 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

5 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

6 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

7 Lambda functions Lambda x : x + 2 Lambda x, y : x + y In some ways, I think of lambda’s as ‘I am lazy and I do not want to write a function so let us do this instead’.

8 Map, filter and reduce Python provides this as Map – transform everything in the iterable 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 )

9 Map filter reduce on strings names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce Wang'] Use map to convert this list into the lastname, firstname format Use filter to remove all names longer than 11 characters Use reduce to find the longest string

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 = {1: ‘fred’, 2:’arv’, 3:’li’} [d[i] for i in d.keys() if i % 2 == 0]

11 Comparison of imperative and functional programming version (taken from msdn) CharacteristicImperative approachFunctional approach Programmer focusHow to perform tasks (algorithms) and how to track changes in state. What information is desired and what transformations are required. State changesImportant.Non-existent. Order of executionImportant.Low importance. Primary flow controlLoops, conditionals, and function (method) calls. Function calls, including recursion. Primary manipulation unitInstances of structures or classes. Functions as first-class objects and data collections.

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

13 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

14 Quicksort List comprehensions at their badass best! Quicksort.py

15 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 "CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python."

Similar presentations


Ads by Google