new Iter8or(iterable, optionsopt)
Creates an instance of Iter8or.
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
iterable | Iterable | | The iterable object to use as the source data set. | |||||||||||||||||
options | Object | <optional> | {} | An object with additional options.Properties
|
- Source
Throws:
- If `iterable` is null, undefined, boolean, or function.
- Type
- TypeError
Methods
avg(fnopt) → {number}
Calculates the average of the elements in the iterable.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn | function | <optional> | Optional mapping function to apply before averaging. |
- Source
Returns:
The average value.
- Type:
- number
Example
const iter = new Iter8or([1, 2, 3]);
const average = iter.avg();
console.log(average); // 2
concat(…iterators) → {Iter8or}
Concatenates the current iterable with other Iter8or instances.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
iterators | Iter8or | <repeatable> | The iterators to concatenate. |
- Source
Throws:
- Throws if any of the provided iterators are not instances of Iter8or.
- Type
- Error
Returns:
A new instance with the concatenated iterable.
- Type:
- Iter8or
Example
const iter1 = new Iter8or([1, 2]);
const iter2 = new Iter8or([3, 4]);
const concatenated = iter1.concat(iter2);
console.log([...concatenated]); // [1, 2, 3, 4]
drop(n) → {Iter8or}
Drops the first `n` elements from the iterable.
Parameters:
Name | Type | Description |
---|---|---|
n | number | The number of elements to drop. |
- Source
Returns:
A new instance with the dropped iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3, 4]);
const dropped = iter.drop(2);
console.log([...dropped]); // [3, 4]
filter(predicate) → {Iter8or}
Filters the iterable using the provided predicate function.
Parameters:
Name | Type | Description |
---|---|---|
predicate | function | The filtering function. |
- Source
Returns:
A new instance with the filtered iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3, 4]);
const filtered = iter.filter(x => x % 2 === 0);
console.log([...filtered]); // [2, 4]
flat(depth) → {Iter8or}
Flattens the iterable up to the specified depth.
Parameters:
Name | Type | Description |
---|---|---|
depth | number | The depth to flatten the iterable. |
- Source
Returns:
A new instance with the flattened iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, [2, [3, 4]]]);
const flattened = iter.flat(2);
console.log([...flattened]); // [1, 2, 3, 4]
flatMap(fn) → {Iter8or}
Maps and flattens the iterable using the provided function.
Parameters:
Name | Type | Description |
---|---|---|
fn | function | The function to map and flatten the iterable. |
- Source
Returns:
A new instance with the flattened iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3]);
const flatMapped = iter.flatMap(x => [x, x * 2]);
console.log([...flatMapped]); // [1, 2, 2, 4, 3, 6]
map(fn) → {Iter8or}
Applies a function to all elements of the iterator and returns a new Iter8or instance.
Parameters:
Name | Type | Description |
---|---|---|
fn | function | The function to apply to each element of the iterator. |
- Source
Returns:
A new Iter8or instance with the mapped values.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3]);
const mapped = iter.map(x => x * 2);
console.log([...mapped]); // [2, 4, 6]
max(fnopt) → {number}
Finds the maximum value in the iterable.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn | function | <optional> | Optional mapping function to apply before finding the maximum. |
- Source
Returns:
The maximum value.
- Type:
- number
Example
const iter = new Iter8or([1, 2, 3]);
const maximum = iter.max();
console.log(maximum); // 3
min(fnopt) → {number}
Finds the minimum value in the iterable.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn | function | <optional> | Optional mapping function to apply before finding the minimum. |
- Source
Returns:
The minimum value.
- Type:
- number
Example
const iter = new Iter8or([1, 2, 3]);
const minimum = iter.min();
console.log(minimum); // 1
partition(predicate) → {Array}
Partitions the elements of the iterator into two groups based on the predicate function.
Parameters:
Name | Type | Description |
---|---|---|
predicate | function | The function that returns true or false for each element. |
- Source
Returns:
An array of two arrays: [array of elements for which the predicate returned true, array of elements for which the predicate returned false].
- Type:
- Array
Example
const iter = new Iter8or([1, 2, 3, 4, 5]);
const [evens, odds] = iter.partition(x => x % 2 === 0);
console.log(evens); // [2, 4]
console.log(odds); // [1, 3, 5]
reduce(reducer, initialValueopt) → {*}
Reduces the iterable using the provided reducer function.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
reducer | function | The reducer function. | |
initialValue | * | <optional> | The initial value for the reduction. |
- Source
Returns:
The reduced value.
- Type:
- *
Example
const iter = new Iter8or([1, 2, 3]);
const sum = iter.reduce((acc, x) => acc + x, 0);
console.log(sum); // 6
reverse() → {Iter8or}
Reverses the order of the elements in the iterable.
- Source
Returns:
A new instance with the reversed iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3]);
const reversed = iter.reverse();
console.log([...reversed]); // [3, 2, 1]
sum(fnopt) → {number}
Sums the elements in the iterable.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn | function | <optional> | Optional mapping function to apply before summing. |
- Source
Returns:
The sum of the elements.
- Type:
- number
Example
const iter = new Iter8or([1, 2, 3]);
const totalSum = iter.sum();
console.log(totalSum); // 6
take(n) → {Iter8or}
Takes the first `n` elements from the iterable.
Parameters:
Name | Type | Description |
---|---|---|
n | number | The number of elements to take. |
- Source
Returns:
A new instance with the taken iterable.
- Type:
- Iter8or
Example
const iter = new Iter8or([1, 2, 3, 4]);
const taken = iter.take(2);
console.log([...taken]); // [1, 2]
toArray() → {Array}
Collects the elements of the iterator into an array.
- Source
Returns:
An array containing all the elements of the iterator.
- Type:
- Array
Example
const iter = new Iter8or(new Set([1, 2, 3]));
const array = iter.toArray();
console.log(array); // [1, 2, 3]
toMap() → {Map}
Collects the elements of the iterator into a Map. The elements must be pairs [key, value].
- Source
Returns:
A Map containing all the key-value pairs from the iterator.
- Type:
- Map
Example
const iter = new Iter8or([['a', 1], ['b', 2], ['c', 3]]);
const map = iter.toMap();
console.log(map); // Map { 'a' => 1, 'b' => 2, 'c' => 3 }
toObject() → {Object}
Collects the elements of the iterator into an object. The elements must be pairs [key, value].
- Source
Returns:
An object containing all the key-value pairs from the iterator.
- Type:
- Object
Example
const iter = new Iter8or([['a', 1], ['b', 2], ['c', 3]]);
const obj = iter.toObject();
console.log(obj); // { a: 1, b: 2, c: 3 }
toSet() → {Set}
Collects the elements of the iterator into a Set.
- Source
Returns:
A Set containing all unique elements of the iterator.
- Type:
- Set
Example
const iter = new Iter8or([1, 2, 2, 3, 4]);
const set = iter.toSet();
console.log(set); // Set { 1, 2, 3, 4 }
toString() → {string}
Concatenates the elements of the iterator into a string.
- Source
Returns:
A string containing all the elements of the iterator concatenated together.
- Type:
- string
Example
const iter = new Iter8or(['H', 'e', 'l', 'l', 'o']);
const str = iter.toString();
console.log(str); // 'Hello'
(static) next() → {IteratorResult}
Returns the next value in the iteration.
- Source
Returns:
The next value of the iterator.
- Type:
- IteratorResult
Example
const iter = new Iter8or([1, 2, 3]);
console.log(iter.next().value); // Output: 1