First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Graph Coverage for Design Elements 1.  Use of data abstraction and object oriented software has increased importance on modularity and reuse.  Therefore.
Lists Introduction to Computing Science and Programming I.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Eight compound procedures and higher-order procedures.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python Control of Flow.
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Higher-Order Procedures (in Ruby) based on ‘Structure and Interpretation of Computer Programs’ (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
By the end of this session you should be able to...
I Power Int 2 Computing Software Development High Level Language Constructs.
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
CS 177 Week 4 Recitation Slides Variables, Files and Functions.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Variables, Expressions, and Statements
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
More Functions with Return Values CS303E: Elements of Computers and Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
I Power Higher Computing Software Development High Level Language Constructs.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Lists Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Programming Model CS 333 Sam Houston State University Dr. Tim McGuire.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Chapter 2 Writing Simple Programs
String and Lists Dr. José M. Reyes Álamo.
Python Aliasing Copyright © Software Carpentry 2010
Computer Programming Fundamentals
Program Design Invasion Percolation: Testing
IDLE Hints To re-load a module after making changes:
Lists in Python.
String and Lists Dr. José M. Reyes Álamo.
Recap Week 2 and 3.
References.
Program Design Invasion Percolation: The Grid
Names of variables, functions, classes
Lecture 3 - Instruction Set - Al
Presentation transcript:

First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Python

First-Class Functions An integer is 32 bits of data...

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to A string is a sequence of bytes representing characters...

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to A string is a sequence of bytes representing characters......that variables can refer to

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to A string is a sequence of bytes representing characters......that variables can refer to A function is a sequence of bytes representing instructions...

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to A string is a sequence of bytes representing characters......that variables can refer to A function is a sequence of bytes representing instructions......and yes, variables can refer to them to

PythonFirst-Class Functions An integer is 32 bits of data......that variables can refer to A string is a sequence of bytes representing characters......that variables can refer to A function is a sequence of bytes representing instructions......and yes, variables can refer to them to This turns out to be very useful, and very powerful

PythonFirst-Class Functions What happens when a function is defined

PythonFirst-Class Functions What happens when a function is defined def threshold(signal): return 1.0 / sum(signal)

PythonFirst-Class Functions What happens when a function is defined def threshold(signal): return 1.0 / sum(signal) stackvalue threshold global

PythonFirst-Class Functions What happens when a function is defined def threshold(signal): return 1.0 / sum(signal) stackvalue name threshold global Not really very different from: name = 'Alan Turing' 'alan turing'

PythonFirst-Class Functions Can assign that value to another variable

PythonFirst-Class Functions Can assign that value to another variable def threshold(signal): return 1.0 / sum(signal) stackvalue threshold global

PythonFirst-Class Functions Can assign that value to another variable def threshold(signal): return 1.0 / sum(signal) t = threshold stackvalue t threshold global

PythonFirst-Class Functions Can assign that value to another variable def threshold(signal): return 1.0 / sum(signal) t = threshold print t([0.1, 0.4, 0.2]) stackvalue t threshold global

PythonFirst-Class Functions Can put (a reference to) the function in a list

PythonFirst-Class Functions Can put (a reference to) the function in a list def area(r): return PI * r * r def circumference(r): return 2 * PI * r stackvalue area circumference global

PythonFirst-Class Functions Can put (a reference to) the function in a list def area(r): return PI * r * r def circumference(r): return 2 * PI * r funcs = [area, circumference] stackvalue funcs area circumference global

PythonFirst-Class Functions Can put (a reference to) the function in a list def area(r): return PI * r * r def circumference(r): return 2 * PI * r funcs = [area, circumference] for f in funcs: print f(1.0) stackvalue funcs area circumference global

PythonFirst-Class Functions Can put (a reference to) the function in a list def area(r): return PI * r * r def circumference(r): return 2 * PI * r funcs = [area, circumference] for f in funcs: print f(1.0) stackvalue funcs area circumference global

PythonFirst-Class Functions Can pass (a reference to) the function into a function

PythonFirst-Class Functions def call_it(func, value): return func(value) Can pass (a reference to) the function into a function

PythonFirst-Class Functions def call_it(func, value): return func(value) print call_it(area, 1.0) Can pass (a reference to) the function into a function

PythonFirst-Class Functions def call_it(func, value): return func(value) print call_it(area, 1.0) print call_it(circumference, 1.0) Can pass (a reference to) the function into a function

PythonFirst-Class Functions Can now write functions of functions

PythonFirst-Class Functions Can now write functions of functions def do_all(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result

PythonFirst-Class Functions def do_all(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result Can now write functions of functions print do_all(area, [1.0, 2.0, 3.0])

PythonFirst-Class Functions def do_all(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result Can now write functions of functions print do_all(area, [1.0, 2.0, 3.0]) [ , , ]

PythonFirst-Class Functions def do_all(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result Can now write functions of functions print do_all(area, [1.0, 2.0, 3.0]) [ , , ] def slim(text): return text[1:-1]

PythonFirst-Class Functions def do_all(func, values): result = [] for v in values: temp = func(v) result.append(temp) return result Can now write functions of functions print do_all(area, [1.0, 2.0, 3.0]) [ , , ] def slim(text): return text[1:-1] print do_all(slim, ['abc', 'defgh']) b efg

PythonFirst-Class Functions Higher-order functions allow re-use of control flow

PythonFirst-Class Functions def combine_values(func, values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current Higher-order functions allow re-use of control flow

PythonFirst-Class Functions def combine_values(func, values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current def add(x, y): return x + y def mul(x, y): return x * y Higher-order functions allow re-use of control flow

PythonFirst-Class Functions def combine_values(func, values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current def add(x, y): return x + y def mul(x, y): return x * y print combine_values(add, [1, 3, 5]) 9 Higher-order functions allow re-use of control flow

PythonFirst-Class Functions def combine_values(func, values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current def add(x, y): return x + y def mul(x, y): return x * y print combine_values(add, [1, 3, 5]) 9 print combine_values(mul, [1, 3, 5]) 15 Higher-order functions allow re-use of control flow

PythonFirst-Class Functions Without higher order functions

PythonFirst-Class Functions Without higher order functions op_1op_2op_3 data_structure_Ado_1Ado_2Ado_3A data_structure_Bdo_1Bdo_2Bdo_3B data_structure_Cdo_1Cdo_2Cdo_3C

PythonFirst-Class Functions Without higher order functions op_1op_2op_3 data_structure_Ado_1Ado_2Ado_3A data_structure_Bdo_1Bdo_2Bdo_3B data_structure_Cdo_1Cdo_2Cdo_3C total: 9

PythonFirst-Class Functions Without higher order functions op_1op_2op_3 data_structure_Ado_1Ado_2Ado_3A data_structure_Bdo_1Bdo_2Bdo_3B data_structure_Cdo_1Cdo_2Cdo_3C With higher order functions total: 9

PythonFirst-Class Functions Without higher order functions op_1op_2op_3 data_structure_Ado_1Ado_2Ado_3A data_structure_Bdo_1Bdo_2Bdo_3B data_structure_Cdo_1Cdo_2Cdo_3C With higher order functions op_1op_2op_3 operate_on_A operate_on_B operate_on_C total: 9

PythonFirst-Class Functions Without higher order functions op_1op_2op_3 data_structure_Ado_1Ado_2Ado_3A data_structure_Bdo_1Bdo_2Bdo_3B data_structure_Cdo_1Cdo_2Cdo_3C With higher order functions op_1op_2op_3 operate_on_A operate_on_B operate_on_C total: 9 total: 6

PythonFirst-Class Functions Must need to know something about the function in order to call it

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments def add_all(*args): total = 0 for a in args: total += a return total

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments def add_all(*args): total = 0 for a in args: total += a return total

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments def add_all(*args): total = 0 for a in args: total += a return total print add_all() 0

PythonFirst-Class Functions Must need to know something about the function in order to call it Like number of arguments def add_all(*args): total = 0 for a in args: total += a return total print add_all() 0 print add_all(1, 2, 3) 6

PythonFirst-Class Functions Combine with "regular" parameters

PythonFirst-Class Functions def combine_values(func, *values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current Combine with "regular" parameters

PythonFirst-Class Functions def combine_values(func, *values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current Combine with "regular" parameters

PythonFirst-Class Functions def combine_values(func, *values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current print combine_values(add, 1, 3, 5) 9 Combine with "regular" parameters

PythonFirst-Class Functions def combine_values(func, *values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current print combine_values(add, 1, 3, 5) 9 Combine with "regular" parameters What does combine_values(add) do?

PythonFirst-Class Functions def combine_values(func, *values): current = values[0] for i in range(1, len(values)): current = func(current, v) return current print combine_values(add, 1, 3, 5) 9 Combine with "regular" parameters What does combine_values(add) do? What should it do?

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True map(F, S)apply F to each element of S

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True map(F, S) apply F to each element of S reduce(F, S) use F to combine all elements of S

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True map(F, S) apply F to each element of S reduce(F, S) use F to combine all elements of S def positive(x): return x >= 0 print filter(positive, [-3, -2, 0, 1, 2]) [0, 1, 2]

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True map(F, S) apply F to each element of S reduce(F, S) use F to combine all elements of S def positive(x): return x >= 0 print filter(positive, [-3, -2, 0, 1, 2]) [0, 1, 2] def negate(x): return -x print map(negate, [-3, -2, 0, 1, 2]) [3, 2, 0, -1, -2]

PythonFirst-Class Functions filter(F, S) select elements of S for which F is True map(F, S) apply F to each element of S reduce(F, S) use F to combine all elements of S def positive(x): return x >= 0 print filter(positive, [-3, -2, 0, 1, 2]) [0, 1, 2] def negate(x): return -x print map(negate, [-3, -2, 0, 1, 2]) [3, 2, 0, -1, -2] def add(x, y): return x+y print reduce(add, [-3, -2, 0, 1, 2]) -2

PythonFirst-Class Functions What is programming?

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions figure out what the pattern is

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions figure out what the pattern is write it down as clearly as possible

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions figure out what the pattern is write it down as clearly as possible build more patterns on top of it

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions figure out what the pattern is write it down as clearly as possible build more patterns on top of it But limits on short-term memory still apply

PythonFirst-Class Functions What is programming? Novice:writing instructions for the computer Expert:creating and combining abstractions figure out what the pattern is write it down as clearly as possible build more patterns on top of it But limits on short-term memory still apply Hard to understand what meta-meta-functions actually do

October 2010 created by Greg Wilson Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.