File: arraygen.txt

Recommend this page to a friend!
  Classes of Stephen Chapman   Array Generator   arraygen.txt   Download  
File: arraygen.txt
Role: Documentation
Content type: text/markdown
Description: Documentation for the array methods
Class: Array Generator
Initialize arrays using several methods
Author: By
Last change:
Date: 9 years ago
Size: 4,061 bytes
 

Contents

Class file image Download

Generating Arrays

These have all been set up as methods added to the built in Array object for consistency. In most cases the []. on the front of the call acts simply as a reminder that the method returns an array. The exceptions actually use that array to pass in an initial value for the method to use as the first entry in the array.

Each of these seven methods creates a dense array in JavaScript containing values based on the parameters that are passed to it. While the examples below are creating small arrays that you could just as easily type in manually, these methods allow the creation of much larger arrays simply by supplying the appropriate parameters.

populate

myArray = [].populate(10);

Creates an array with the entries having values corresponding to their position in the array. For example the above call creates the array [0,1,2,3,4,5,6,7,8,9]

You can populate arrays as big as you require (up to the maximum array size of 4294967294) simply by specifying the number of elements you want in the array and each position will be given a value that is the same as its index.

range

myArray = [].range(-5,10);

creates an array with entries in the specified range. For example the above call creates an array containing [-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]

Note that it doesn't matter which way around you specify the two values, this method always returns an array of integers in ascending order.

to

myArray = [10].to(-5);

similar to the range method except that this one reads the starting value from the array the method is attached to and so can produce arrays with the integers in descending order. For example the above call creates the array [10,9,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5]

intervals

myArray = [].intervals(1,10,0.5);

unlike the above methods which populate the array with integers, this method can apply any fixed interval between the values. The first parameter supplies the starting point, the second is the length of the array to be produced and the third is the interval between the values (which defaults to 1 if not supplied). For example the above call creates the array [1,1.5,2,2.5,3,3.5,4,4.5,5,5.5]

To reverse the direction of the values in the array simply specify a negative gap.

alpha

myArray = [].alpha[40];

not numbers this time but uppercase letters of the alphabet that simply repeat the alphabet as many times as required to fill the array. For example the above call creates the array ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N']

repeat

myArray = ['#'].repeat(10);

puts the same value in all the entries in the array but allows you to specify what value that is. For example the above call creates the array ['#', '#','#','#','#','#','#','#','#','#']

plot

f = function(x) {return x * 2;}; myArray = [].plot(f,0,5,0.5);

The most complex of the array methods generates an array of objects where each object consists of X and Y coordinates. First it calls the intervals method using the second through fourth parameters supplied to generate the X values. It then uses the function supplied in the first parameter to generate a corresponding Y value for each of the X values using that formula. For example the above call creates the array [{x:0,y:0},{x:0.5,y:1},{x:1,y:2},{x:1.5,y:3},{x:2,y:4}]

hasIndex

for (k in myArray) { if (myArray.hasIndex(k)) { // code for processing array entries goes here - only array entries will be processed as the if statement skips over other properties and methods } }

this one doesn't generate an array, instead it provides a safe way to use for-in loops with arrays where additional methods or properties have been added to either the array itself or to the array prototype (as I have done with all of the array methods provided here).