Class: Harray

Harray

An infinite array.

new Harray(element, formula)

Parameters:
Name Type Description
element Number | Array Every number passed as argument before the formula will be used as an element. You can also pass an array as the first argument instead of multiple elements.
formula Harray~formula A formula which will be used to calculate the next element.
If it does not exist the difference between the last two elements will be used as increment value to generate the sequence.
If there's a single element and no formula was provided the sequence will be generated using 1 as increment.
Properties:
Name Type Description
length Infinity Returns the length of the Harray. (TIP: It's infinite)
cycle Array If the harray uses a finite cycle to generate its elements, the cycle will be here, otherwise it will be undefined.
Please see the Harray.cycle() method.
Source:
Example
let evensHarray = new Harray(0, 2);
evensHarray.get(2) // -> 4
evensHarray.get(5) // -> 10

let oddsHarray = new Harray(1, 3);
oddsHarray.get(3) // -> 7
oddsHarray.get(4) // -> 9

let binaryHarray = new Harray(1, function(element) {
     return element * 2;
};

// If your environment supports Proxies you can access elements directly using brackets notation
binaryHarray[1] // -> 2
binaryHarray[2] // -> 4
binaryHarray[3] // -> 8

// Otherwise you can use `.get(x)`
binaryHarray.get(1) // -> 2
binaryHarray.get(2) // -> 4
binaryHarray.get(3) // -> 8

let nonUniformHarray = new Harray(1, 10, 22, 24);
nonUniformHarray.get(1) // -> 10
nonUniformHarray.get(3) // -> 24
nonUniformHarray.get(4) // -> 26
nonUniformHarray.get(5) // -> 28

let oneItemHarray = new Harray(10);
oneItemHarray.get(0) // -> 10
oneItemHarray.get(1) // -> 11
oneItemHarray.get(2) // -> 12

let oneItemHarray = new Harray([20, 30]);
oneItemHarray.get(0) // -> 20
oneItemHarray.get(1) // -> 30
oneItemHarray.get(2) // -> 40

Methods


<static> addMethod(methodName, method)

Adds a method to the Harray prototype.
Parameters:
Name Type Description
methodName String The name of the property which will hold the method.
method function The method which will be added to the prototype.
Source:
Example
let getDoubleFunction = function(index) {
     return this.get(i) * 2;
}

Harray.addMethod('getDouble', getDoubleFunction);

let harr = new Harray(1, 2);
harr.getDouble(0) // -> 2
harr.getDouble(1) // -> 4

<static> cycle(cycle)

Creates a Harray object which repeats the given cycle.
Parameters:
Name Type Description
cycle * | Array Arguments you want to use to create a cycle or an array of elements.
Source:
Returns:
Harray - A Harray object which repeats the given cycle.
Example
let cycle = Harray.cycle(1, 2, 3);

cycle.get(2) // -> 3
cycle.get(3) // -> 1
cycle.get(4) // -> 2

let anotherCycle = Harray.cycle([0, 1]);
anotherCycle.get(1) // -> 1
anotherCycle.get(2) // -> 0

[undefined]()

Defines an iterator for Harray.
Source:
Returns:
GeneratorFunctionPrototype

every(fn [, index])

This method calls the callback function once for every element in the Harray until it returns a falsy value.
Parameters:
Name Type Argument Description
fn function A callback function which will be called with the element as the only argument. It should return a falsy value in order to stop the loop.
index Number <optional>
A starting index. Default is 0.
Source:
Throws:
Will throw a TypeError if no callback function was provided.
Type
TypeError
Example
let harr = new Harray(0, 2);
let evensUntilTen = [];
harr.every(function(element) {
     if (element >= 10) {
         return false;
     } else {
         evensUntilTen.push(element);
         return true;
     }
});

console.log(evensUntilTen) // -> [0, 2, 4, 6, 8]

get(index)

Gets the value from an index. If your environment supports proxies you can directly access values using brackets notation, just like: `myHarray[1]`.
Parameters:
Name Type Description
index Number A value's index.
Source:
Returns:
value - The value of an index.

getIterator()

Gets the Harray iterator.
Source:
Returns:
GeneratorFunctionPrototype

getRange( [start] [, end])

Gets an aray with numbers from a start index until a final index (inclusively). If the final index is smaller then the initial index the returned array will be in the reverse order (from start to end, as expected). If only one argument is passed it will be used as the final index.
Parameters:
Name Type Argument Description
start Number <optional>
The initial index. Defaults to 0.
end Number <optional>
The final index. Defaults to 0.
Source:
Returns:
rangeArray - An array of numbers from the initial index until the final index (inclusively).
Example
let harr = new Harray(0, 2);
let firstTenEvens = [];
harr.getRange(0, 9);

console.log(firstTenEvens) // -> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

getReadableStream( [startIndex])

Returns a readable stream which outputs each one of the Harray elements.
Parameters:
Name Type Argument Description
startIndex Number <optional>
The index to start from when outputting content. Defaults to 0.
Source:
See:
Example
// Prints an odd number to the console every two seconds
let harr = new Harray(1, 3);
let positiveOddsStream = harr.getReadableStream();

positiveOddsStream.on('data', function(chunk) {
     positiveOddsStream.pause();
     console.log(chunk.toString());
     setTimeout(function() {
         positiveOddsStream.resume();
     }, 2000);
});

indexOf(element)

Returns the index of an element into the Harray.
Parameters:
Name Type Description
element * Any element in the Harray.
Source:
Returns:
The index of that element in the Harray
Type
Number
Example
let harr = new Harray(0, 5);

harr.indexOf(5) // -> 1
harr.indexOf(20) // -> 5

set(index, index)

Sets the value for an index.
Parameters:
Name Type Description
index Number An index.
index * The value for that index.
Source:
Returns:
harray - The Harray instance in which the value has been set.

some(fn [, index])

This method calls the callback function once for every element in the Harray until it returns a truthy value.
Parameters:
Name Type Argument Description
fn function A callback function which will be called with the element as the only argument. It should return a truthy value in order to stop the loop.
index Number <optional>
A starting index. Default is 0.
Source:
Throws:
Will throw a TypeError if no callback function was provided.
Type
TypeError
Example
let harr = new Harray(1, 3);
let oddsUntilTen = [];
harr.some(function(element) {
     if (element >= 10) {
         return true;
     } else {
         oddsUntilTen.push(element);
     }
});

console.log(oddsUntilTen) // -> [1, 3, 5, 7, 9]

zip(arr)

Creates an object joining values from the Harray (which will be the keys) and values from the argument array (which will be the values).
Parameters:
Name Type Description
arr Array An array of values.
Source:
Returns:
An object whose keys come from the Harray and values come from the argument passed.
Type
Object
Example
let harr = new Harray(0, 2);
let fruits = ['apple', 'pear', 'banana', 'papaya'];
harr.zip(fruits) // -> {0: 'apple', 2: 'pear', 4: 'banana', 6: 'papaya'}

Type Definitions


formula(element, index)

This is the formula that will be used to generate the next element in the sequence. It receives the current element, does whatever you want with it and then returns the next element for the sequence.
Parameters:
Name Type Description
element * The element before the one being calculated now.
index Number The index for the element being calculated now.
Source:
Returns:
nextElement - The next element for the sequence.
Example
let timesTen = function(element) {
  return element * 10;
}

let harr = new Harray(2, timesTen);
harr.get(0) // -> 2
harr.get(1) // -> 20
harr.get(2) // -> 200

let twoTimesIndex = function(element, index) {
  return index * 2;
}

let anotherHarr = new Harray(0, twoTimesIndex);
harr.get(0) // -> 0
harr.get(1) // -> 2
harr.get(2) // -> 4