# npm

## enumjs

0.0.1 • Public • Published

# enum.js

## Install

``````npm install enumjs
``````

Or from source:

``````git clone git://github.com/sergi/enumjs.git
cd enumjs
``````

## What is it

Enum.js provides easy representation of finite or infinite sequences of elements.

Enumerations are a uniform manner of reading and manipulating the contents of a data structure, or as a simple manner of reading or writing sequences of characters, numbers, strings, etc. from/to files, network connections or other inputs/outputs.

Enumerations are typically computed as needed, which allows the definition and manipulation of huge (possibly infinite) sequences. Manipulating an enumeration is a uniform and often comfortable way of extracting subsequences (For example, using `Enumjs.filter`), converting sequences into other sequences (Using `Enumjs.map`), gathering information (function `Enumjs.scanl` et al) or performing loops (functions Enumjs.iter and Enum.map).

The library comes with support with common "sequenceable" items such as arrays or strings, but it shines when creating custom enumerations.

## Simple examples

In the example below, Enumjs.randInt() creates an infinite enumeration of random numbers. Combined with `Enum.map`, we may turn this into an infinite enumeration of squares of random even numbers:

Similarly, to get an enumeration of 50 random integers, we may use Enum.take:

## Defining custom enumerations

In order to define

One of the advantages of Enum.js is that it can operate over infinite lists of items, as long as the `next()` operation is properly defined. Obviously, the Fibonacci enumeration above is an example of this, and that's why the `count` method returns infinity.

For example, an enumeration for the Fibonacci sequence would look a bit like this:

To list the first 300 Fibonacci numbers we can do:

Enumerations can only go forward. That means that most operations in the enumeration consume the current item (that is, advance the cursor). Enumerations are entirely functional and in case to be based on a particular object like an array, they do not modify the original object, while trying to be as efficient as possible.

Enum.js is inspired by OCaml's `Enum` module.

## Keywords

none

### Install

`npm i enumjs`

### Repository

github.com/sergi/enumjs

0

0.0.1