Ralph: oh, I have The Good Parts, there’s a long-enough rant about i++ in there as well. I still wasn’t swayed. He likes i +=1 because it’s more explicit, same reason Python doesn’t do i++ either. Explicit over implicit, as the Zen says.
I thought it was from “incrementor”, which I think I have in my ANSI-C (K&R) book… first they start out using the whole word, then the next example shortens it to “i”. The other word they used, with while loops, was “counter” but I don’t remember if they ever shortened that to “c”
Pullo:
even more goodies…
Javascript 1.8 has lamda-style and generators.
lamdas in Python are just basically inline functions. They do what functions do, but are statements. So, an anonymous inline function who is a statement rather than an object. In Python they’re basically used like one-offs. In Javascript, who already knows anonymous functions, I think it would just be a slightly cleaner big of code here and there maybe… not sure why they added it, actually.
Generators are pretty cool: basically you may have a function who is supposed to loop through things and return values to another function who’s calling for them. Normally, a function would run through its loop until it’s finished, and exit. People wanting to avoid this behaviour would try closures (who would still run all at once) or callbacks (run something after a loop value was returned maybe). Here, a generator has the “yield” keyword: it basically pauses the function, instead of exiting like return, and lets the original caller call it again, where it picks back up right where it left off. The only part stored in memory is the part of the loop you’re at, instead of the whole list, because the function is only creating one “loop” at a time.
Procs and Lambdas I know from Ruby. They’re pretty useful in their own right as they allow you to save reusable code as an object and, well, reuse it.
However, unless I’ve missed something, for a language such as JavaScript that already has anonymous functions, they don’t add much in the way of functionality, just a little syntactic sugar.
I had to look up what generators are in Python and they seem quite close to Ruby’s Enumerator class.
When you get into doing something a little more intense than changing CSS styles on the fly, I can imagine they’d be very useful.
Just having a little play around with this idea, I found this code on MDN, which will calculate the first ‘n’ values in the Fibonacci sequence:
function fibonacci(n){
var fn1 = 1;
var fn2 = 1;
for(var i=0; i<n; i++){
var current = fn2;
fn2 = fn1;
fn1 = fn1 + current;
yield current;
}
}
This is cool as, as you point out, you can then step through the values and have them calculated one at a time, as opposed to create them all in memory in one go.
I guess this is getting right off-topic now, so I’ll stop.
Thanks for pointing all of that out. It’s nice to use JavaScript for something other than webpage manipulation
To the best of my knowledge it was used in ForTran in 1954 whereas C wasn’t invented until the 1970s.
The use of single character variable names was very common in early ForTran as the computers only had a few hundred bytes available. The variables ‘a’ through ‘h’ were for floating point numbers and so ‘i’ was the obvious choice for the first integer that the program used. It didn’t really matter what the variable was going to be used for at that point so possibly its becoming restricted to use with loops occurred with C - although the computers I was working with in the late 70s still mostly only had a couple of hundred bytes of memory available so using longer variable names could result in the program being unable to compile.
Still playing around with lambdas and fund out you can do this:
Array.prototype.iterate = function(lambda){
return [lambda(i) for (i of this)]
}
var arr = [1,2,3,4,5];
var lambda = function(x) x * x;
console.log(arr.iterate(lambda));
// [1, 4, 9, 16, 25]