Returns: Seq
- A new sequence.
Throws:
TypeError
if the existing sequence is null or undefined when invoke via call/apply/bind.
Param | Description |
---|---|
...others | Iterable, iterator, generator function, array-like object, or single value to concatenate into new sequence. If omitted, the method returns the existing sequence. |
Example
const seq = Seq.of(1, 2, 3);
const result = seq.append([4, 5, 6], { seven: 7 }, [[8], [9]], [[[10], [11]]]);
console.log(result.toArray());
// => [ 1, 2, 3, 4, 5, 6, { seven: 7 }, [ 8 ], [ 9 ], [ [ 10 ], [ 11 ] ] ]
Applies a key-generating function to each element of a sequence and returns a sequence yielding unique keys and their number of occurrences in the original sequence. This method should not be used with large or infinite sequences.
Returns: Seq
- The result sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or projection is a generator function or not a function.
Param | Description |
---|---|
projection | A function transforming each item of the input sequence into a key to be compared against the others. |
Example
class Person {
constructor(name) {
this.Name = name;
}
}
const mrA = new Person('A', 20);
const mrB = new Person('B', 15);
const seq = Seq.of(mrA, mrA, mrB);
const nameCounts = seq.countBy(x => x.Name).toArray();
console.log(nameCounts)
// => [['A', 2], ['B', 1]]
Returns: Seq
- An empty sequence.
Example
const emptySeq = Seq.empty();
console.log(emptySeq.toArray());
// => []
Returns: boolean
- true if the callback function returns a truthy value for at least one element in the sequence. Otherwise, false.
Throws:
- If the source sequence is null or undefined when invoke via call/apply/bind; or predicate is a generator function or not a function.
Param | Type | Description |
---|---|---|
predicate | function |
A function to test on the elements of the seq. |
Example
const seq = Seq.of(1,2,3,4,5);
const result = seq.exists(x => x % 2 === 0);
console.log(result);
// => true
Returns: Seq
- The result sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or predicate is a generator function or not a function.
Param | Type | Description |
---|---|---|
predicate | function |
A function to test whether each item in the input sequence should be included in the output. |
Example
const seq = Seq.of(1,2,3,4,5).filter(x => x % 2 === 0);
console.log([...seq]);
// => [2, 4]
Returns: The last element in the sequence that passes the test in the specified predicate function.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or predicate is a generator function or not a function.
Param | Description |
---|---|
predicate | A function to test each element for a condition. |
Example
const seq = Seq.of(1, 2, 42, 323, 423, 32, 23, 10, 11);
console.log(seq.findBack(x => x % 2 === 0));
// => 10
Returns: boolean
- true if the callback function returns a truthy value for at least one element in the sequence. Otherwise, false.
Throws:
- If the source sequence is null or undefined; or predicate is a generator function or not a function.
Param | Type | Description |
---|---|---|
predicate | function |
A function to test on the elements of the seq. |
Example
const seq = Seq.of(2, 4, 5, 8);
const result = seq.exists(x => x % 2 === 0);
console.log(result);
// => true
Returns: Seq
- The return sequence.
Throws:
TypeError
if source doesn't conform iterable protocol.
Param | Description |
---|---|
source | Anything that implemented iterable protocol. |
Returns the first element of the sequence.
Returns: The first element of the sequence.
Example
@exception {TypeError} If the source sequence is null or undefined when invoke via call/apply/bind.
const seq = Seq.of(1, 2, 3, 4, 5);
console.log(seq.head());
// => 1
Returns: Seq
- The result sequence.
Throws:
TypeError
if count is a negative number; or initializer is a generator function or not a function.
Param | Type | Description |
---|---|---|
count | Number |
The maximum number of items to generate for the sequence. |
initializer | function |
A function that generates an item in the sequence from a given index. |
Example
const fiveNums = Seq.init(5, x => x * 2);
console.log(fiveNums.toArray());
// => [0, 2, 4, 6, 8]
Returns: Seq
- The result sequence.
Throws:
TypeError
if initializer a generator function or not a function.
Param | Type | Description |
---|---|---|
initializer | function |
A function that generates an item in the sequence from a given index. |
Example
const seq = Seq.initInfinite(x => x * 2);
const first5Nums = seq.take(5);
console.log(first5Nums.toArray());
// => [0, 2, 4, 6, 8]
Returns: boolean
- True if the sequence is empty; false otherwise.
Throws:
TypeError
if the existing sequence is null or undefined when invoke via call/apply/bind.
Example
console.log(Seq.empty().isEmpty());
// => true
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or predicate is a generator function or not a function.
Param | Type | Description |
---|---|---|
callback | function |
A function to apply to each element of the sequence. |
Example
const seq = Seq.of(1, 2, 3, 4, 5);
seq.iter(x => console.log(x));
// => 1
// => 2
// => 3
// => 4
// => 5
Returns: The value at the last position in the source sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind.
Example
const seq = Seq.range(0,10);
const last = seq.last();
console.log(last);
// => 9
Returns: number
- The length of the sequence.
Throws:
TypeError
if the existing sequence is null or undefined when invoke via call/apply/bind.
Example
const seq = Seq.of(1,2,3);
console.log(seq.length);
// => 3;
Returns: Seq
- Return the new mapped sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or callback is a generator function or not a function.
Param | Type | Description |
---|---|---|
callback | function |
The function invoked on each item. |
Example
const seq = Seq.of(1,2,3,4,5).map(x => x * x);
console.log([...seq]);
// => [1, 4, 9, 16, 25]
const seq = Seq.of(1,2,3,4,5).map((item, index) => [index, item]);
console.log([...seq]);
// => [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
Returns: Seq
- A new sequence.
Param | Description |
---|---|
args | Elements used to create the sequence. |
Example
const nums = Seq.of(1,2, { 'three' : 3}, [4], '5', 0b110);
console.log(nums);
// => [ 1, 2, { three: 3 }, [ 4 ], '5', 6 ]
Returns: Seq
- A new sequence.
Throws:
TypeError
if the existing sequence is null or undefined when invoke via call/apply/bind.
Param | Description |
---|---|
...others | Iterable, iterator, generator function, array-like object, or single value to prepend into new sequence. If omitted, the method returns the existing sequence. |
Example
const seq = Seq.of(4,5);
const prepended = seq.prepend([1,2], { three : 3 });
console.log(prepended.toArray());
// => [ 1, 2, { three: 3 }, 4, 5]
Returns: Seq
- The result sequence.
Throws:
TypeError
if 'begin', 'end', and 'step' are not finite numbers when passed.
Param | Type | Default | Description |
---|---|---|---|
[begin] | number |
0 |
The first number of the sequence |
end | number |
The end of the range of numbers. It won't include in the result sequence. | |
[step] | number |
1 |
The value to increment or decrement by. |
Example
console.log(Seq.range(5).toArray());
// => [ 0, 1, 2, 3, 4 ]
console.log(Seq.range(-5).toArray());
// => [ 0, -1, -2, -3, -4 ]
console.log(Seq.range(1,5).toArray());
// => [ 1, 2, 3, 4 ]
console.log(Seq.range(0, 20, 5).toArray());
// => [ 0, 5, 10, 15 ]
console.log(Seq.range(0, -5, -1).toArray());
// => [ 0, -1, -2, -3, -4 ]
console.log(Seq.range(0).toArray());
// => []
console.log(Seq.range(10, -5, -1).toArray());
// => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4 ]
console.log(Seq.range(-1, -2, -5).toArray());
// => [-1]
Returns: A single value that results from reduction.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or reducer is a generator function or not a function; or when the source sequence is empty and initial value is not provided.
Param | Type | Description |
---|---|---|
reducer | function |
It takes two arguments - (accumulator, currentValue). if initialValue is provided, the value will be used as the first argument in the reducer. Otherwise; the first element of the sequence will be used as the first argument. |
Example
const result = Seq.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(result);
// => 55
const result = Seq.of([0, 1], [2, 3], [4, 5]).reduce((accumulator, currentValue) => accumulator.append(currentValue) , []);
console.log(result);
// => [0, 1, 2, 3, 4, 5]
Returns: Seq
- The generated sequence.
Throws:
TypeError
if count is a negative number.
Param | Type | Description |
---|---|---|
count | Number |
The number of elements to replicate. |
initial | The value to replicate. |
Example
const seq = Seq.replicate(5, 1);
console.log(seq.toArray());
// => [1, 1, 1, 1, 1]
Returns: Seq
- The reversed sequence.
Throws:
TypeError
if the source sequence is null or undefined when invoke via call/apply/bind.
Example
const seq = Seq.of(0, 1, 2, 3, 4);
console.log(seq.reverse().toArray());
// => [4, 3, 2, 1, 0]
Returns a new sequence containing all the elements of the existing sequence except the first.
Returns: Seq
- The result sequence.
Example
@exception {TypeError} If the source sequence is null or undefined when invoke via call/apply/bind.
const seq = Seq.of(1, 2, 3, 4, 5);
console.log(seq.tail().toArray());
// => [2, 3, 4, 5]
Returns: Seq
- The result sequence.
Throws:
TypeError
if the source sequence is null or undefined when invoke via call/apply/bind; or count is a negative number.
Param | Description |
---|---|
count | The number of items to take. |
Example
const seq = Seq.of(1,2,3,4,5);
const taken = seq.take(2);
console.log([...taken]);
// => [1, 2]
Returns: Seq
- The result sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind; or predicate is a generator function or not a function.
Param | Type | Description |
---|---|---|
predicate | function |
A function that evaluates to false when no more items should be returned. |
Example
const seq = Seq.from([1, 2, 3, 1, 2, 3]);
const result = seq.takeWhile(x => x < 3).toArray();
console.log(result);
=> [1, 2]
Returns: Array
- The result array.
Throws:
TypeError
if the existing sequence is null or undefined when invoke via call/apply/bind.
Example
const seq = Seq.of(1,2,3,4);
const arr = seq.toArray();
console.log(arr);
// => [1,2,3,4]
Returns: A string representing the elements of the sequence.
Throws:
TypeError
If the source sequence is null or undefined when invoke via call/apply/bind.
Example
console.log(Seq.of(1,2,3).toString());
// => "1, 2, 3"