Range function implementation in Javascript: An Introduction to Increment, Pre-increment and Python's range function

One of the key features of python's syntax is that in order to achieve semantic validity, the style of programming is  intentionally biased towards a certain code-cleanliness. This feat is owing to increased conformity at the cost of less flexibility at certain points. For instance in javascript you can often force an entire program into a for-loop with no visible operating loop-construct.

As such there are no post-increments (postcrement) or pre-increments (increment) operators, but rather only the full expanded syntax i = i+3; In fact you cannot even shortcut through means of the add-equal += operator, as this is by default inherited as a function for strings.

Recall that a post-increment gets incremented post (after) its evaluation, the operator which many are the  most familiar with.
Note: To demonstrate take this example,  you could in fact just try this ad hoc in your browsers javascript console, which you can access in Google-Chrome and Opera by pressing CTRL+SHIFT+I; in Firefox in the Menu Developer and then selecting Errors. In Opera press the ESC key after it has loaded, in Chrome click the Console Tab.

var a = {} //define a as an Object
a[0] = 0 //set the zeroth element to zero, not null!
0 //console returns variable context in this case zero
a[0]++ < 1 //try to evaluate a post-increment statement
true
a[0] = 0 //reset variable
++a[0] < 1
false
//declare new variables for your operating loop, otherwise variables previously 
//defined in the global namespace will be imported

for(var i = 0; i < 5; ++i) {
Console.WriteLine(i);
}
1 //Outputs 1 since i is incremented before the evaluation
2
3
4

A for loop in javascript, ANSI C, C++, C# etc... is first declared, evaluated and then operated, evaluated and operated.... till an exit condition is met.

In the above example the only point at which i has the value 0 is during its declaration. The next step is incrementing, evaluating and operating on the loop's declarations.

Python forgoes this alltogether exclusively using iterators and the function range instead, which returns a very lean iterable object. Such an object is capable of returning one by one its members by answering to certain special functions such as__iter__() or __getitem__() methods, which are invoked by the Python Interpreter during runtime.
the function range([start], stop[, step]) returns a iterables (an numeric array) with the artihmetic progression set in step (default = 1)
If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero , Source: PyDoc






As such the for loop in python assumes the following form:

#!/usr/bin/python
#note that m does not have to be declared elsewhere
for n in range(1, 5):
print(n) #Python >3 requires proper calling of the print("hey %s %s" % ("Joe", 1337)) - function
#else is optional
else:
print('for loop ended')
//returns:
0
1
2
3
4

#range actually produces a generator object, not an actual array as such
a = range(10)
#expands to
a
#returns:
range(0,10)
#list allows to iterate on this generator function
#a generator object only contains methods for iteration and means to operate on variables declared on the fly
#it does not contain the iterables
list(a)
#returns an iterable: (by calling the generator function)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


This is similar to
Java's:
for (int i : IntArray)
PHP's:
foreach(objIter as elem){ ..loop operation...}
C#'s:
foreach (type value in objIter){ Console.WriteLine(value);} //type is the type for value e.g. string, int...


Note: There are performance issues with the use of foreach constructs, involving much more function calls and stack overhead. In Javascript never use for-in for Arrays but only iterable objects.

LihatTutupKomentar