Collection Functions
Really simple functions for working with built-in collection types, inspired by F#'s collection modules design.
Features
- Modules for iterables, arrays, maps and sets.
- Composable functional design with full type-safety when used with TypeScript.
Installation
Add package using NPM or yarn
npm i --save collection-fns
yarn add collection-fns
You can import the top level modules directly:
Additionally, you can import the specific module functions from dist/lib/[module]
:
Examples
Calculating primes lazily with iterators:
; const primes = for const prime of primes console
Grouping numbers into odd and even buckets
; const oddAndEven =
This works by use of partial application, however all functional can also be called directly such as:
Iterables)
Pipes
The pipe()
function is a stand-in until the pipe (|>
) operator gets implemented in ESNext.
For pipes of up to 26 steps, the multi-argument overloads can be used where the first argument is the initial value, and all following arguments are functions take the result of the step before and returning a new result. The result from the final step is then returned as the result of the pipe.
For longer pipes there is an alternative syntax:
- The pipe is started by passing
pipe(...)
a single initial value. - Each
.then(...)
step in a pipe takes a callback that is passed the value from the previous step. - At the end of the pipe, access the
.result
property to get the value returned from the last step.
Taking the example from the proposal linked above:
{ return str + ", " + str;} { return str0 + str;} { return str + '!';}
The following statements are equivalent:
let result = ;result // Prints: "Hello, hello!" let result = result // Prints: "Hello, hello!" let result = resultresult // Prints: "Hello, hello!"
NPM scripts
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 😉)