pseudoramble
Consider Using Collection Functions
I thought these functions were well known. I see them used incorrectly in code I read, which I found sorta surprising. I encourage folks to look into the collection utilities their language provides.

Here's a pretty typical example (Javascript):

``````var alphabet = ["a", "b", "c", "d", "e"];
var capitalized = [];
for (var i = 0; i < alphabet.length; i++) {
var upperCaseLetter = alphabet[i].toUpperCase();
capitalized.push(upperCaseLetter);
}
console.warn(capitalized);``````
This takes each element in the array, assumes a function called toUpperCase() exists on each element in the array, and pushes the value into a new array.

There's anything wrong with this (in the what & how it's doing it sense; I could've made a mistake) but there is a nicer way to generate this using Javascript:

``````var alphabet = ["a", "b", "c", "d", "e"];
var capitalized = alphabet.map(function(letter) {
return letter.toUpperCase();
});
console.warn(capitalized);``````
map() takes a function as its argument, which is used to generate new values. map() then iterates across each element in the list, executes the given function, and adds the returned value into a new array.

In other words, it does what the first snippet did before, but gets rid of the manual tracking and tediousness for you. Great right?

When using these functions, you've gotta make a concerted effort to dump your old habits. They do sort of look like typical loops in some cases. Here's a this-works-but-is-completely-missing-the-point example using the some() function:

``````var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var hasBigNumbers = false;
numbers.some(function(number) {
var result = number > 9;
if (result) {
hasBigNumbers = result;
return true;
}
});
console.warn(hasBigNumbers);``````
The intent of some() is to find out if any element in a collection can be evaulated to true. The result of some is a boolean indicating whether the predicate was true for one or more values within the collection. Its usage above tries to use it like a short-circuiting loop. While it exhibits this behavior, it's only by coincidence. It could be implemented other ways and still be correct.

A better way to use some() may look like this:

``````var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var hasBigNumbers = numbers.some(function(number) {
return number > 9;
});
console.warn(hasBigNumbers);``````
This has the same effect as the first version with some() but in a lot less words, and a lot less mechanics.

One handy thing is you can chain these kinds of calls together in some scenarios. Here's a modified example of some code I was working on a while back for part of this site:

``````var entries = lookup_entries();
var matching_entries = entries.map(function(entry) {
return {
title : entry.title,
score : determine_score(entry);
};
}).filter(function(entry) {
return entry.score > 0;
});``````
Once again we see map() in action, but we also see another function filter() . filter() takes each element, and determines if it should be "accepted" (the function evaulates to true) or "rejected" (the function evaluates to false). The net result of filter is a new array where all of the "accepted" values are held, but the "rejected" ones are not.

I sorta like how it looks. It seems like others don't. I dunno - whatever floats your boat.

More importantly, it seems like others want to use them but are using them incorrectly. I think you should use them without a doubt. If you are though, try and use them correctly.