Your SlideShare is downloading. ×
0
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Closures
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Closures

265

Published on

Anonymous functions. Functions as data-types.

Anonymous functions. Functions as data-types.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
265
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Advanced Programming
  • 2. //delegatesFunc<string, string> myFunc = delegate(string var1) { return "some value"; };//lambda’sFunc<string, string> myFunc = var1 => "some value";//Calling functionsstring myVar = myFunc("something");
  • 3. A first class function simply means that it is a function whichyour language treats as a first class data type.
  • 4. static void Main(string[] args) { var inc = GetAFunc(); Console.WriteLine(inc(5)); Console.WriteLine(inc(6));}public static Func<int,int> GetAFunc(){ var myVar = 1; Func<int, int> inc = delegate(int var1) { myVar = myVar + 1; return var1 + myVar; }; return inc;}
  • 5. A free variable just happens to be a variable which isreferenced in a function which is not a parameter of thefunction or a local variable of the function.
  • 6. var myVar = "this is good";Func<string, string> myFunc = delegate(string var1) { return var1 + myVar; };
  • 7. A closure is a first-class functions with free variables that arebound in the lexical environment.
  • 8. Closures
  • 9. void someOperation() { long t0 = System.nanoTime(); boolean success = false; try { someOperationInternal(); // may fail, throwing SomeException success = true; } finally { long elapsed = System.nanoTime() - t0; logElapsedTime("someOperation", success, elapsed); }} void newOperation() { long t0 = System.nanoTime(); boolean success = false; try { someOtherOperationInternal(); // may fail, throwing SomeException success = true; } finally { long elapsed = System.nanoTime() - t0; logElapsedTime("someOperation", success, elapsed); }}
  • 10. It forces the programmer to constantly repeat fairly common tasks everytime a similar operation is needed.
  • 11. a = (1 + 2) * 5;
  • 12. Common pattern appearing in your code.
  • 13. Abstract the common parts.Log(“someOperations”, ()=>someOperations());
  • 14. Allowing the caller to provide the parts that differ. Log(“someOperations”, ()=>someOperations());Log(“someOtherOperations”, ()=>someOtherOperations()); Log(“someCoolOperations”, ()=>someCoolOperations());
  • 15. Control Abstractions
  • 16. Example

×