CSC1018F: Functional Programming
-
Upload
leonard-whitehead -
Category
Documents
-
view
13 -
download
0
description
Transcript of CSC1018F: Functional Programming
CSC1018F:Functional Programming
Diving into Python Ch. 16
Lecture Outline
Recap of Regular Expressions [week 3]Functional Programming
The Zen of Functional ProgrammingFinding Path NamesFiltering and Mapping RevisitedRevision Exercise: Chess Notation
Recap of Regular Expressions
A powerful mechanism for identifying patterns in textBut need to be handled with care
An entirely new syntax is required
Verbose regular expressions include whitespace and comments
The verbose form is preferred
Introduction to Functional Programming
Data-centric thought patternFunctions are simply treated as another form of dataCan 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
Finding 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 scriptos.path.dirname(filename) - separates out and returns the path portion of filenameos.path.abspath(pathname) - expands a pathname from partial to full. Performs normalizationos.getcwd() - returns the current working directory
Filtering 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: >>> def odd(n):
... return n % 2
...
>>> li = range(10)
>>> filter(odd, li)
[1,3,5,7,9]
>>> [e for e in li if odd(e)]
Mapping 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: >>> def double(n):
... return n * 2
...
>>> li = range(10)
>>> map(double, li)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> [double(e) for e in li]
Example: Dynamically Importing Modules
__import__ - a built in function that takes a module name as a string and returns the moduleAllows us to load and access modules that are only named at run-timeExample:>>> moduleNames = [‘sys’, ‘os’, ‘re’]
>>> modules = map(__import__, moduleNames)
>>> modules[<module 'sys' (built-in)>, <module 'os' from '/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/os.pyc'>, <module 're' from '/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/re.pyc'>]
>>> modules[0].version'2.3.3 (#2, Dec 23 2003, 22:56:29) \n[GCC 3.1 20020420 (prerelease)]'
Revision 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 linesThe 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