I'm struggling with this concept. By looking at below, I don't understand when I type on the console that "counter" is equal to the following below.  How does it know just to pick up this piece of the code? How does this work?  function(val) {   count += val;   return console.log(count); };   The Code  var incrementer = function(initialValue) {   var count = initialValue;    return function(val) {     count += val;     return console.log(count);   }; };  var counter = incrementer(5);  counter(3); counter(1);
1086
How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?  I have seen  given on Wikipedia, but unfortunately it did not help.
23909
Looking for reviewers to suggest optimization and improvements, and to evaluate coding practices.  final class Variables {     private final int x;     private final int y;      Variables(int x2, int y2) {         this.x = x2;         this.y = y2;     }      public int getX() {         return x;     }      public int getY() {         return y;     }      @Override public String toString()  {          return "x = " + x + " y = " + y;     } }  public final class FindMissing {      private FindMissing() {};      /**  * Takes an input a sorted array with two variables that repeat in the  * range. Remaining variable are consecutive. and returns two variables that repeats  *   * eg: for a range of 1,2,3,4, a valid input is [1, 4], [1, 3] etc.  *   * @param a : the sorted array.  */ /*  * Why is start needed ?  * If start is not given then if a1 is empty how do we know if missing numbers were  * [1, 2] or [4, 5] ??.   */ public static Variables sortedConsecutiveTwoMissing (int[] a, int startOfRange) {     if (a == null) throw new NullPointerException("a1 cannot be null. ");      int low = startOfRange;     // calculating the highest value of the range. it needs to be 2 more than length since all elememts are consecutive.     int high = startOfRange + (a.length - 1) + 2;      int i = 0;     boolean foundX = false;     int x = 0;       while (i &lt; a.length) {         if (a[i] != low) {             if (foundX) {                 return new Variables(x, low);             } else {                 x = low;                 foundX = true;             }         } else {             i++;         }         low++;     }      return new Variables(x, high);  }  public static void main(String[] args) {         // unsortedConsecutiveTwoMissing         int[] a6 = {1, 4};         System.out.println("Expected 2 and 3, Actual " + sortedConsecutiveTwoMissing(a6, 1));         // unsortedConsecutiveTwoMissing         int[] a7 = {-4, -1};         System.out.println("Expected -3 and -2, Actual " + sortedConsecutiveTwoMissing(a7, -4));         int[] a8 = {-4, -3, -2, -1};         System.out.println("Expected 0 and 1, Actual " + sortedConsecutiveTwoMissing(a8, -4));         int[] a9 = {5};         System.out.println("Expected 4 and 6, Actual " + sortedConsecutiveTwoMissing(a9, 4));         }      }
92031
Whats this syntax useful for :       function(String... args)   Is this same as writing       function(String[] args)    with difference only while invoking this method or is there any other feature involved with it ?
36511
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:  var myCallBackExample = {     myFirstFunction : function( param1, param2, callback ) {         // Do something with param1 and param2.         if ( arguments.length == 3 ) {             // Execute callback function.             // What is the "best" way to do this?         }     },     mySecondFunction : function() {         myFirstFunction( false, true, function() {             // When this anonymous function is called, execute it.         });     } };   In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.
2689
What is the idiomatic Python equivalent of this C/C++ code?  void foo() {     static int counter = 0;     counter++;     printf("counter is %d\n", counter); }   specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
41914
Consider:  int testfunc1 (const int a) {   return a; }  int testfunc2 (int const a) {   return a; }   Are these two functions the same in every aspect or is there a difference?  I'm interested in an answer for the C language, but if there is something interesting in the C++ language, I'd like to know as well.
24985
I saw , but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?
41585
Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.   And now that we're here, how do they differ from a regular function?
79695
I am trying to calculate a field that marks duplicates and sequentially numbers them.  I am using the code below that I found here: .  It works, but I get a duplicate starting value before it begins sequentially numbering unless I only run the field calculator on the selected values.  When I calculate after manually selecting the duplicate rows and running it on only those values, it works fine.  Here's the code:  prevFieldValue = '' counter = 0 def GetDuplicateCounter(myFieldValue):   global prevFieldValue   global counter   if myFieldValue == prevFieldValue:     counter += 1   else:     counter = 1     prevFieldValue = myFieldValue   return counter   Results running it on all roads (nothing selected):    Results running it after manually selecting the duplicated fields (not too efficient!).
41848
I here define the hierarchy of basic mathematical operators and their respective "inverse" operation (see ).  $$ \begin{array}{c|c|c|}   &amp; \text{Operator} &amp; \text{"Inverse"} \\ \hline \text{Incrementation} &amp; a+1 &amp; a-1 \\ \hline \text{Addition} &amp; a+b &amp; a-b \\ \hline \text{Multiplication} &amp; ab &amp; \frac{a}{b} \\ \hline \text{Exponentiation} &amp; a^b &amp; \sqrt[b]{a} \\ \hline \text{Tetration} &amp; ^ba &amp; \sqrt[b]{a}_s \\ \hline \text{} \vdots &amp;   \vdots &amp; \vdots \\ \hline \end{array} $$  Now, it is clear that each "level" is simply the previous one except the process is done several times.  Adding two integers is like incrementing one integer many times.  Multiplication is addition of the same number many times.    My question is: does anything precede incrementation?
107998
Is it like...  var obj = new Object();  obj.function1 = function(){     //code }   or something like that?
95803
I'm having problems trying to overload the post increment operator in C#. Using integers we get the following results.  int n;  n = 10; Console.WriteLine(n); // 10 Console.WriteLine(n++); // 10 Console.WriteLine(n); // 11  n = 10; Console.WriteLine(n); // 10 Console.WriteLine(++n); // 11 Console.WriteLine(n); // 11   But, when I try it using classes, it looks like the objects are exchanged.  class Account {     public int Balance { get; set; }     public string Name { get; set; }      public Account(string name, int balance)     {         Balance = balance;         Name = name;     }      public override string ToString()     {         return Name + " " + Balance.ToString();     }      public static Account operator ++(Account a)     {         Account b = new Account("operator ++", a.Balance);         a.Balance += 1;         return b;     }      public static void Main()     {         Account a = new Account("original", 10);          Console.WriteLine(a); // "original 10"          Account b = a++;          Console.WriteLine(b); // "original 11", expected "operator ++ 10"         Console.WriteLine(a); // "operator ++ 10", expected "original 11"     } }   Debugging the application, the overloaded operator method, returns the new object with the old value (10) and the object that has been passed by reference, has the new value (11), but finally the objects are exchanged. Why is this happening?
588
JavaScript allows functions to be treated as objects--if you first define a variable as a function, you can subsequently add properties to that function.  How do you do the reverse, and add a function to an "object"?  This works:  var foo = function() { return 1; }; foo.baz = "qqqq";   At this point, foo() calls the function, and foo.baz has the value "qqqq".  However, if you do the property assignment part first, how do you subsequently assign a function to the variable?  var bar = { baz: "qqqq" };   What can I do now to arrange for bar.baz to have the value "qqqq" and bar() to call the function?
36777
What is the difference between == and === in JavaScript?  I have also seen != and !== operators.  Are there more such operators?
64302
I frequently find myself writing code like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; foreach (int i in list) {     Console.Write("{0}\t", i.ToString()); } Console.WriteLine();   Better would be something like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; Console.WriteLine("{0}\t", list);   I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?
9792
Yesterday I had a two-hour technical phone interview (which I passed, woohoo!), but I completely muffed up the following question regarding dynamic binding in Java.  And it's doubly puzzling because I used to teach this concept to undergraduates when I was a TA a few years ago, so the prospect that I gave them misinformation is a little disturbing...  Here's the problem I was given:  /* What is the output of the following program? */  public class Test {    public boolean equals( Test other ) {     System.out.println( "Inside of Test.equals" );     return false;   }    public static void main( String [] args ) {     Object t1 = new Test();     Object t2 = new Test();     Test t3 = new Test();     Object o1 = new Object();      int count = 0;     System.out.println( count++ );// prints 0     t1.equals( t2 ) ;     System.out.println( count++ );// prints 1     t1.equals( t3 );     System.out.println( count++ );// prints 2     t3.equals( o1 );     System.out.println( count++ );// prints 3     t3.equals(t3);     System.out.println( count++ );// prints 4     t3.equals(t2);   } }   I asserted that the output should have been two separate print statements from within the overridden equals() method: at t1.equals(t3) and t3.equals(t3).  The latter case is obvious enough, and with the former case, even though t1 has a reference of type Object, it is instantiated as type Test, so dynamic binding should call the overridden form of the method.  Apparently not.  My interviewer encouraged me to run the program myself, and lo and behold, there was only a single output from the overridden method: at the line t3.equals(t3).  My question then is, why?  As I mentioned already, even though t1 is a reference of type Object (so static binding would invoke Object's equals() method), dynamic binding should take care of invoking the most specific version of the method based on the instantiated type of the reference.  What am I missing?
22301
I just came across an interesting situation in JavaScript.  I have a class with a method that defines several objects using object-literal notation.  Inside those objects, the this pointer is being used.  From the behavior of the program, I have deduced that the this pointer is referring to the class on which the method was invoked, and not the object being created by the literal.    This seems arbitrary, though it is the way I would expect it to work.  Is this defined behavior?  Is it cross-browser safe?  Is there any reasoning underlying why it is the way it is beyond "the spec says so" (for instance, is it a consequence of some broader design decision/philosophy)?  Pared-down code example:  // inside class definition, itself an object literal, we have this function: onRender: function() {      this.menuItems = this.menuItems.concat([         {             text: 'Group by Module',             rptletdiv: this         },         {             text: 'Group by Status',             rptletdiv: this         }]);     // etc }