F21SC Industrial Programming: Functional Programming in Python

Similar documents
A case study of delegates and generics in C#

Lecture 21: Functional Programming in Python. List Comprehensions

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Python: Functions and Generators. Giuseppe Attardi

Functional Programming and Haskell

Iterators & Generators

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

ITERATORS AND STREAMS 9

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Haskell: yet another Functional Language. Distributed and Parallel Technology

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Overloading. F21SC Industrial Programming: Python: Advanced Language Features. Overloading. Overloading arithmetic operations

Class definition. F21SC Industrial Programming: Python. Post-facto setting of class attributes. Class attributes

F21SC Industrial Programming: Python: Classes and Exceptions

Functional Programming for Imperative Programmers

SD314 Outils pour le Big Data

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

CSCE 314 Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

Types and Static Type Checking (Introducing Micro-Haskell)

Shell CSCE 314 TAMU. Higher Order Functions

Introduction to Functional Programming (Python) John R. Woodward

An introduction to functional programming. July 23, 2010

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Overloading. F21SC Industrial Programming: Python: Advanced Language Features. Overloading. Overloading arithmetic operations

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

G Programming Languages - Fall 2012

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

Types and Static Type Checking (Introducing Micro-Haskell)

F28HS Hardware-Software Interface: Systems Programming

Functional Programming in C++

Motivation. Reflection in C# Case study: Implicit Serialisation. Using implicit serialisation. Hans-Wolfgang Loidl

Parallel Haskell on MultiCores and Clusters

F21SC Industrial Programming: Python: Python Libraries

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

CSCE 314 Programming Languages

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Topic 8: Lazy Evaluation

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Concepts of Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

Algorithmic Thinking: Computing with Lists

Functional Languages. Hwansoo Han

Shell CSCE 314 TAMU. Haskell Functions

CSCC24 Functional Programming Scheme Part 2

Organization of Programming Languages CS3200/5200N. Lecture 11

functional programming in Python, part 2

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

CSE : Python Programming

Imperative languages

F28HS Hardware-Software Interface: Systems Programming

Computer Science 1001.py. Lecture 11: Recursion and Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

F21SC Industrial Programming: Python: Advanced Language Features

CS 242. Fundamentals. Reading: See last slide

Functional Programming

Shell CSCE 314 TAMU. Functions continued

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction to Functional Programming in Haskell 1 / 56

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

CS 11 Haskell track: lecture 1

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Advanced topics, part 2

