Skip to content

Commit

Permalink
Fixed bug in zip and added Natural numbers support
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulapopoola committed Oct 26, 2015
1 parent 7fb2797 commit ed9b7b4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
15 changes: 14 additions & 1 deletion api/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
<dt><a href="#Ones">Ones()</a> ⇒ <code><a href="#Stream">Stream</a></code></dt>
<dd><p>Returns an infinite stream of ones</p>
</dd>
<dt><a href="#NaturalNumbers">NaturalNumbers()</a> ⇒ <code><a href="#Stream">Stream</a></code></dt>
<dd><p>Returns the stream of Natural numbers</p>
</dd>
</dl>
<a name="Stream"></a>
## Stream
Expand Down Expand Up @@ -419,8 +422,18 @@ Constructs a stream made up of consecutive numbers up to `stop`
Returns an infinite stream of ones

**Kind**: global function
**Returns**: <code>[Stream](#Stream)</code> - Value at nth index in stream
**Returns**: <code>[Stream](#Stream)</code> - An infinite stream of Ones
**Example**
```js
var ones = Stream.Ones();
```
<a name="NaturalNumbers"></a>
## NaturalNumbers() ⇒ <code>[Stream](#Stream)</code>
Returns the stream of Natural numbers

**Kind**: global function
**Returns**: <code>[Stream](#Stream)</code> - The infinite stream of natural numbers
**Example**
```js
var naturals = Stream.NaturalNumbers();
```
54 changes: 53 additions & 1 deletion spec/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ describe('Stream()', function () {
expect(sum.elementAt(4)).to.deep.equal([25]);
});

it('can append a new stream onto an existing stream', function () {
var s1 = Stream.fromArray([1,3,5]);
var s2 = Stream.fromArray([2,4,6]);
var concatenated = s1.append(s2);
expect(concatenated.length()).to.equal(6);
expect(concatenated.toArray()).to.deep.equal([1,3,5,2,4,6]);
});

it('can pick a new stream from an existing stream', function () {
var s1 = Stream.fromArray([1,3,5,7,9,11]);
var s2 = s1.pick(3);
expect(s2.length()).to.equal(3);
expect(s2.toArray()).to.deep.equal([1,3,5]);
});

it('can find the length of a stream', function () {
var s1 = Stream.fromArray([1,3,5]);
var sum = s1.length();
Expand Down Expand Up @@ -178,12 +193,49 @@ describe('Stream()', function () {
});
expect(triples.toArray()).to.deep.equal([3,9,15]);
});

it('can filter streams', function () {
var s1 = Stream.fromArray([1,2,3,4,5]);
var evenNumbers = s1.filter(function (element) {
return element % 2 === 0;
});
expect(evenNumbers.toArray()).to.deep.equal([2,4]);
});

it('can test for membership of a stream', function () {
var s1 = Stream.fromArray([1,2,3,4,5]);
expect(s1.contains(2)).to.equal(true);
expect(s1.contains(-2)).to.equal(false);
});

it('can remove elements from a stream', function () {
var s1 = Stream.fromArray([1,2,3,4,5]);
s1 = s1.remove(2);
expect(s1.length()).to.equal(3);
expect(s1.toArray()).to.deep.equal([3,4,5]);
});

it('can convert a finite stream to an array', function () {
var s1 = Stream.fromArray([1,3,5]);
var doubled = s1.map(function (element){
return 2 * element;
});
expect(doubled.toArray()).to.deep.equal([2,6,10]);
});
});

it('Ones return an infinite number of Ones', function () {
var s1 = Stream.Ones();

var first5Elements = s1.pick(5);
expect(first5Elements.toArray()).to.deep.equal([1,1,1,1,1]);

expect(s1.elementAt(1000)).to.equal(1);
});

it('NaturalNumbers returns the infinite stream of Natural Numbers', function () {
var s1 = Stream.NaturalNumbers();

var first5Elements = s1.pick(5);
expect(first5Elements.toArray()).to.deep.equal([1,2,3,4,5]);
});
});
51 changes: 46 additions & 5 deletions stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function elementAt(index) {
if(s.isEmpty()) {
return;
}
s = s.tail();
try {
s = s.tail();
} catch (e){
return;
}
index--;
}

Expand Down Expand Up @@ -325,6 +329,7 @@ function remove(n) {
return new Stream(null, null);
}
s = s.tail();
n--;
}

return new Stream(
Expand Down Expand Up @@ -395,13 +400,28 @@ function zip(/* arguments */) {
continue;
}
zippedFirsts.push(s.head());
args[i] = s.tail(); //overwrite stream with tail
}

//Find cleaner pattern
return new Stream(
zippedFirsts,
function () {
return Stream.zip.apply(null, args);
var moved = [];
for(var i=0, len = args.length; i < len; i++){
var tmp = args[i];
var hasTail = true;
try {
tmp = tmp.tail();
} catch (e) {
hasTail = false;
} finally {
if(hasTail) {
moved.push(tmp);
}
}
}

return Stream.zip.apply(null, moved);
}
);
}
Expand Down Expand Up @@ -508,7 +528,7 @@ function upTo(stop){
* Returns an infinite stream of ones
*
* @static
* @returns {Stream} Value at nth index in stream
* @returns {Stream} An infinite stream of Ones
* @example
*
* var ones = Stream.Ones();
Expand All @@ -517,6 +537,26 @@ function Ones() {
return new Stream(1, Ones);
};

/**
* Returns the stream of Natural numbers
*
* @static
* @returns {Stream} The infinite stream of natural numbers
* @example
*
* var naturals = Stream.NaturalNumbers();
**/
function NaturalNumbers() {
return new Stream(
1,
function () {
return Stream.add(
Stream.NaturalNumbers(),
Stream.Ones());
}
);
};

//Instance methods
Stream.prototype.head = head;
Stream.prototype.tail = tail;
Expand All @@ -543,4 +583,5 @@ Stream.fromArray = fromArray;
Stream.fromInterval = fromInterval;
Stream.from = from;
Stream.upTo = upTo;
Stream.Ones = Ones;
Stream.Ones = Ones;
Stream.NaturalNumbers = NaturalNumbers;

0 comments on commit ed9b7b4

Please sign in to comment.