CSC1018F: Functional Programming - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CSC1018F: Functional Programming

Description:

Revision Exercise: Chess Notation. Recap of Regular Expressions ... Do this using regular expressions, maps and filters, where appropriate ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 10
Provided by: daven2
Category:

less

Transcript and Presenter's Notes

Title: CSC1018F: Functional Programming


1
CSC1018FFunctional Programming
  • Diving into Python Ch. 16

2
Lecture 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

3
Recap 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

4
Introduction 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

5
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 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

6
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

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)
7
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

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
8
Example 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)'
9
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 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
Write a Comment
User Comments (0)
About PowerShow.com