SlideShare a Scribd company logo
1 of 16
Advanced Programming
//delegates
Func<string, string> myFunc = delegate(string var1)
      {
         return "some value";
      };

//lambda’s
Func<string, string> myFunc = var1 => "some value";

//Calling functions
string myVar = myFunc("something");
A first class function simply means that it is a function which
your language treats as a first class data type.
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;
}
A free variable just happens to be a variable which is
referenced in a function which is not a parameter of the
function or a local variable of the function.
var myVar = "this is good";
Func<string, string> myFunc = delegate(string var1)
   {
        return var1 + myVar;
   };
A closure is a first-class functions with free variables that are
bound in the lexical environment.
Closures
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);
  }
}
It forces the programmer to constantly repeat fairly common tasks every
time a similar operation is needed.
a = (1 + 2) * 5;
Common pattern appearing in your code.
Abstract the common parts.

Log(“someOperations”, ()=>someOperations());
Allowing the caller to provide the parts that differ.

    Log(“someOperations”, ()=>someOperations());
Log(“someOtherOperations”, ()=>someOtherOperations());
    Log(“someCoolOperations”, ()=>someCoolOperations());
Control Abstractions
Example

More Related Content

What's hot

Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous JavascriptGarrett Welson
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsJay Baxi
 
Inline and lambda function
Inline and lambda functionInline and lambda function
Inline and lambda functionJawad Khan
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2matifch
 
Unit tests in_symfony
Unit tests in_symfonyUnit tests in_symfony
Unit tests in_symfonySayed Ahmed
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptNexThoughts Technologies
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkESUG
 
Kojak Metrics Explained
Kojak Metrics ExplainedKojak Metrics Explained
Kojak Metrics ExplainedBart Wood
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structureJd Mercado
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 

What's hot (20)

Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Inline and lambda function
Inline and lambda functionInline and lambda function
Inline and lambda function
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2
 
Iteration
IterationIteration
Iteration
 
Unit tests in_symfony
Unit tests in_symfonyUnit tests in_symfony
Unit tests in_symfony
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Introduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScriptIntroduction of Object Oriented JavaScript
Introduction of Object Oriented JavaScript
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Kojak Metrics Explained
Kojak Metrics ExplainedKojak Metrics Explained
Kojak Metrics Explained
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 

Similar to Closures

Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)Allan Marques Baptista
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsPiotr Pelczar
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of LambdasEsther Lozano
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with JavascriptDeepankar Chopra
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023Laurence Svekis ✔
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfOrtus Solutions, Corp
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015senejug
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpuDhaval Jalalpara
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 

Similar to Closures (20)

Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)
 
Function C++
Function C++ Function C++
Function C++
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Scala functions
Scala functionsScala functions
Scala functions
 
The... Wonderful? World of Lambdas
The... Wonderful? World of LambdasThe... Wonderful? World of Lambdas
The... Wonderful? World of Lambdas
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdfITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
ITB_2023_CommandBox_Task_Runners_Brad_Wood.pdf
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
SeneJug java_8_prez_122015
SeneJug java_8_prez_122015SeneJug java_8_prez_122015
SeneJug java_8_prez_122015
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 

Closures

  • 2. //delegates Func<string, string> myFunc = delegate(string var1) { return "some value"; }; //lambda’s Func<string, string> myFunc = var1 => "some value"; //Calling functions string myVar = myFunc("something");
  • 3. A first class function simply means that it is a function which your 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 is referenced in a function which is not a parameter of the function 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 are bound in the lexical environment.
  • 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 every time 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());