Permalink
Browse files

Added an idea for an iterator

  • Loading branch information...
1 parent d4412f0 commit 159627d3ce5de75e6e7820e245f10de39854e8ae Alex Young committed Oct 24, 2009
Showing with 80 additions and 1 deletion.
  1. +53 −0 closure.js
  2. +1 −1 test/riot
  3. +26 −0 test/sequence_test.js
View
@@ -50,8 +50,53 @@ Closure.Class = {
};
/* Sequences */
+Closure.Iterator = Closure.Class.create({
+ initialize: function(iterator) {
+ this.reset();
+ this.setIterator(iterator);
+ },
+
+ setIterator: function(iterator) {
+ if (typeof iterator !== 'undefined') {
+ if (typeof iterator === 'function') {
+ this.iterator = iterator;
+ } else {
+ this.iterator = function(v, i) { return iterator[i]; };
+ }
+ }
+ },
+
+ next: function() {
+ this.last = this.iterator(this.last, this.index);
+ this.index++;
+ return this.last;
+ },
+
+ reset: function() {
+ this.last = 0;
+ this.index = 0;
+ },
+
+ slice: function(start, end) {
+ var values = [];
+ for (var i = 0; i < start; i++) {
+ this.next();
+ }
+ for (var i = start; i < end; i++) {
+ values.push(this.next());
+ }
+ this.last = 0;
+ this.index = 0;
+ return values;
+ }
+});
+
Closure.Sequence = Closure.Class.create({
initialize: function(items) {
+ if (typeof items === 'function') {
+ items = new Closure.Iterator(items);
+ }
+
this.start = 0;
this.end = items.length;
this.items = items;
@@ -83,6 +128,10 @@ Closure.Sequence = Closure.Class.create({
},
iterate: function() {
+ if (this.iterator) {
+ return function () { return this.iterator.next() };
+ }
+
var seq = this,
i = seq.start - 1,
getter = function() { return i < seq.start ? null : (i < seq.end ? seq.items[i] : null); };
@@ -127,6 +176,10 @@ Closure.Sequence = Closure.Class.create({
return seq;
},
+ first: function() {
+ return this.take(1);
+ },
+
at: function(n) {
var seq = this.clone();
seq.start = n;
Submodule riot updated 2 files
+0 −8 riot.js
+0 −1 test.js
View
@@ -1,6 +1,32 @@
load('test_helper.js');
Riot.context('Sequence', function() {
+ /*given('an iterator', function() {
+ var iterator = new Closure.Iterator(function(x) { return x + 1; }),
+ seq = new Closure.Sequence(iterator);
+
+ should('take take values', seq.take(5).toString()).equals('1,2,3,4,5');
+ //should('return the first value', seq.first().toString()).equals('1');
+ //should('take values then tail', seq.take(100).tail(1).toString()).equals('100');
+ });
+ */
+ given('an array iterator', function() {
+ var iterator = new Closure.Iterator([3, 6, 9, 12, 15]);
+ should('return expected values', iterator.next()).equals(3);
+ should('return expected value again', iterator.next()).equals(6);
+ });
+
+ given('a function iterator', function() {
+ var iterator = new Closure.Iterator(function(x) { return x + 1; });
+ should('iterate once with next', iterator.next()).equals(1);
+ should('iterate again with next', iterator.next()).equals(2);
+ });
+
+ given('a sequence that uses an iterator', function() {
+ var seq = new Closure.Sequence(function(x, i) { return i * 2; });
+ should('take values from the sequence', seq.take(3).toString()).equals('0,2,4');
+ });
+
given('an array', function() {
var a = [1, 2, 3, 4, 5],
seq = new Closure.Sequence(a),

0 comments on commit 159627d

Please sign in to comment.