hwu-logo.png 1 class Rational { 2 int numerator ; int denominator ; 4 public Rational ( int numerator, int denominator ) {

Programming Languages Third Edition

3. Functional Programming. Oscar Nierstrasz

Graphical Untyped Lambda Calculus Interactive Interpreter

61A Lecture 32. Wednesday, November 14

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Code Contracts in C#

INTRODUCTION TO HASKELL

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS 457/557: Functional Languages

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

CIS192: Python Programming

Option Values, Arrays, Sequences, and Lazy Evaluation

A Brief Introduction to Scheme (I)

python 01 September 16, 2016

Streams and Evalutation Strategies

Part I Basic Concepts 1

15. Functional Programming

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

C# Data Manipulation

Functional Programming

To figure this out we need a more precise understanding of how ML works

User-defined Functions. Conditional Expressions in Scheme

Functional Programming Concepts for Data Processing

April 2 to April 4, 2018

F28HS Hardware-Software Interface: Systems Programming

CircuitPython 101: Working with Lists, Iterators and Generators

A general introduction to Functional Programming using Haskell

Extending Jython. with SIM, SPARQL and SQL

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

C# Data Manipulation

Transcription:

F21SC Industrial Programming: Functional Programming in Python Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 1 2017/18 0 No proprietary software has been used in producing these slides Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 1 / 20

What is a functional language? The distinctive feature of pure functional languages is their referential transparency. Definition (Stoy, 1977) The only thing that matters about an expression is its value, and any subexpression can be replaced by any other equal in value. Moreover, the value of an expression is, within certain limits, the same wherever it occurs. Implications: Two expressions are equal if they have the same value, e.g. sin(6) = sin(1+5). Value-based equality enables equational reasoning, where one expression is substituted by another of equal value, e.g. f(x) + f(x) = 2*f(x) Scope matters: if x = 6, then sin(x) = sin(6) = sin(1+5) Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 2 / 20

Properties of functional languages Computations in functional languages are free of side effects. See above: these languages are referentially transparent. Computations in functional languages are state-less. This excludes the almost ubiquitous pattern in imperative languages of assigning first one, then another value to the same variable to track the program state. Functions are first class (objects). Everything you can do with data can be done with functions themselves (such as passing a function to another function). Recursion is used as a primary control structure. In some languages, no other loop construct exists. Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 3 / 20

Properties of functional languages (cont d) There is a focus on list processing. Lists are often used with recursion on sublists as a substitute for loops. Functional programming either discourages or outright disallows statements, and instead works with the evaluation of expressions (in other words, functions plus arguments). In the pure case, one program is one expression (plus supporting definitions). Functional programming focusses on what is to be computed rather than how it is to be computed. Much functional programming utilises higher order functions (in other words, functions that operate on functions that operate on functions). Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 4 / 20

Functional languages are free of state Goal: Create a list of all square values of some integer numbers. Imperative solution 1 : def square(x): return x*x input = [1, 2, 3, 4] output = [] for v in input: output.append(square(v)) NB: the contents of the list output changes as you iterate over input; to understand the program, you have to follow the control-flow 1 From https://marcobonzanini.com/2015/06/08/functional-programming-in-python/ Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 5 / 20

Functional languages are free of state (cont d) Functional solution 2 : def square(x): return x*x input = [1, 2, 3, 4] output = map(square, input) NB: in this version, there is no internal state; the result list is defined in one go (bulk operation); you only need to understand the operation on each element. 2 From https://marcobonzanini.com/2015/06/08/functional-programming-in-python/ Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 6 / 20

Functionally inspired constructs in Python 1 List comprehensions 2 Set and dictionary comprehensions 3 Libraries of higher-order functions (map, reduce, zip) 4 Iterators (also called lazy data structures ) Material from Functional Programming in Python, by David Mertz, O Reilly Media, 2015. Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 7 / 20

Functionally inspired constructs in Python 1 List comprehensions 2 Set and dictionary comprehensions 3 Libraries of higher-order functions (map, reduce, zip) 4 Iterators (also called lazy data structures ) Material from Functional Programming in Python, by David Mertz, O Reilly Media, 2015. Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 7 / 20

List comprehensions These change the way how you think about the data-structure: focus on what the collection is, rather than how it is constructed. combs = [(x, y) for x in [1,2,3] for y in [1,2,3] if x!= y] This is equivalent to this more verbose code combs = [] for x in [1,2,3]: for y in [1,2,3]: if x!= y: combs.append((x, y)) Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 8 / 20

List comprehensions These change the way how you think about the data-structure: focus on what the collection is, rather than how it is constructed. combs = [(x, y) for x in [1,2,3] for y in [1,2,3] if x!= y] This is equivalent to this more verbose code combs = [] for x in [1,2,3]: for y in [1,2,3]: if x!= y: combs.append((x, y)) Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 8 / 20

Generator comprehensions These describe how to get the data, that is not realised until one explicitly asks for it. They implement lazy data structures. with open(file,"r") as f: xs = (line for line in f if re.search( "event_type":"read", line)) NB: in this example, f is a generator, iterating over the file; you can t directly ask for its length 2 See this sample source code Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 9 / 20

Generator comprehensions (cont d) A traditional, more verbose, version: def proc_file(file): """Find read events in an issuu log file.""" f = open(file,"r") for line in f: if re.search( "event_type":"read", line): yield line f.close() Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 10 / 20

Set and dictionary comprehensions In the same way that list comprehensions are used to define a list in a bulk operation, set or dictionary comprehensions can be used to define sets or dictionaries in one construct. { i:chr(48+i) for i in range(10) } This defines a dictionary, mapping digits (0 9) to their character codes. Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 11 / 20

Libraries of higher-order functions The most common higher-order functions are 3 : map: perform the same operation over every element of a list filter: selects elements from a list, to form a new list reduce (in module functools): do a pair-wise combination over all elements of a list zip: takes one element from each iterable and returns them in a tuple any: checks whether any element of a list fulfills a given predicate all: checks whether all elements of a list fulfills a given predicate takewhile: returns elements for as long as the predicate is true dropwhile: discards elements while the predicate is true groupby: collects all the consecutive elements from the underlying iterable that have the same key value NB: in Python 3.x, all these functions are iterators; therefore, usage is different from Python 2.x (see the examples on the next slides) 3 See https://docs.python.org/3.4/howto/functional.html Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 12 / 20

Libraries of higher-order functions filter(test, sequence) returns a sequence, whose elements are those of sequence that fulfill the predicate test. E.g. filter(lambda x: x % 2 == 0, range(10)) map(f, sequence) applies the function f to every element of sequence and returns it as a new sequence. map(lambda x: x*x*x, range(10)) map(lambda x,y: x+y, range(1,51), range(100,50,-1)) reduce(f, [a1,a2,a3,...,an]) computes f(...f(f(a1,a2),a3),...,an) reduce(lambda x,y:x*y, range(1,11)) reduce(f, [a1,a2,...,an], e) computes f(...f(f(e,a1),a2),...,an) Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 13 / 20

More higher-order examples Demo 3 See this sample source code Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 14 / 20

Recursion vs. Iteration The following two versions of factorial are equivalent: def factorialr(n): "Recursive factorial function" assert (isinstance(n, int) and n >= 1) return 1 if n <= 1 else n * factorialr(n-1) def factoriali(n): "Iterative factorial function" assert (isinstance(n, int) and n >= 1) product = 1 while n >= 1: product *= n n -= 1 return product Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 15 / 20

Recursion vs. Iteration As a footnote, the fastest version I know of for factorial() in Python is in a functional programming style, and also expresses the what of the algorithm well once some higher-order functions are familiar: from functools import reduce from operator import mul def factorialhof(n): return reduce(mul, range(1, n+1), 1) Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 16 / 20

Recursion vs. Iteration For example, the quicksort algorithm is very elegantly expressed without any state variables or loops, but wholly through recursion: def quicksort(lst): "Quicksort over a list-like sequence" if len(lst) == 0: return lst pivot = lst[0] pivots = [x for x in lst if x == pivot] small = quicksort([x for x in lst if x < pivot]) large = quicksort([x for x in lst if x > pivot]) return small + pivots + large Some names are used in the function body to hold convenient values, but they are never mutated Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 17 / 20

The concept of lazy data structures We typically think of a data structure as fully expanded data In most cases, this matches the representation of data in memory However, sometimes we do not want fully expanded data, but still use it as a normal data structure E.g. when working with large data sets, we just want to iterate over the data A data structure that is only expanded if and when it is used, is a lazy data structure Python s generators (and to some degree iterators) allow to use lazy data structures In Python 3, iterators are the preferred mechanism for constructing data structures Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 18 / 20

Simple lazy Sieve of Eratosthenes def get_primes(): "Simple lazy Sieve of Eratosthenes" candidate = 2 found = [] while True: if all(candidate % prime!= 0 for prime in found): yield candidate found.append(candidate) candidate += 1 primes = get_primes() # print(next(primes), next(primes), next(primes)) # (2, 3, 5) for _, prime in zip(range(10), primes): print(prime, end=" ") Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 19 / 20

Summary Python borrows many of its advanced language features from functional languages List and generator comprehensions, for concise, bulk operations on data structures Higher-order functions to encode commonly occuring compute structures Generators (and iterators) to get the benefits of lazy data structures Hans-Wolfgang Loidl (Heriot-Watt Univ) Python 2017/18 20 / 20