itiriri
A library built for ES6 iteration protocol.
{ let n = 1; while true n++; } const s = ;console;// 3.1406380562059946
itiriri provides similar functions as JavaScript arrays: filter, slice, map, reduce, every, some etc. and more. The functions are optimized for ES6 iterators and can be chained to perform simple but powerful manipulations over iterables.
Installation
Using npm
$ npm install 'itiriri' --save
Importing
;
Support
The itiriri library can be used with any ES6 compatible runtime.
Usage
itiriri can be used with a build-it type like array, Map, Set, a generator function or a custom iterable.
; const values = 2 0 4 8; const s = ;console; // prints: 4,2,0,1 // prints: 4 2 0 1for const n of s console console; // prints: 7
Deferred execution
JavaScript's array methods like filter, slice and others that return an array create a shallow copy for the result and are executed once called.
itiriri functions that return iterables are not executed unless chained with a function that reduces a value or transforms to a built-in type. The iterable source is iterated only once.
Let's see what happens in the below example.
; { let a b = 0 1; while true a; a b = b a + b; } // Finding first 3 Fibonacci numbers that contain 42const result = ; for const e of result console; // outputs: 514229, 267914296, 7778742049
Step by step:
-
result
is assigned to a itiriri. At this pointnumbers
array is not iterated, the execution is deferred until the result is being iterated. -
filter
method creates an iterator to pipe only numbers passing the predicate.filter
does not buffer elements and only pipes them one-by-one totake
as it is iterated. -
take
pipes only first three elements as it is iterated and breaks. -
for...of
instruction starts iteration and requests elements one at a time.
Due to deferred execution, most of the functions that don't need entire sequence of elements to build an iterator (like filter, map, concat etc.) can be used with infinite iterables (like Fibonacci in the above example). These functions are also optimized to pass elements through and do not buffer them resulting in a more optimized memory usage.
Functions like sort, reverse, shuffle etc. that require entire sequence of elements in order to build an iterator expect to receive a finite iterable.
Benchmarks
Using itiriri
is considerable faster than using array methods when processing large inputs.
In filter-map-slice example arrays of different size are used to filter and map a result of 100 elements:
Array size (N) | array |
itiriri |
---|---|---|
1000 | 111,611 ops/sec ±9.63% (86 runs) | 44,213 ops/sec ±1.92% (88 runs) |
5000 | 18,507 ops/sec ±0.67% (90 runs) | 42,103 ops/sec ±2.63% (84 runs) |
10000 | 8,655 ops/sec ±0.70% (91 runs) | 42,803 ops/sec ±2.20% (86 runs) |
50000 | 1,640 ops/sec ±0.79% (88 runs) | 43,446 ops/sec ±2.17% (88 runs) |
100000 | 848 ops/sec ±0.93% (87 runs) | 43,137 ops/sec ±2.15% (87 runs) |
200000 | 46.38 ops/sec ±0.74% (59 runs) | 42,445 ops/sec ±2.48% (90 runs) |
Using array
methods performance drops significantly for large inputs due to creation of intermediary states for filter
and map
.
Using itiriri
iteration always stops after 100 elements are found, therefore the size of the input doesn't affect the performance.
More benchmarks can be found in /benchmark.
Running Tests
$ npm install$ npm test
Bundling
If you want to use itiriri in the browser, there is a gulp
task that creates a minified file:
$ npm install$ gulp bundle// creates itiriri.min.js file in the root folder
Once you include the itiriri.min.js
file on your page, you can use it as:
<!-- ... -->
Complete list of methods
- average
- concat
- distinct
- entries
- every
- exclude
- fill
- filter
- find
- findIndex
- findLast
- findLastIndex
- first
- flat
- forEach
- groupBy
- groupJoin
- includes
- indexOf
- intersect
- join
- keys
- last
- lastIndexOf
- leftJoin
- length
- map
- max
- min
- nth
- prepend
- reduce
- reduceRight
- reverse
- rightJoin
- shuffle
- skip
- skipWhile
- slice
- some
- sort
- splice
- sum
- take
- takeWhile
- toArray
- toGroups
- toMap
- toSet
- toString
- union
- values
average
Returns the average value.
Syntax
: number;: number;
Parameters
selector
- (optional) a value transformer that accepts two arguments:element
- the current elementindex
- the index of the current element- returns a number that is used for average value calculation
For a sequence with no elements returns undefined
.
Example
; // returns 42 // returns 1.5 // returns undefined
concat
Concatenates a sequence with another one.
Syntax
: IterableQuery<T>;: IterableQuery<T>;
Parameters
other
- (required) a sequence or a value to be concatenated
Example
; // returns [1, 2, 3, 4, 5]
concat
is a deferred method and is executed only when the result sequence is iterated.
distinct
Returns a sequence of unique elements.
Syntax
: IterableQuery<T>;distinct<S> S: IterableQuery<T>;
Parameters
selector
- (optional) a function to get element's value for comparison. Accepts one argument:element
- current element- returns a value to be used for comparison
Example
; ; // returns [1, 42, 3, 4] ; // returns [{value: 1}, {value: 2}]
distinct
is a deferred method and is executed only when the result sequence is iterated.
entries
Returns a sequence of key/value pair for each element and its index.
Syntax
: IterableQuery<number T>;
Example
; ;// returns [[0, 'Alice'], [1, 'Bob'], [2, 'David']]
entries
is a deferred method and is executed only when the result sequence is iterated.
every
Tests whether all the elements pass the predicate.
Syntax
: boolean;
Parameters
predicate
- (required) function to test for each elementelement
- the current elementindex
- the index of the current element- returns
true
orfalse
Example
; ; // returns true; // returns false
exclude
Returns a sequence of elements not contained in a given sequence.
Syntax
exclude<S>others: Iterable<T>: IterableQuery<T>;exclude<S>others: Iterable<T> S: IterableQuery<T>;
Parameters
others
- (required) a sequence of elements to be excludedselector
- (optional) a function to get element's value for comparison, accepts one argument:element
- current element- returns a value to be used for comparison
Example
; ; // returns [2, 8, 2] ; // returns [{id: 1}]
exclude
is a deferred method and is executed only when the result sequence is iterated.
fill
Returns a sequence filled from a start index to an end index with a static value. The end index is not included.
Syntax
: IterableQuery<T>;: IterableQuery<T>;: IterableQuery<T>;
Parameters
value
- (required) value to fillstart
- (optional) start index, defaults to 0end
- (optional) end index, defaults to sequence length
Example
; ; // returns [7, 7, 7, 7, 7]; // returns [1, 2, 3, 7, 7]; // returns [1, 7, 7, 4, 5]
fill
is a deferred method and is executed only when the result sequence is iterated.
filter
Returns a sequence of elements that pass the predicate.
Syntax
: IterableQuery<T>;
Parameters
predicate
- (required) function to test for each element that accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
orfalse
Example
; ; // returns [1, 2]; // returns []
filter
is a deferred method and is executed only when the result sequence is iterated.
find
Finds the first element that satisfies the specified predicate.
Syntax
: T;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
if element satisfies the predicate,false
otherwise
If no element satisfies the predicate, returns undefined
.
Example
; ; // returns 2; // returns undefined
findIndex
Finds the first index at which a given element satisfies the specified predicate.
Syntax
: number;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
if element satisfies the predicate,false
otherwise
If no element satisfies the predicate, returns -1
.
Example
; ; // returns 1; // returns -1
findLast
Finds the last element that satisfies the specified predicate.
Syntax
: T;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
if element satisfies the predicate,false
otherwise
If no element satisfies the predicate, returns undefined
.
Example
; ; // returns 21; // returns undefined
findLastIndex
Finds the last index at which a given element satisfies the specified predicate.
Syntax
: number;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
if element satisfies the predicate,false
otherwise
If not present, returns -1
.
Example
; ; // returns 2; // returns -1
first
Returns the first element in a sequence.
Syntax
: T;
For an empty sequence returns undefined
.
Example
; ; // returns 'a'; // returns undefined
flat
Returns a sequence with all sub-sequences concatenated.
Syntax
flat<S> Iterable<S>: IterableQuery<S>;
Parameters
selector
- (required) a transformation function to map each element to a sequence, accepts two argumentselement
- the current elementindex
- the index of the current element- returns an iterable
Example
;
flat
is a deferred method and is executed only when the result sequence is iterated.
forEach
Runs through every element and applies a given function.
Syntax
: void;
Parameters
action
- (required) function to apply on each element, accepts two arguments:element
- the current elementindex
- the index of the current element
Example
; ;// 1// 2// 3
groupBy
Groups elements by a given key, optionally applying a transformation over each element.
Syntax
groupBy<K> K: IterableQuery<K IterableQuery<T>>; groupBy<K E> K E: IterableQuery<K IterableQuery<E>>;
Parameters
keySelector
- (required) function that provides element's group key, accepts two arguments:element
- the current elementindex
- the index of the current element- returns the group key of current element
valueSelector
- (optional) function to transform values, accepts two arguments:element
- the current elementindex
- the index of the current element- returns a transformation of current element
Example
; const students = name: 'Alice' gender: 'female' name: 'Bob' gender: 'male' name: 'David' gender: 'male'; ;// [['female', ['Alice']], ['male', ['Bob', 'David']]]
groupBy
is a deferred method and is executed only when the result sequence is iterated.
groupJoin
Returns a sequence of correlated elements where each element from the current sequence is matched with zero or more elements from the other sequence.
Syntax
groupJoin<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : IterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from the source sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
rightKeySelector
- (required) function that provides the key of each element from joined sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
joinSelector
- (required) a transformation function to apply on each joined element with group, accepts two arguments:left
- element from the original sourceright
- array of elements from the joined source that have the same key as left element's key
The joinSelector
function is called on each element from the source sequence and the array of matched
elements from the joined sequence.
When an element from the source sequence doesn't match with any of the elements from the joined sequence,
the joinSelector
function will be called with an empty array.
Example
; const books = title: 'Clean code' categoryId: 1 title: 'Code complete' categoryId: 1 title: 'Scrum' categoryId: 2; const categories = id: 1 name: 'CS' id: 2 name: 'Agile'; ;// [// {category: 'CS', books: ['Clean code', 'Code complete']},// {category: 'Agile', books: ['Scrum']}// ]
groupJoin
is a deferred method and is executed only when the result sequence is iterated.
includes
Determines whether the sequence includes a certain element.
Syntax
: boolean;: boolean;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
includes
uses triple equals ===
to compare elements.
Example
; ; // returns true; // returns false
indexOf
Returns the first (zero-based) index at which a given element can be found.
Syntax
: number;: number;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
When an element is not found, returns -1
.
indexOf
uses triple equals ===
to compare elements.
Example
; ; // returns 2; // returns -1
intersect
Returns a set intersection with a given sequence.
Syntax
: IterableQuery<T>;intersect<S>other: Iterable<T> S: IterableQuery<T>;
Parameters
other
- (required) the sequence to intersect withselector
- (optional) a value transformer function to be used for comparisons, accepts one argument:element
- the current element- returns a value used for comparisons
Example
; ; // returns [2, 3] ; // returns [{id: 1, name: 'Alice'}]
intersect
is a deferred method and is executed only when the result sequence is iterated.
join
Returns a sequence of correlated elements transformation that match a given key.
Syntax
join<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : IterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from source sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
rightKeySelector
- (required) function that provides the key of each element from joined sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
joinSelector
- (required) a transformation function to apply on each matched tuple, accepts two arguments:left
- element from the source sequenceright
- element from the joined sequence- returns a new result
The join
method works as an sql inner join.
Example
; ;// returns ['2-2', '3-3'] ;// returns [{country: 'US', code: '+1'}]
join
is a deferred method and is executed only when the result sequence is iterated.
keys
Returns a sequence of keys for each index in the source sequence.
Syntax
: IterableQuery<number>;
Example
; ; // returns [0, 1, 2]
keys
is a deferred method and is executed only when the result sequence is iterated.
last
Returns the last element in a sequence.
Syntax
: T;
For an empty sequence returns undefined
.
Example
; ; // returns 'c'; // returns undefined
lastIndexOf
Returns the last index at which a given element can be found.
Syntax
: number;: number;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
When an element is not found, returns -1
.
lastIndexOf
uses triple equals ===
to compare elements.
Example
; ; // returns 2; // returns -1
leftJoin
Returns a sequence of correlated elements transformation that match a given key.
Syntax
leftJoin<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : IterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from source sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
rightKeySelector
- (required) function that provides the key of each element from joined sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
joinSelector
- (required) a transformation function to apply on each matched tuple, accepts two arguments:left
- element from the source sequenceright
- element from the joined sequence, orundefined
if no match was found- returns element's key
The leftJoin
method works as an sql left join.
When an element from the left sequence doesn't match with any of the elements from the right sequence,
the joinSelector
function is called with an undefined
right value.
Example
; ;// returns ['1-#', '2-2', '2-2', '3-3'] ;// returns [// {book: 'History', owner: 'Eve'},// {book: 'Math', owner: 'Bob'},// {book: 'Art', owner: '--'}]
leftJoin
is a deferred method and is executed only when the result sequence is iterated.
length
Returns the number of elements in a sequence.
Syntax
: number;: number;
Parameters
predicate
- (optional) a function to count only the elements that match the predicate, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
orfalse
Example
; length; // returns 5length elem > 2; // returns 3
map
Returns a sequence of transformed values.
Syntax
map<S> S: IterableQuery<S>;
Parameters
selector
- (required) a value transformer function to apply to each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns a new value
Example
; ; // returns [10, 20, 30]
map
is a deferred method and is executed only when the result sequence is iterated.
max
Returns the maximum element in a sequence.
Syntax
: T;: T;
Parameters
compareFn
- (optional) a comparer function that compares two elements from a sequence and returns:-1
whena
is less thanb
1
whena
is greaterb
0
whena
equals tob
If sequence is empty, returns undefined
.
Example
; ; // returns 3; // returns undefined; // returns 3
min
Returns the minimum element in a sequence.
Syntax
: number;: T;
Parameters
compareFn
- (optional) a comparer function that compares two elements from a sequence and returns:-1
whena
is less thanb
1
whena
is greaterb
0
whena
equals tob
If sequence is empty, returns undefined
.
Example
; ; // returns 1; // returns undefined; // returns 11
nth
Returns the element at a specified index.
Syntax
: T;
Parameters
index
- (required) zero based index at which to get the element
For a negative index returns the element from the end of the sequence.
If index is out of the range, returns undefined
.
Example
; // returns 'c' // returns 'd' // returns undefined
prepend
Returns a sequence with given elements at the beginning.
Syntax
: IterableQuery<T>;: IterableQuery<T>;
Parameters
other
- (required) the sequence or element to be added at the beginning
Example
; ; // returns [1, 2, 3, 9, 10]
prepend
is a deferred method and is executed only when the result sequence is iterated.
reduce
Applies a function against an accumulator and each element (from left to right) to reduce it to a single value.
Syntax
: T; reduce<S> S initialValue: S : S;
Parameters
callback
- (required) function to execute on each element in the sequence, taking three argumentsaccumulator
the accumulator accumulates the callback's return values;current
the current element being processed;currentIndex
the index of the current element being processed;
initialValue
- (optional) value to use as the first argument to the first call of thecallback
Calling reduce
on an empty sequence without an initial value throws an error.
Example
; ; // returns 42; // returns 16
reduceRight
Applies a function against an accumulator and each element (from right to left) to reduce it to a single value.
Syntax
: T; reduceRight<S> S initialValue: S : S;
Parameters
callback
- (required) function to execute on each element in the sequence, taking three argumentsaccumulator
the accumulator accumulates the callback's return values;current
the current element being processed;currentIndex
the index of the current element being processed;
initialValue
- (optional) value to use as the first argument to the first call of thecallback
Calling reduceRight
on an empty sequence without an initial value throws an error.
Example
; ; // returns 42; // returns [3, 2, 1]
reverse
Returns a sequence of elements in a reversed order.
Syntax
: IterableQuery<T>;
Example
; ; // returns [3, 2, 1]
reverse
is a deferred method and is executed only when the result sequence is iterated.
rightJoin
Returns a sequence of correlated elements transformation that match a given key.
Syntax
rightJoin<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : IterableQuery<TResult>;
Parameters
other
- (required) sequence to joinrightKeySelector
- (required) function that provides the key of each element from joined sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
leftKeySelector
- (required) function that provides the key of each element from source sequence, accepts two arguments:element
- the current elementindex
- the index of the current element- returns element's key
joinSelector
- (required) a transformation function to apply on each matched tuple, accepts two arguments:right
- element from the joined sequenceleft
- element from the source sequence, orundefined
if no match found- returns new result
The rightJoin
method works as an sql right join.
When an element from the right sequence doesn't match with any of the elements from the left sequence,
the rightJoin
function is called with an undefined
left value.
Example
; ;// returns ['2-2', '3-3', '#-4', '2-2'] ;// returns [// {student: 'Alice', book: '--'},// {student: 'Bob', book: 'Math'},// {student: 'Eve', book: 'History'}]
rightJoin
is a deferred method and is executed only when the result sequence is iterated.
shuffle
Returns the sequence of elements in a random order.
Syntax
: IterableQuery<T>;
This method is implemented using Fisher–Yates
algorithm for generating the random permutation. Math.rand()
is used to generate random numbers.
Example
; ;// returns a random permutation of the same elements// like: [2, 5, 3, 1, 4]
shuffle
is a deferred method and is executed only when the result sequence is iterated.
skip
Skips the specified number of elements from the beginning of sequence and returns the remaining ones.
Syntax
: IterableQuery<T>;
Parameters
count
- (required) number of elements to skip
When count is greater than actual number of elements, results in an empty sequence.
Accepts also a negative count, in which case skips the elements from the end of the sequence.
Example
; ; // [3, 4, 5]; // []; // [1, 2, 3]
skip
is a deferred method and is executed only when the result sequence is iterated.
skipWhile
Skip elements while they satisfy the predicate.
Syntax
skipWhile<T> boolean: IterableQuery<T>;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element
Example
; ; // returns []; // returns [1, 2, 3]; // returns [3]; // returns [1, 2, 3]
skipWhile
is a deferred method and is executed only when the result sequence is iterated.
slice
Returns a sequence that represents the range of elements from start to end.
Syntax
: IterableQuery<T>;: IterableQuery<T>;
Parameters
start
- (required) zero-based index at which to begin extractionend
- (optional) zero-based index before which to end extraction
The end
index is not included in the result.
Example
; ; // returns [2, 3]
slice
is a deferred method and is executed only when the result sequence is iterated.
some
Tests whether at least one element passes the predicate.
Syntax
: boolean;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns
true
orfalse
Example
; ; // returns true; // returns false
sort
Returns a sequence of sorted elements.
Syntax
: IterableQuery<T>;: IterableQuery<T>;
Parameters
compareFn
- (optional) a comparer function that compares two elements from a sequence and returns:-1
whena
is less thanb
1
whena
is greaterb
0
whena
equals tob
This method fallbacks to native JavaScript array sort method.
Example
; ; // returns [0, 4, 7, 9, 12] ascore - bscore);// returns [// {score: 0, value: 'b'},// {score: 1, value: 'a'},// {score: 2, value: 'c'}]
sort
is a deferred method and is executed only when the result sequence is iterated.
splice
Returns a sequence that skips elements and/or adds new elements.
Syntax
: IterableQuery<T>;
Parameters
start
- (required) index at which to start changing the sequencedeleteCount
- (optional) an integer indicating the number of original elements to skipitems
- (optional) elements to add to the sequence
Example
; ;// returns ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'] ;// returns ['angel', 'clown', 'drum', 'sturgeon']
splice
is a deferred method and is executed only when the result sequence is iterated.
sum
Returns the sum of all elements.
Syntax
: number;: number;
Parameters
selector
- (optional) a value transformer function to apply to each element, accepts two arguments:element
- the current elementindex
- the index of the current element- returns a value to be used for sum calculation
Optionally, a function can be provided to apply a transformation and map each element to a value.
Example
; ; // returns 6; // returns 3
take
Returns a specified number of elements from the beginning of sequence.
Syntax
: IterableQuery<T>;
Parameters
count
- (required) number of elements to take
If a negative count is specified, returns elements from the end of the sequence.
Example
; ; // returns [1, 2]; // returns [2, 3]; // returns [1, 2, 3]
take
is a deferred method and is executed only when the result sequence is iterated.
takeWhile
Returns elements while they satisfy the predicate.
Syntax
takeWhile<T> boolean: IterableQuery<T>;
Parameters
predicate
- (required) function to test for each element, accepts two arguments:element
- the current elementindex
- the index of the current element
Example
; ; // returns [1, 2, 3]; // returns []; // returns [1, 2]; // returns []
takeWhile
is a deferred method and is executed only when the result sequence is iterated.
toArray
Creates an array copy of the sequence.
Syntax
: T;toArray<S> S: S;
Parameters
selector
- (optional) a value transformer function to apply to each element
When providing a selector function, creates an array of values returned by applying the function on each element.
Example
; ; // returns [1, 2, 3]; // returns [1, 2]
toGroups
Creates a map of element groups by a given key.
Syntax
toGroups<M> M: Map<M T>; toGroups<M N> M N: Map<M N>;
Parameters
keySelector
- (required) a transformer function to apply to each element to get its key, accepts two arguments:element
- the current elementindex
- the index of the current element
valueSelector
- (optional) a transformer function to apply to each element, accepts two arguments:element
- the current elementindex
- the index of the current element
Method toGroups
creates a JavaScript Map
of key-value pairs where each key is the result from keySelector
and value is an array of elements
(or the result of applying valueSelector
on each element) from the original sequence for which the key is the same.
Example
; ;// returns Map {0 => [14, 4], 1 => [1, 7, 9]} ;// returns Map {'female' => ['Alice'], 'male' => ['Bob', 'David']}
toMap
Creates a map of elements by a given key.
Syntax
toMap<M> M: Map<M T>; toMap<M N> M N: Map<M N>;
Parameters
keySelector
- (required) a transformer function to apply to each element to get its key, accepts two arguments:element
- the current elementindex
- the index of the current element
valueSelector
- (optional) a transformer function to apply to each element, accepts two arguments:element
- the current elementindex
- the index of the current element
Method toMap
returns a JavaScript Map
of key-value pairs where each key is the result from keySelector
and value is the element
(or the result of applying valueSelector
on the element) that corresponds to the key.
If the sequence contains two elements with the same key, method toMap
throws an error.
Example
; ;// returns Map {97 => 'a', 98 => 'b', 99 => 'c'} ;// returns Map {97 => 'A', 98 => 'B', 99 => 'C'} ;// throws an Error
toSet
Creates a set of elements.
Syntax
: Set<T>;toSet<S> S: Set<S>;
Parameters
selector
- (optional) a transformer function to apply to each element to get its value, accepts two arguments:element
- the current elementindex
- the index of the current element
Method toSet
returns a JavaScript Set
of the original elements in the sequence, or their transformation when a selector
is provided.
Example
; ; // returns Set {1, 2, 3} ; // returns Set {1, 2}
toString
Returns a string representing the specified sequence and its elements.
Syntax
: string;
Method toString
calls .toString()
function on each element and joins the result by ,
.
Example
; ; // returns 1,2,3; // returns 1,,3; // returns [object Object],[object Object]
union
Returns a set union with a given sequence.
Syntax
: IterableQuery<T>;union<S>other: Iterable<T> S: IterableQuery<T>;
Parameters
other
- (required) the sequence to join withselector
- (optional) a value transformer function to be used for comparisons, accepts one argument:element
- the current element
Example
; ; // returns [1, 2, 3, 4] ;// returns [// {id: 1, name: 'Alice'},// {id: 2, name: 'Bob'},// {id: 3, name: 'David'}]
union
is a deferred method and is executed only when the result sequence is iterated.
values
Returns a sequence of values for each index in the source sequence.
Syntax
: IterableQuery<T>;
Example
; ; // returns [1, 2, 3]
values
is a deferred method and is executed only when the result sequence is iterated.