functions are also data! other functions as input!

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Testing Michael Ernst CSE 140 University of Washington.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
I Power Int 2 Computing Software Development High Level Language Constructs.
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Fundamentals of Programming I Higher-Order Functions
CS314 – Section 5 Recitation 10
Higher Order Functions
COMPSCI 107 Computer Science Fundamentals
Types CSCE 314 Spring 2016.
Introduction to Higher Order (Functional Programming) (Python) part 2
Java for Beginners University Greenwich Computing At School DASCO
Python Loops and Iteration
Introduction to Python
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
Containers and Lists CIS 40 – Introduction to Programming in Python
Chapter 10 Lists.
The Strategy Pattern II: Emulating Higher-Order Functions
Java Review: Reference Types
6.001 SICP Data abstractions
CHAPTER FOUR Functions.
Lists in Python.
Introduction to Python
Functions As Objects.
Chapter 10 Lists.
CS190/295 Programming in Python for Life Sciences: Lecture 6
CISC101 Reminders Assn 3 due tomorrow, 7pm.
PROGRAMMING IN HASKELL
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSC1018F: Intermediate Python
More Looping Structures
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Building Java Programs
Lambda Functions, MapReduce and List Comprehensions
CS150 Introduction to Computer Science 1
15-110: Principles of Computing
CS150 Introduction to Computer Science 1
COMPUTER PROGRAMMING SKILLS
Java: Variables, Input and Arrays
Programming Languages and Paradigms
More Looping Structures
CISC101 Reminders Assignment 3 due today.
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Reasoning with Types.
Lecture 2 - Names & Functions
Presentation transcript:

functions are also data! other functions as input! Remembering Data x = 41 name: x type: int 41 name: tempF type: float 32.0 tempF = 32.0 def dbl(x): return 2*x name: dbl type: function we’ll see an important Big Idea: functions as “first-class” objects, or data values, meaning that they can be stored in variables, passed as arguments to functions just like any other object, and created dynamically like other values. This is unfortunately not well-realized in Java (you can create functions that accept other functions as input but it’s very cumbersome) Just like numbers, strings, and lists, functions are also data! Therefore, functions can take other functions as input! Functions that take other functions as input or return functions are called higher order functions

def dbl(x): return 2*x myList = [1, 2, dbl] canWeDouble = myList[2] canWeDouble(12) >>> 24 >>> myList[2](3) # or simply >>> 6

Mapping Map – The application of one specific operation to each element in a list Example: suppose we wanted to double the value of every number in a list, and output the new list? >>> dblList([1,2,3,4,5]) [2,4,6,8,10] Sure we can do it in list comprehension. But mapping makes it more genral.

A general approach… What if we could apply any arbitrary function to each element in a list to produce a new list? [1, 2, 3, 4, 5] def dbl(x): return 2*x dbl(1) dbl(2) dbl(3) dbl(4) dbl(5) [2, 4, 6, 8, 10]

map! map(f,t) – A built-in function that applies any arbitrary function f to every element in t t is any iterable object, list is an example def dbl(x): return 2*x lst = [1,2,3,4,5] newLst = map(dbl,lst) Point out that lists and strings are iterable objects Why? map() returns an iterable object.

Map examples def dbl(x): return 2*x >>> list( map(dbl, [0,1,2,3,4,5]) ) [0, 2, 4, 6, 8, 10] >>> list( map(dbl, 'test') ) ['tt', 'ee', 'ss', 'tt'] def square(x): return x**2 >>> list( map(square, range(6)) ) [0, 1, 4, 9, 16, 25] def isA(x): return x == 'a' 'test' – I expect very few to get this! Give them hints about the type of each indexed letter in a string. range(6) - Point out that range defaults to a start of 0 if only one number is specified >>> list( map(isA, 'go away!') ) [False, False, False, True, False, True, False, False]

Map ! Without map With map! def dblList(lst): if lst == []: return lst else: return [lst[0]*2] + dblList(lst[1:]) Without map def dbl(x): return x*2 def dblList(lst): return list( map(dbl, lst) ) With map!

Map v. Lists? map( dbl, range(99)) vs. [ dbl(num) for num in range(99) ]

List comprehension computes as listed. Map v. Lists? map( dbl, range(9999999999999)) vs. [ dbl(num) for num in range(999999999999999) ] Scalability! Map binds (connects) function to data, it doesn’t generate the list until referenced. List comprehension computes as listed.

Reducing lists reduce(f,t) – Applies f (a function of two arguments) cumulatively to the items of t applied from left to right, so as to reduce the sequence to a single value NOT a built-in function! Available in functools module Example: reduce(add, [1,2,3,4]) does: add(add(add(1,2),3),4) def add(x,y): return x + y The process is 1+2, then 3+3, then 6+4, then 10+5. from functools import reduce >>> reduce(add, [1,2,3,4,5]) NOTE: f must return the same type! Why? 15

Filter filter(f,t) – constructs a list from those elements of t for which f returns True applied from left to right Example: def is_vowel(x): return x in ‘aeiou’ >>> x = filter(is_vowel, ‘hello world’) >>> list(x) >>> [‘e’, ‘o’, ‘o’]

Computations == Transformations data new data Common transformations - found in many programming languages. Map - apply same action to every element in sequence. [2, 7, 6, 4] [4, 14, 12, 8] (Remember: lists and strings are sequences.) double Filter - select certain items in a sequence by a predicate. (A predicate is a function that returns True or False.) MapReduce isEven [3, 2, 13, 17, 6] [2, 6] Reduce - apply the same action between elements of a sequence. reduce(add, [2, 3, 7, 4]) == (((2+3)+7)+4) == 16