Functional Programming Facilities in Python - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Functional Programming Facilities in Python

Description:

python interpreter is in /usr/bin (Unix path) # denotes a comment ... usr/bin/python. print range(1,10,1) # start, stop, step. print range(4,0,-1) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 7
Provided by: KenHa98
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Facilities in Python


1
Functional Programming Facilities in Python
  • Supplementary Lecture
  • K.A.Hawick, April 2008

2
  • !/usr/bin/python
  • print "A Python Example showing lists and map"
  • mylist 1, 2, 3, 4, 5
  • print mylist
  • def func(arg)
  • return arg arg
  • print "first in imperative style using
    'foreach'"
  • for e in mylist
  • print func(e)
  • print "and again using functional style"
  • print map( func, mylist )
  • and now show off use of filter
  • def test( arg )
  • python interpreter is in /usr/bin (Unix path)
  • denotes a comment
  • python is dynamic so can make lists on the fly
  • note the two space indents for eg function
    definitions
  • foreach for in syntax
  • Output
  • A Python Example showing lists and map
  • 1, 2, 3, 4, 5
  • first in imperative style using 'foreach'
  • 1
  • 4
  • 9
  • 16
  • 25
  • and again using functional style
  • 1, 4, 9, 16, 25
  • 1, 4, 9

3
  • functions are first class entities
  • We can have lists of functions
  • Note lambda function (a way to make an anonymous
    function)
  • Output
  • !/usr/bin/python
  • def func1( arg )
  • return arg 1
  • def func2( arg )
  • return arg 2
  • def func3( arg )
  • return arg arg arg
  • funclist func1, func2, func3, lambda xxxxx
  • list 1, 2
  • for f in funclist
  • print map( f, list )

2, 3 2, 4 1, 8 1, 16

4
  • !/usr/bin/python
  • print range(1,10,1) start, stop, step
  • print range(4,0,-1)
  • print reduce( lambda n,mnm, range(4,0,-1) )
  • print reduce( lambda n,mnm, range(4,0,-1) )
  • print "-------------------------------"
  • mylist 1, 2, 3 4, 5 list
    concatenation
  • mylist.sort()
  • mylist.reverse()
  • mylist.pop()
  • mylist.append( 42)
  • print mylist
  • print mylist.index(3)
  • range generates a numerical list
  • Note it does not include the stop value
  • reduce function does cumulative reduction
  • ( ( (4 3) 2 ) 1 )
  • Also various list methods available
  • Output
  • 1, 2, 3, 4, 5, 6, 7, 8, 9
  • 4, 3, 2, 1
  • 24
  • 10
  • -------------------------------
  • 5, 4, 3, 2, 42
  • 2
  • 4

5
  • Can map functions to functions
  • Need the lambda expression here to make a
    function call from the identifier argument
  • Output
  • calling funcA
  • calling funcB
  • calling funcC
  • !/usr/bin/python
  • def funcA()
  • print "calling funcA"
  • def funcB()
  • print "calling funcB"
  • def funcC()
  • print "calling funcC"
  • map( lambda f f(), funcA, funcB, funcC )

6
So What is Python?
  • python is (mostly) an imperative/OO language
  • but it has some useful functional facilities
  • hybrid languages that let you use the best of
    both worlds are likely to become common
  • (incidentally, in python lists are mutable,
    tuples are almost like lists but are immutable)
Write a Comment
User Comments (0)
About PowerShow.com