UT Dallas CS 4337 - #Sebesta ch09A subprogram-closure - rev2 (24 pages)

Previewing pages 1, 2, 23, 24 of 24 page document View the full content.
View Full Document

#Sebesta ch09A subprogram-closure - rev2



Previewing pages 1, 2, 23, 24 of actual document.

View the full content.
View Full Document
View Full Document

#Sebesta ch09A subprogram-closure - rev2

33 views


Pages:
24
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Closures A closure is a subprogram and the referencing environment where it was defined The referencing environment is needed if the subprogram can be called from any arbitrary place in the program A static scoped language that does not permit nested subprograms doesn t need closures Closures are only needed if a subprogram can access variables in nesting scopes and it can be called from anywhere To support closures an implementation may need to provide unlimited extent to some variables because a subprogram may access a nonlocal variable that is normally no longer alive Copyright 2012 Addison Wesley All rights reserved 1 1 Closures continued A JavaScript closure function makeAdder x return function y return x y var add10 makeAdder 10 var add5 makeAdder 5 document write add 10 to 20 add10 20 br document write add 5 to 20 add5 20 br The closure is the anonymous function returned by makeAdder Copyright 2012 Addison Wesley All rights reserved 1 2 Closures continued C We can write the same closure in C using a nested anonymous delegate Func int int the return type specifies a delegate that takes an int as a parameter and returns and int static Func int int makeAdder int x return delegate int y return x y Func int int Add10 makeAdder 10 Func int int Add5 makeAdder 5 Console WriteLine Add 10 to 20 0 Add10 20 Console WriteLine Add 5 to 20 0 Add5 20 Copyright 2012 Addison Wesley All rights reserved 1 3 Python Python supports Lambda functions functions that might not be bound to a name You might also see them referred to as anonymous functions Lambda functions are more restrictive than other functions because they can hold only a single expression In its most basic form Lambda is another syntax for defining a function In the following example the object named a is a Lambda function and performs the same task as the function named add one def add one x return x 1 type add one type function Python def add one x return x 1 type add one type function add one 2 3 a lambda type a type function a 2 3 x x 1 def add one x return x 1 Python map You can use the Lambda syntax to define a function inline as an argument to a function such as map that expects another function as an argument The syntax of the map function is map func seq1 seq2 where func is a function that is applied to the sequence of arguments represented by seq1 and seq2 Typically the sequences that are arguments to map and the object returned by map are lists Python The next example first defines a function named times two def times two x return x 2 times two 8 16 Next the map function applies times two to a list map times two 1 2 3 4 2 4 6 8 Python def times two x return x 2 times two 8 16 Next the map function applies times two to a list map times two 1 2 3 4 2 4 6 8 You can define an inline Lambda function as an argument to map In this example the Lambda function is not bound to a name map lambda x x 2 1 2 3 4 2 4 6 8 Python List Comprehensions List comprehensions apply functions to lists For example the following code which does not use a list comprehension uses for to iterate over items in a list my list for x in range 10 my list append x 10 my list 10 11 12 13 14 15 16 17 18 19 Python You can use a list comprehension to perform the same task neatly and efficiently The syntax is similar but a list comprehension is enclosed within square brackets and the operation x 10 precedes the iteration for x in range 10 my list x 10 for x in range 10 my list 10 11 12 13 14 15 16 17 18 19 The results when using a for structure and a list comprehension are the same Python The next example uses a list comprehension to fill a list with powers of 2 potwo 2 x for x in range 1 13 print potwo 2 4 8 16 32 64 128 256 512 1024 2048 4096 The next list comprehension fills a list with even numbers The if clause returns values only if the remainder after dividing a number by 2 is 0 if x 2 0 x for x in range 1 11 if x 2 0 2 4 6 8 10 Python The final example shows nested list comprehensions It nests for loops and uses x y to catenate the elements of both lists in all combinations A a b c B 1 2 3 all x y for x in A for y in B print all a1 a2 a3 b1 b2 b3 c1 c2 c3 Python The final example shows nested list comprehensions It nests for loops and uses x y to catenate the elements of both lists in all combinations A a b c B 1 2 3 all x y for x in A for y in B print all a1 a2 a3 b1 b2 b3 c1 c2 c3 Copyright 2012 Addison Wesley All rights reserved 1 14 First Second Third class object Sebesta 9 9 What are 1 first class value 2 second class value and 3 third class value What languages support them first class second class third class for subroutine subprogram Copyright 2012 Addison Wesley All rights reserved 1 15 First Second Third class object In general a value in a programming language is said to have first class status if it can be passed as a parameter returned from a subroutine or assigned into a variable Simple types such as integers and characters are first class values in most programming languages Copyright 2012 Addison Wesley All rights reserved 1 16 First Second Third class object A second class value can be passed as a parameter but not returned from a subroutine or assigned into a variable and A third class value cannot be passed as a parameter Labels are third class values in most programming languages as it cannot be passed as a parameter Copyright 2012 Addison Wesley All rights reserved 1 17 First Second Third class object Subroutines display most variation They are first class values in all functional programming languages and most scripting languages They are also first class values in C and with some restrictions in several other imperative languages including Fortran Modula 2 and 3 Ada 95 C and C They are second class values in most other imperative languages and third class values in Ada 83 Copyright 2012 Addison Wesley All rights reserved 1 18 First Second Third class object First class subroutines in a language with nested scopes introduce an additional level of complexity That is they raise the possibility that a reference to a subroutine may outlive the execution of the scope in which that routine was declared Copyright 2012 Addison Wesley All rights reserved 1 19 First Second …


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view #Sebesta ch09A subprogram-closure - rev2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view #Sebesta ch09A subprogram-closure - rev2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?