Wednesday, March 25, 2015

Closures

Samples


This is a closure in Apache Spark:
JavaRDD<String> distFile = sc.textFile("README.md");
JavaRDD<String> words =
 distFile.flatMap(line -> Arrays.asList(line.split(" ")));



References

  1. [DeveloperFusion] The beauty of closures
    1. To put it very simply, closures allow you to encapsulate some behaviour, pass it around like any other object, and still have access to the context in which they were first declared
      1. This allows you to separate out control structures, logical operators etc from the details of how they're going to be used. 
      2. The ability to access the original context is what separates closures from normal objects
  2. [StackOverflow] What is a closure
    1. A closure is a persistent scope which holds on to local variables even after the code execution has moved out of that block
  3. [StackExchange] What is a closure
    1. a function that can be stored as a variable (referred to as a "first-class function") and has special ability to access other variables local to the scope it was created in.
    2.  An object is data that has one or more functions bound to it. A closure is a function that has one or more variables bound to it. The two are basically identical, at an implementation level at least. 
      1. the object is not defined up-front like an object class, or instantiated through a constructor call in your code. 
      2. Instead, you write the closure as a function inside of another function. 
        1. The closure can refer to any of the outer function's local variables, and the compiler detects that and moves these variables from the outer function's stack space to the closure's hidden object declaration. 
        2. You then have a variable of a closure type, and even though it's basically an object under the hood, you pass it around as a function reference, because the focus is on its nature as a function.