DOC PREVIEW
UT Dallas CS 4337 - #Sebesta ch09A subprogram-closure - rev2

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ClosuresClosures (continued)Slide 3PythonSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13PowerPoint PresentationFirst, Second, Third class objectSlide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Closures•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-1Closures (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-2Closures (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-3Python•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):...22222return x + 1...>>> type (add_one)<type 'function'>Python>>> def add_one(x):...2222 2return x + 1...>>> type (add_one)<type 'function'>>>> add_one(2)3>>> a = lambda x: x + 1>>> type(a)<type 'function'>>>> a(2)3def add_one(x): return x + 1Python•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 ismap(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):...22222return 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):...22222return 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):...22222my_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-14First, 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-15First, 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-16First, 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-17First,


View Full Document

UT Dallas CS 4337 - #Sebesta ch09A subprogram-closure - rev2

Download #Sebesta ch09A subprogram-closure - rev2
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?