Really simple functions for working with iterable types, inspired by F#'s seq module design.
Add package using NPM or yarn
npm i --save iterable-fns
yarn add iterable-fns
You can import the top level modules directly:
import { groupBy } from 'iterable-fns'
Calculating primes lazily with iterators can either be done by calling each of the basic functions:
import { count, initRaw, map, filter } from '../src/iterable-fns'
const range = initRaw({ from: 1, to: 100 })
const mapped = map(range, (x) => ({
x,
factors: filter(initRaw({ from: 1, to: x }), (y) => x % y === 0),
}))
const filtered = filter(mapped, (num) => count(num.factors) === 2)
const primes = map(filtered, (num) => num.x)
or can utilise the chain
methods:
import { init } from 'iterable-fns'
const primes = init({ from: 1, to: 100 })
.map((x) => ({
x,
factors: init({ from: 1, to: x }).filter((y) => x % y === 0),
}))
.filter((num) => num.factors.count() === 2)
.map((num) => num.x)
for (const prime of primes) {
console.log(prime)
}
Grouping numbers into odd and even buckets
import { init, toArray } from 'iterable-fns'
const oddAndEven = init({ from: 1, to: 25 })
.groupBy((i) => (i % 2 === 0 ? 'even' : 'odd'))
.map(([key, values]) => [key, toArray(values)])
yarn test
: Run test suiteyarn start
: Run yarn build
in watch modeyarn test:watch
: Run test suite in interactive watch modeyarn test:prod
: Run linting and generate coverageyarn build
: Generate bundles and typings, create docsyarn lint
: Lints codeyarn commit
: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)Wraps the two given iterables as a single concatenated iterable.
The first iterable.
The second iterable.
Create a new chainable iterator from an existing iterable source.
The input collection.
Applies the given function to each element of the sequence and returns a new sequence comprised of the results for each element where the function returns a value. This can be thought of as doing both a filter and a map at the same time.
The input collection.
A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the source iterable and concatenates all the results.
The input collection.
A function to transform elements of the input collection into collections that are concatenated.
Combines the given collection-of-iterables as a single concatenated iterable.
The input collection.
Returns the number of items in the collection.
The input collection.
Returns a iterable that contains no duplicate entries according to the equality comparisons on the elements. If an element occurs multiple times in the sequence then the later occurrences are discarded.
The input collection.
Returns a iterable that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.
The input collection.
A function that transforms the collection items into comparable keys.
Tests if every element of the collection satisfies the given predicate.
The input collection.
A function to test against each item of the input collection.
Tests if any element of the collection satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Returns a new iterable containing only the elements of the collection for which the given predicate returns true.
The input collection.
A function to test whether each item in the input collection should be included in the output.
Returns the first element for which the given function returns true, otherwise undefined.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true.
The input collection.
A function to test whether an item in the collection should be returned.
Applies a key-generating function to each element of a collection and yields a iterable of unique keys and an array of all elements that have each key.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
A function that transforms an element of the collection into a comparable key.
Generates a new chainable iterable which, when iterated, will return the specified number sequence.
Generates a new chainable iterable which, when iterated, will return the specified number sequence.
The sequence of numbers to generate.
Generates a new iterable which, when iterated, will return the specified number sequence.
The sequence of numbers to generate.
Creates a new iterable whose elements are the results of applying the specified mapping to each of the elements of the source collection.
The input collection.
A function to transform items from the input collection.
Returns the maximum of the values in the collection.
The input collection.
Returns the maximum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a comparable value.
Returns the mean (average) of the values in the collection.
The input collection.
Returns the mean (average) of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a summable value.
Returns the minimum of the values in the collection.
The input collection.
Returns the minimum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a comparable value.
Returns an interable of each element in the input sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.
The input collection
Yields each element of the iterable in reverse order.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
Returns the elements of the iterable after a specified count.
The input collection.
The number of items to skip.
Yields an iterable ordered by the selected key. If no selector is specified, the elements will be compared directly.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
A function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys, descending.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
A function to transform items of the input sequence into comparable keys.
Yields an iterable ordered by the selected key descending. If no selector is specified, the elements will be compared directly.
NOTE: Requires complete iteration of source before yielding first element.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Returns the sum of the values in the collection.
The input collection.
Returns the sum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a summable value.
Returns the elements of the iterable up to a specified count.
The input collection.
The number of items to take.
Creates an array from the source iterable object.
An Iterable objext to convert to an array.
Generated using TypeDoc
Returns the number of items in the collection.
The input collection.
count
length(init(5)) // Returns: 5