Really simple functions for working with async iterable types, inspired by F#'s collection modules design.
- Full type-safety with TypeScript
- Zero dependency
- Pure functions
Add package using NPM or yarn
npm i --save async-iterable-fns
yarn add async-iterable-fns
You can import the top level modules directly:
import { groupBy } from 'async-iterable-fns'
Calculating primes lazily with iterators can either be done by calling each of the basic functions:
import { count, initRaw, map, filter } from 'async-iterable-fns'
const findPrimes = async () => {
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, async (num) => (await count(num.factors)) === 2)
const primes = map(filtered, (num) => num.x)
return await toArray(primes)
}
or can utilise the chainable methods:
import { init } from 'async-iterable-fns'
const findPrimesChained = async () => {
const primes = init({ from: 1, to: 100 })
.map((x) => ({
x,
factors: init({ from: 1, to: x }).filter((y) => x % y === 0),
}))
.filter(async (num) => (await num.factors.count()) === 2)
.map((num) => num.x)
for await (const prime of primes) {
console.log(prime)
}
}
yarn test
: Run test suiteyarn start
: Runyarn 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 😉)