Enumeration over abstract collection of elements
npm install enumjs
Or from source:
git clone git://github.com/sergi/enumjs.git cd enumjs npm link
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
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.
var seq = EnumfromArray1 2 3 4 5 6;// [1, 2, 3, 4, 5, 6]// ^// Cursor is at position 0seqnext; // returns 1// [1, 2, 3, 4, 5, 6]// ^// Cursor is at position 1// Iterate over the values and print them. This will consume the whole// enumerationseqiterconsole.log;// [1, 2, 3, 4, 5, 6]// ^// Cursor is at position 5. The enumeration is now depleted.var seq2 = EnumfromArray1 2 3 4 5 6;varreturn e % 2 === 0;;var el = seq2findeven; // Returns 2// [1, 2, 3, 4, 5, 6]// ^// Cursor is at position 1
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:
EnumrandIntfilter return e % 2 == 0map return e * e
Similarly, to get an enumeration of 50 random integers, we may use Enum.take:
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
For example, an enumeration for the Fibonacci sequence would look a bit like this:
var fibo =var n;var data = thisdata;if datacount < 2n = datacount;elsen = datacache0 + datacache1;datacachepushn;datacache = datacacheslice-2; // We only cache last 2 valuesdatacount++;return n;return Infinity;var _clone = fibo;_clonedatacount = thisdatacount;_clonedatacache = thiscache0 thiscache1;return clone;// Initial extra properties for this enumerationcount: 0 cache:;
To list the first 300 Fibonacci numbers we can do:
for var i = 0; i < 300; i++console.logfiboget;
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
Copyright 2012 Sergi Mansilla.