Generating Sequences Using Emitters by Eric Hosick (erichosick@gmail.com) Published

Introduction

In this post, we will show how we can generate different sequences by using emitters.

Hay! Emitters are framework agnostic. You can use them in your Javascript programs by ‘npm installing’ emitters.

Let’s define a sequence that is the addition of two emitters limited to the number of elements you enter (as long as it is less than 100,001):

m.map( m.add ( m.emitFromRange(2, Infinity, 2), // positive integers m.emitFromRange(1, Infinity, 2) // negative integers ), m.min( m.propGet("value", m.elemById("inp05")), 100000 ) ); // Quick Documentation: // m.map(algorithm, max) - Invokes an algorithm up to max number of times aggregating the results. // m.add(left, right) - add left to right. // m.min(arg1, arg2 ... argN) - find min of arg1, arg2 ... argN. // m.emitFromRange(min,max,by) - On each invocation, emit an element starting at min upto max intcremented-by by. // m.propGet(prop,item) - return the value located in item.prop. // m.elemById(id) - return the dom element with id.

Try it out:



enter maximum elements in the sequence



Go to the console (in chrome: View->Developer->Javascript Console) and checkout the library ‘m’: just type m return to see what is available).

Programming Emitters

Let’s start with simple emitters and build up to more complex ones.

Basic Emitter

Let’s build a basic emitter:

var emiter = m.emitFromRange(2, Infinity, 2); // even positive numbers

Try it out:

Every time you press the button, we call emiter.go like so:

emiter.go;

Basic Emitter 2

What does this one do?

var emiter = m.emitFromRange(1, Infinity, 2);

Looping To Create a Sequence

Instead of pressing the button a lot of times, let’s use a map to call our emitter some number of times on our behalf.

m.map(m.emitFromRange(10, Infinity, 10),100).go;





Since this is an infinite range, we tell our map to only call the emitter 100 times.

Looping To Create a Sequence 2

If we are creating a sequence from a finite range, we can ignore the maximum number of elements in our map:

m.map(m.emitFromRange(60, 2, -2)).go;





Creating Sequences With Algorithms

We can do more than just emit from a range of values. We can plug emitters into algorithms. Let’s shift a range by 2 using addition:

var emitAdd2 = m.map( m.add(2, m.emitFromArr([1, 2, 3, 4, 5, 12, 15]) ) );

Try it out:





We are also using an emitFromArr emitter which pulls values from an array.

Sequences Of Strings

Let’s write each element as an equation (we are invoking multiply as a string) to the console as they are being sequenced (open the console to see the results):

m.map( m.writeLn( m.mul(3, m.emitFromRange(1, 3, 1)) ), 5 );

Try it out:





Emitters Described

An emitter provides the next item from… something. We have created two emitters so far:

emitFromArr - An emitter that pulls items from an array.

- An emitter that pulls items from an array. emitFromRange - An emitter that pulls items from a dynamically generated range of numbers.

We can do some cool things with these.

How about:

m.emitFromRange(0, Infinity, 23); // an unlimited range m.emitFromRange(-20, 20, .5); // a simple range m.emitFromRange(1, Infinity, 2); // emit odd numbers m.emitFromRange(0, Infinity, 2); // emit even numbers

How about a range that repeats:

m.map( m.emitFromRange(1,3,1,true), // infinite sequence 30 ).go;

Try it out:





Add Two Sequences

Now things are going to get interesting. In our prior algorithm example, we add 2 to an emitter. However, we can actually add two emitters:

Let’s add two emitters (the above example):

m.map( m.add( m.emitFromRange(0, Infinity, 2), m.emitFromRange(1, Infinity, 2) ),100 ).go;





Subtract Two Sequences

We could also subtract two sequences being emitted:

var sequenceBoring = m.map( m.sub( m.emitFromRange(0, Infinity, 2), m.emitFromRange(1, Infinity, 2) ),120 ).go;





How Flexible Are Emitters?

Emitters are so flexible that we can use an emitter as the increment-by value for our emitFromRange emitter.:

var seuenceBoring = m.map( m.emitFromRange(1, Infinity, m.emitFromArr([1,-8,3,12], true) ) ),500 ).go;





That is one strange sequence we are emitting.

How Flexible Are Emitters 2

In this example, we can see how emitters can work with UI elements so we can see what is going on as each element is generated.

Check this out:

m.propSet("value", m.elemById("lst06B"), m.emitFromRange( 1, Infinity, m.propSet("value", m.elemById("lst06A"), m.emitFromArr([1,-8,3,12], true) ) ) );

Try it out:

value of 'by'

value of emitter



Different Examples (You can try in the console)

Alternate +/-

var alternate = m.mul( m.emitFromRange(0, Infinity, 1), m.emitFromArr([1, -1], true) ); // [0, -1, 2, -3, 4, ...] // remember to call alternate.go; in the // javascript console to use alternate.

Powers

var power2 = m.pow( 2, m.emitFromRange(1, Infinity, 1) ); // [1, 2, 4, 8, 16, ...]

BUT

var power2 = m.pow( m.emitFromRange(1, Infinity, 1), 2 ); // [1, 4, 9, 16, 25, ...]

Conclusion

We’ve shown a few interesting ways we can generate sequences using emitters. There is always a need for more emitters (a Fibonacci sequence emitter would be awesome for someone to add).

And remember, the emitter is framework agnostic.

If you find emitters interesting, please follow @erichosick.