Code coverage report for nodash/lib/Stream.js

Statements: 100% (62 / 62)      Branches: 100% (22 / 22)      Functions: 100% (17 / 17)      Lines: 100% (62 / 62)      Ignored: none     

All files » nodash/lib/ » Stream.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116      1     5   1 77 2   75 75 75 60 60 60   75 59 59     2   56 56   1   58 58     5 5 5 5 1     5   5         33 9   1 71 54 47     17   24       330 14 33 33   316 113 107   203 202 202 11385           1 15   5       1 6 5 5     1       3 1   1 39 12   39 36     3          
/* vim: set et sw=2 ts=2: */
'use strict';
 
module.exports = [ 'Thunk', 'List', 'idf', 'tuple', 'typeOf', 'freeze', 'error',
  function (Thunk, List, idf, tuple, typeOf, freeze, error) {
 
  var Nodash = this;
 
  function Stream(generator) {
    if (!(this instanceof Stream)) {
      return new Stream(generator);
    }
    var thunk = new Thunk(generator);
    var self = this;
    this.head = function () {
      var value = thunk.get().fst();
      self.head = idf(value);
      return value;
    };
    this.tail = function () {
      var value = thunk.get().snd();
      switch (typeOf(value)) {
        case 'list':
        case 'stream':
          break;
        case 'function':
          value = new Stream(value);
          break;
        default:
          error(TypeError);
      }
      self.tail = idf(value);
      return value;
    };
  }
  Stream.__type = 'stream';
  Stream.prototype = new List();
  Stream.prototype.constructor = Stream;
  Stream.prototype.toString = function () {
    return '[object Stream head=' + this.head() + ']';
  };
 
  freeze(Stream);
 
  return {
 
    'Stream stream': Stream,
 
    'lazy arrayToList': function (val) {
      if (Nodash.isFunction(val)) {
        return new Thunk(val);
      }
      function generator(i) {
        if (i < val.length) {
          return new List(val[i], new Thunk(function () {
            return generator(i + 1);
          }));
        }
        return Nodash.emptyList();
      }
      return generator(0);
    },
 
    each: function (f, xs) {
      if (Nodash.is(List, xs)) {
        while (!xs.isEmpty()) {
          f(xs.head());
          xs = xs.tail();
        }
      } else if (Nodash.isArray(xs) || Nodash.isString(xs)) {
        for (var i = 0; i < xs.length; i++) {
          f(xs[i], i);
        }
      } else if (Nodash.isObject(xs)) {
        var ks = Object.keys(xs);
        for (var j = 0; j < ks.length; j += 1) {
          f(xs[ks[j]], ks[j]);
        }
      }
    },
 
    repeat: function (x) {
      function generator() {
        return tuple(x, generator);
      }
      return new Stream(generator);
    },
 
    iterate: function (f, seed) {
      function generator(seed) {
        return function () {
          var newSeed = f(seed);
          return tuple(seed, generator(newSeed));
        };
      }
      return new Stream(generator(seed));
    },
 
    cycle: function (xs) {
      if (Nodash.isArray(xs)) {
        xs = Nodash.lazy(xs);
      }
      function generator(ys) {
        if (ys.isEmpty()) {
          ys = xs;
        }
        return function () {
          return tuple(ys.head(), generator(ys.tail()));
        };
      }
      return new Stream(generator(xs));
    }
 
  };
}];