day macha
To hell with for loops

All these process an array, returning the processed:

var array_to_return;

for(var i=0;i<old_array.length;i++) {
array_to_return.push(processed old_array[i]);
}
return array_to_return;

and

var array_to_return;

for each(var i in old_array) {
array_to_return.push(processed i);
}
return array_to_return;

are not as nice as

return old_array.map(function (i) { return processed i; });

and (this removes things from an array)

return old_array.filer(function (i) { return i!=something_bad; });

is pretty nice too.

You need to be taking arrays into functions to modify them, however. This seems to be standard practice in functional programming.

It’s really nice: having your program’s data in arrays being processed, thereby reducing the data fragmentation throughout your program—great for debugging. Example (all but the last are arrays being processed):

var spaceData = getDataFromSpace();
debugOutput(spaceData);
var magicalGypsies = getMagicalGypsies();
debugOutput(magicalGypsies);
var predictions = processSpaceData(spaceData, magicalGypsies);
debugOutput(predictions);
var xmlPredictions = xmlifyPredictions(predictions);
sendXMLPredictionsToAGraphMaybe(xmlPredictions)
Blog comments powered by Disqus