Literable
A library for work with iterators inspired in .Net LINQ
Install
npm install literable
Quick examples
const Literable = require('literable');
const natural = new Literable(function*() {
let current = 1;
while (true) yield current++;
});
const pairsSum = natural.take(1000)
.where(num => num % 2)
.sum();
console.log(`The sum of all pair natural numbers below 1001 is ${pairsSum}`);
// The sum of all pair natural numbers below 1001 is 250000
Api
- aggregate: Applies an accumulator function over a sequence.
- all: Determines whether all elements of a sequence satisfy a condition.
- any: Determines whether any element of a sequence exists or satisfies a condition.
- append: Appends values to the end of the sequence.
- average: Computes the average of a sequence of numeric values.
- concat: Concatenates the sequences.
- contains: Determines whether a sequence contains a specified element.
- count: Returns the number of elements in a sequence.
- defaultIfEmpty: Returns the elements of a sequence, or a default value if the sequence is empty.
- distinct: Returns distinct elements from a sequence.
- elementAt: Returns the element at a specified index in a sequence.
- elementAtOrDefault: Returns the element at a specified index in a sequence or a default value if the index is out of range.
- except: Produces the set difference of two sequences.
- findIndex: Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within the sequence or a portion of it. This method returns -1 if an item that matches the conditions is not found.
- findLastIndex: Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within the sequence or a portion of it.
- first: Returns the first element of a sequence.
- firstOrDefault: Returns the first element of a sequence, or a default value if no element is found.
- flat: Projects each element of a sequence to a sequence and all sub-sequence elements concatenated into it recursively and flattens the resulting sequences into one sequence.
- forEach: Performs the specified action on each element of the sequence.
- groupBy: Groups the elements of a sequence.
- groupJoin: Correlates the elements of two sequences based on key equality, and groups the results.
- indexOf: Searches for the specified element and returns the index of its first occurrence in a sequence or in a range of elements in the sequence.
- intersect: Produces the set intersection of two sequences.
- join: Correlates the elements of two sequences based on matching keys.
- last: Returns the last element of a sequence.
- lastIndexOf: Returns the index of the last occurrence of a value in a sequence or in a portion of the sequence.
- lastOrDefault: Returns the last element of a sequence, or a default value if no element is found.
- max: Returns the maximum value in a sequence of values.
- maxElement: Returns the maximum value in a sequence of values.
- min: Returns the minimum value in a sequence of values.
- minElement: Returns the minimum value in a sequence of values.
- ofType: Filters the elements of an sequence based on a specified type.
- orderBy: Sorts the elements of a sequence in ascending order.
- orderByDescending: Sorts the elements of a sequence in descending order.
- prepend: Adds values to the beginning of the sequence.
- reverse: Inverts the order of the elements in a sequence.
- select: Projects each element of a sequence into a new form.
- selectMany: Projects each element of a sequence to a sequence and flattens the resulting sequences into one sequence.
- sequenceEqual: Determines whether two sequences are equal according to an equality comparer.
- single: Returns a single, specific element of a sequence.
- singleOrDefault: Returns a single, specific element of a sequence, or a default value if that element is not found.
- skip: Bypasses a specified number of elements in a sequence and then returns the remaining elements.
- skipLast: Bypasses a specified number of elements in the end of a sequence and then returns the remaining elements.
- skipWhile: Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
- sum: Computes the sum of a sequence of numeric values.
- take: Returns a specified number of contiguous elements from the start of a sequence.
- takeLast: Returns a specified number of contiguous elements from the end of a sequence.
- take-while: Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.
- thenBy: Performs a subsequent ordering of the elements in a sequence in ascending order.
- thenByDescending: Performs a subsequent ordering of the elements in a sequence in descending order.
- toArray: Creates an array from a sequence.
- traverseBreadthFirst: Traverse over elements exploring all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
- traverseDepthFirst: Traverse over elements exploring as far as possible along each branch before backtracking.
- union: Produces the set union of two sequences.
- where: Filters a sequence of values based on a predicate.
- zip: Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.