Functional Programming

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Clojure 3 Recursion, Higher-order-functions 27-Aug-15.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Epydoc API Documentation Extraction in Python Edward Loper.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
If/else, return, user input, strings
Python: Building Geoprocessing Tools David Wynne, Ghislain Prince.
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Map, List, Stack, Queue, Set Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Higher Order Functions
Reminder About Functions
Introduction to Higher Order (Functional Programming) (Python) part 2
Week 8 Classes and Objects
CSC1018F: Functional Programming
Topic: Functions – Part 2
basic Python programs, defining functions
Functions CIS 40 – Introduction to Programming in Python
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
6 Delegate and Lambda Expressions
Introduction to Python
basic Python programs, defining functions
MapReduce, Dictionaries, List Comprehensions
Coding Concepts (Sub- Programs)
FP Foundations, Scheme In Text: Chapter 14.
Week 8 Classes and Objects
Classes Special thanks to Roy McElmurry, Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
expressions, variables, for loops
expressions, variables, for loops
while loops; logic; random numbers; tuples
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Lambda Functions, MapReduce and List Comprehensions
CISC101 Reminders All assignments are now posted.
Recursion, Higher-order-functions
„Lambda expressions, Optional”
def-ining a function A function as an execution control structure
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Lecture 20 – Practice Exercises 4
© Sangeeta M Chauhan, Gwalior
Week 2 Classes and Objects
Reasoning with Types.
Lecture 2 - Names & Functions
Presentation transcript:

Functional Programming Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

Functions as parameters Have you ever wanted to pass an entire function as a parameter Python has functions as first-class citizens, so you can do this You simply pass the functions by name

Properties of Functions Field Description __name__ This is the name of the function. This only have a meaningful value is the function is defined with “def”. __class__ This is a reference to the class a method belongs to. __code__ This is a reference to the code object used in the implementation of python __doc__ This is the documentation string for the function.

inspect A useful class for inspecting functions and classes. from inspect import * Field Description getdoc(x) Returns a pretty version of the docstring for the give object. getcomments(x) Returns the comments that appear just above the given function/class/module. getsource(x) Returns the source code for the given function/class/module getmembers(x) Returns a list of the members (fields and methods) of a class

Function Parameter Example ex.py 1 2 3 4 5 6 7 8 9 def mult_2(x): return x * 2 def add_2(x): return x + 2 def opp_on_item(item, func): return func(item) #main opp_on_item(12, mult_2) #result: 24 opp_on_item(12, add_2) #result: 14

Lambda Sometimes you need a simply arithmetic function Its silly to write a method for it, but redundant not too With lambda we can create quick simple functions Facts Lambda functions can only be comprised of a single expression No loops, no calling other methods Lambda functions can take any number of variables Syntax: lambda param1,…,paramn : expression

Lambda Syntax lambda.py 1 2 3 4 5 6 7 8 9 #Example 1 #Example 1 square_func = lambda x : x**2 square_func(4) #return: 16 #Example 2 close_enough = lambda x, y : abs(x – y) < 3 close_enough(2, 4) #return: True #Example 3 def get_func(n) : return lambda x : x * n + x % n my_func = get_func(13) my_func(4) #return: 56

operator Most of the built-in functions (len, +, *, <) can be accessed through the operator module Need to import the operator module from operator import * Operator Function - neg(x) + pos(x) Operator Function == eq(x,y) != ne(x, y) < lt(x, y) > gt(x, y) <= le(x, y) >= ge(x, y) Operator Function - sub(x, y) + add(x, y) * __mul__(self, other)

Partially Instantiated Functions We have seen that we can create lambda functions for quick functions on the go We have also seen that we can use the built in operators through the operator class What we would like to do is use the built in operators with a silly lambda function We can do this by partially instantiating function with the partial function from the functools package You supply some of the parameters and get a function back the needs the rest of the parameters in order to execute

partial partial.py 1 2 3 4 5 6 7 8 9 def mult1(x): return 2 * x def mult1(x): return 2 * x mult2 = lambda x : 2 * x mult3 = partial(mul, 2) x = 10 print(mult1(5)); #10 print(mult2(5)); #10 print(mult3(5)); #10

Higher-Order Functions A higher-order function is a function that takes another function as a parameter They are “higher-order” because it’s a function of a function Examples Map Reduce Filter Lambda works great as a parameter to higher-order functions if you can deal with its limitations

Transform Example Let’s write a method called transform that takes a list and a function as parameters and applies the function to each element of the list transform.py 1 2 3 4 5 6 7 def mult_2(x): return x * 2 ... #Main x = [1, 2, 3] transform(x, mult_2) print(x) #[2, 4, 6]

Transform Solution transform.py 1 2 3 4 5 6 7 8 9 def transform(arr, func): for i in range(len(arr)): arr[i] = func(arr[i]) x = [1, 2, 3] transform(x, mult_2) print(x) #[2, 4, 6]