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

25 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



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?