all files / lib/stream/ stream.js

100% Statements 19/19
100% Branches 6/6
100% Functions 6/6
100% Lines 19/19
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                                      171382×       276279× 276279× 125×   276154×             124924× 125396× 125396× 119166×       5758×          
 
/*
 * Parsec
 * https://github.com/d-plaindoux/parsec
 *
 * Copyright (c) 2016 Didier Plaindoux
 * Licensed under the LGPL2 license.
 */
 
 module.exports = (function() {
     
     'use strict';
     
     var atry  = require('../data/try.js');
     
     /**
      * Stream basic type
      */
     function Stream() {
     }
     
     // Stream 'a => number -> number
     Stream.prototype.location = function(index) {
         return index;
     };
     
     // Stream 'a => number -> Try 'a
     Stream.prototype.get = function(index) {
         try {
            if (this.endOfStream(index)) {
                return atry.failure(new Error("End of stream reached")); 
            } else {
                return atry.success(this.unsafeGet(index));
            }
         } catch (e) {
             return atry.failure(e); 
         }
     };    
     
     // Stream 'a => [Comparable 'a] -> number -> boolean
     Stream.prototype.subStreamAt = function(s, index) {
         for (var i = 0; i < s.length; i++) {
             var value = this.get(i + index);
             if (!value.isSuccess() || value.success() !== s[i]) {
                 return false;
             }
         }
         
         return true;
     };
 
     return function() {
         return new Stream();
     };
     
 }());