spudly.shuoink.com

the best way to predict the future is to implement it

Entries Comments


Month: May, 2008

Array.difference();

31 May, 2008 (17:00) | JavaScript | 2 comments

Here’s a prototyped method for Array that computes the symmetric difference between two arrays. It basically returns all the values that are in one or the other array but not both.
Usage:
var setA = [ 1, 2, 3, 4, 5, 6 ];
var setB = [ 4, 5, 6, 7, 8, 9, 10 ];
var diff = setA.difference( [...]

Array.complement();

31 May, 2008 (16:51) | JavaScript | No comments

Heres a method to get the relative complement of one array to another. Thus, it will return an array of everything that is in the current array but not in the array you pass in.
Usage:
var setA = [ 1, 2, 3 ];
var setB = [ 3, 4, 5 ];
var setC = setA.complement( setB ); // [...]

Array.union();

31 May, 2008 (15:36) | JavaScript | No comments

Here’s another set theory prototype for the Array object in JavaScript. This one performs a union of the two arrays.
Usage;
var setA = [ 1, 2, 3, 4, 5, 6 ];
var setB = [ 4, 5, 6, 7, 8 ];
var union = setA.union( setB ); // Now union is [ 1, 2, 3, 4, 5, 6, [...]

Array.intersection()

31 May, 2008 (14:48) | JavaScript | 1 comment

I’m sure a lot of you know what set theory is if you’re reading a programming blog. Anyway, a big part of set theory is the concept of an intersection between two sets of data. This is essentially the parts of the sets that the two have in common. Here’s an prototype for the intersection [...]

Array.grep

31 May, 2008 (14:38) | JavaScript | No comments

Here’s another prototype for the javascript Array class of objects: Array.grep(). This takes either a regular expression object or any other kind of value that can be compared with ===. If a regex is given, it will test the regex against each value. If something else is given, it will simply compare them with ===. [...]

Array.unique();

31 May, 2008 (09:07) | JavaScript | 2 comments

Here’s a prototype method for the Array object that allows you to get the unique values in an array!
Array.prototype.unique = function() {

var seen = {};
var unique = [];

for ( var i = 0; i < this.length; i++ ) {
if ( [...]