Title: CSC1018F: Functional Programming
1CSC1018FFunctional Programming
- Diving into Python Ch. 16
2Lecture Outline
- Recap of Regular Expressions week 3
- Functional Programming
- The Zen of Functional Programming
- Finding Path Names
- Filtering and Mapping Revisited
- Revision Exercise Chess Notation
3Recap of Regular Expressions
- A powerful mechanism for identifying patterns in
text - But need to be handled with care
- An entirely new syntax is required
- Verbose regular expressions include whitespace
and comments - The verbose form is preferred
4Introduction to Functional Programming
- Data-centric thought pattern
- Functions are simply treated as another form of
data - Can be operated on and passed as arguments!
- Enables higher level logic and avoid busywork
coding - A philosophy of focusing on the data and how it
needs to be transformed
5Finding Path Names
- Functions useful in finding the name and path of
the currently executing script - sys.argv - a list with the name and command line
arguments of the script - os.path.dirname(filename) - separates out and
returns the path portion of filename - os.path.abspath(pathname) - expands a pathname
from partial to full. Performs normalization - os.getcwd() - returns the current working
directory
6Filtering Lists Differently
- Pass a function and a list to a filter. List
elements are passed to the function which returns
True for elements in the final filtered list - Syntax filter(fn, li)
- fn takes a single argument and evaluates to true
or false - Example
gtgtgt def odd(n) ... return n 2 ... gtgtgt li
range(10) gtgtgt filter(odd, li) 1,3,5,7,9 gtgtgt e
for e in li if odd(e)
7Mapping Lists Differently
- Pass a function and a list to a map. List
elements are passed through the function - Syntax map(fn, li)
- fn takes a single argument, operates on it and
returns a new version - Example
gtgtgt def double(n) ... return n 2 ... gtgtgt li
range(10) gtgtgt map(double, li) 0, 2, 4, 6, 8,
10, 12, 14, 16, 18 gtgtgt double(e) for e in li
8Example Dynamically Importing Modules
- __import__ - a built in function that takes a
module name as a string and returns the module - Allows us to load and access modules that are
only named at run-time - Example
gtgtgt moduleNames sys, os, re gtgtgt
modules map(__import__, moduleNames) gtgtgt
modules ltmodule 'sys' (built-in)gt, ltmodule 'os'
from '/Library/Frameworks/Python.framework/Version
s/2.3/lib/python2.3/os.pyc'gt, ltmodule 're' from
'/Library/Frameworks/Python.framework/Versions/2.3
/lib/python2.3/re.pyc'gt gtgtgt modules0.version '2
.3.3 (2, Dec 23 2003, 225629) \nGCC 3.1
20020420 (prerelease)'
9Revision Exercise
- You are tasked with coding an anonymization
program. This replaces names with initials in
order to hide identity - To begin, read in a list of names from a file
idprotect.txt, which contains names in the
form Firstname and also Firstname Surname on
separate lines - The main text should be read in from
plaintext.txt and all occurrences of idprotect
names replaced with their corresponding initial.
Thus John becomes J. and John Smith becomes
J. S. The final anonymous text should be output
to the file anontext.txt - Do this using regular expressions, maps and
filters, where appropriate