mech-emit
An emitter
returns the next element from a source for each invocation of the emitter
.
That source could be an array
, hashtable
, open socket
, list
, a range
(like 1..6), etc. Emitter don't care. It just emits the next thing.
Example Usage
Emitters Library
This library contains a bunch of emitters.
See Mechanism Home for more information and other libraries.
Supported Mechanisms
- emitFromArr - Given an array,
emitFromArr
emits a single element at a time from the array. - emitFromRange - Given a range,
emitFromRange
emits a single element from that range.
// TODO: Add emitters like
- emitSin - Emits a sine wave with range 0 to 1 of a frequency. Returned value is relative to the last emission.
- emitCos - Emits a cosine wave with range 0 to 1 of a frequency. Returned value is relative to the last emission.
- emitTriangle - Emits a triangle wave with range 0 to 1 of a frequency. Returned value is relative to the last emission.
- emitFib - Emit the Fibonacci sequence.
emitFromArr
Given an array, emitFromArr
will continue to emit each element in the array. If no more elements are left, emitFromArr
will emit undefined
. If repeat is true, emitFromArr
will start emitting the array from the beginning: never emitting undefined
.
var em = m;emgo // 1st time returns 3emgo // 2nd time returns 3emgo // 3rd time returns 8emgo // 4th time returns undefined// then undefined forever
var em = m;emgo // 1st time returns 7emgo // 2nd time returns 2emgo // 3rd time returns 7emgo // 4th time returns 2// ... forever
When set to true, the emitter
will emit forever. Be careful when using with emitters mechanisms like loop and map.
emitFromRange
Given a min
, max
and (increment-)by
, emitFromRange
will emit the range. At the end of the range, emitFromRange
will emit undefined
. If repeat
is true
, emitFromRange
will start emitting the range from the beginning.
var em = m;emgo // 1st time returns 2emgo // 2nd time returns 4emgo // 3rd time returns 6emgo // 4th time returns undefined// then undefined forever
var em = m;emgo // 1st time returns 8emgo // 2nd time returns 7emgo // 3rd time returns 6emgo // 4th time returns 5emgo // 5th time returns undefined// then undefined forever
And, you can emit from a HUGE range:
var em = memgo // 1st time return 0emgo // 2nd time returns 1000emgo // 3rd time returns 2000// then foreverish
And let's make our by
an emitter
:
var em = m;emgo; // returns 1emgo; // returns 4emgo; // returns 3emgo; // returns 7emgo; // returns 10// forever repeating when we hit 300
Infinite Sized Maps!
Let's say you want to apply an algorithm to a range of data that has no bounds.
Using mechanisms, this is really easy:
var em = m;emgo // 1st time return 2emgo // 2nd time returns 1002emgo // 3rd time returns 2002// then foreverish
Passing this into a mapping mechanism:
var emap = m; emapgo // limited to 1000 elements
This will not loop forever because, internally, emitFromRange
limits to the number of items map
will attempt to traverse to 1000. This number can be set.
But really, who needs a map when you can turn any algorithm into a "mapping" algorithm by simply adding emitters to the algorithm.
Add
can become an add mapping algorithm by providing an emitter to one of adds
operands.
Interface Change Warning
TODO: We may change the interface of emit's optional repeat parameter. We may make it:
repeat
- will cause the emitter to repeat when it reaches the end of the items to emit.reverse
- will cause the emitter to reverse what it emits starting at the end of the array.repeat-reverse
- will cause the emitter to flip flop between emitting forward and then reverse.
Setup
Using In Your Projects
Change directory to your node project.
$ npm install --save mech-emit
Development
Get Involved!
There are a lot of core mechanisms just waiting to be created. Many of them can be created in a few hours including in-depth tests. Clone mech-library to get started!
Setup
Install:
$ npm install
Continuous test:
$ gulp
Test:
$ gulp webtests
Test Server
Read documentation in gulpfile.js to see how to setup automated web testing.
$ gulp webserver