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

Closures

  • 1.
  • 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 classfunction 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 variablejust 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 isa first-class functions with free variables that are bound in the lexical environment.
  • 8.
  • 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 theprogrammer to constantly repeat fairly common tasks every time a similar operation is needed.
  • 11.
    a = (1+ 2) * 5;
  • 12.
  • 13.
    Abstract the commonparts. Log(“someOperations”, ()=>someOperations());
  • 14.
    Allowing the callerto provide the parts that differ. Log(“someOperations”, ()=>someOperations()); Log(“someOtherOperations”, ()=>someOtherOperations()); Log(“someCoolOperations”, ()=>someCoolOperations());
  • 15.
  • 16.