| 1 | 1 | function Enum(next, count, clone) { |
| 2 | 21 | this.next = next; |
| 3 | 21 | this.count = count; |
| 4 | 21 | this.clone = clone; |
| 5 | | } |
| 6 | 1 | module.exports = Enum; |
| 7 | | |
| 8 | | /** |
| 9 | | * Enum#iter(function) |
| 10 | | * |
| 11 | | * e.iter(f) calls the function f with each element of e in turn. This |
| 12 | | * consumes the enumeration. |
| 13 | | **/ |
| 14 | 1 | Enum.prototype.iter = function(fn) { |
| 15 | 2 | var curr; |
| 16 | 2 | while ((curr = this.next()) !== null) |
| 17 | 11 | fn(curr); |
| 18 | | }; |
| 19 | | |
| 20 | | /** |
| 21 | | * Enum#find(function) -> Element |
| 22 | | * |
| 23 | | * e.find(f) returns the first element x of e such that f(x) returns true, |
| 24 | | * consuming the enumeration up to and including the found element, or, |
| 25 | | * returns null if no such element exists in the enumeration, consuming the |
| 26 | | * whole enumeration in the search. Since find consumes a prefix of the |
| 27 | | * enumeration, it can be used several times on the same enumeration to find |
| 28 | | * the next element. |
| 29 | | **/ |
| 30 | 1 | Enum.prototype.find = function(fn) { |
| 31 | 4 | var curr, found; |
| 32 | 4 | while ((curr = this.next()) !== null) { |
| 33 | 16 | if (fn(curr) === true) { |
| 34 | 1 | found = curr; |
| 35 | 1 | break; |
| 36 | | } |
| 37 | | } |
| 38 | | |
| 39 | 4 | return found || null; |
| 40 | | }; |
| 41 | | |
| 42 | 1 | Enum.prototype.isEmpty = function() { |
| 43 | 2 | if (this.fast) |
| 44 | 0 | return this.count() === 0; |
| 45 | | else |
| 46 | 2 | return this.peek() === null; |
| 47 | | }; |
| 48 | | |
| 49 | | // peek e returns None if e is empty or Some x where x is the next element of e. |
| 50 | | // The element is not removed from the enumeration. |
| 51 | 1 | Enum.prototype.peek = function() { |
| 52 | 7 | var next = this.get(); |
| 53 | 7 | if (next !== null) |
| 54 | 5 | this.push(next); |
| 55 | | |
| 56 | 7 | return next; |
| 57 | | }; |
| 58 | | |
| 59 | 1 | Enum.prototype.get = function() { |
| 60 | 25 | return this.next(); |
| 61 | | }; |
| 62 | | |
| 63 | 1 | Enum.prototype.junk = function() { |
| 64 | 0 | this.next(); |
| 65 | | }; |
| 66 | | |
| 67 | 1 | Enum.prototype.push = function(e) { |
| 68 | 6 | var make = function(self) { |
| 69 | 6 | var fNext = self.next; |
| 70 | 6 | var fCount = self.count; |
| 71 | 6 | var fClone = self.clone; |
| 72 | 6 | var nextCalled = false; |
| 73 | | |
| 74 | 6 | self.next = function() { |
| 75 | 4 | nextCalled = true; |
| 76 | 4 | self.next = fNext; |
| 77 | 4 | self.count = fCount; |
| 78 | 4 | self.clone = fClone; |
| 79 | | |
| 80 | 4 | return e; |
| 81 | | }; |
| 82 | | |
| 83 | 6 | self.count = function() { |
| 84 | 1 | var n = fCount(); |
| 85 | 1 | return nextCalled === true ? n : n + 1; |
| 86 | | }; |
| 87 | | |
| 88 | 6 | self.clone = function() { |
| 89 | 0 | var c = fClone(); |
| 90 | 0 | if (nextCalled === false) |
| 91 | 0 | make(c); |
| 92 | | |
| 93 | 0 | return c; |
| 94 | | }; |
| 95 | | }; |
| 96 | | |
| 97 | 6 | make(this); |
| 98 | | }; |
| 99 | | |
| 100 | 1 | Enum.make = function(next, count, clone) { |
| 101 | 0 | var e = new Enum(); |
| 102 | 0 | e.next = next; |
| 103 | 0 | e.count = count; |
| 104 | 0 | e.clone = clone; |
| 105 | | |
| 106 | 0 | return e; |
| 107 | | }; |
| 108 | | |
| 109 | 1 | Enum.empty = function() { |
| 110 | 0 | var e = new Enum(); |
| 111 | 0 | e.next = function() { return null; }; |
| 112 | 0 | e.count = function() { return 0; }; |
| 113 | 0 | e.clone = function() { return e.empty(); }; |
| 114 | 0 | e.fast = true; |
| 115 | | |
| 116 | 0 | return e; |
| 117 | | }; |
| 118 | | |
| 119 | 1 | Enum.init = function(n, fn) { |
| 120 | 0 | var e = new Enum(); |
| 121 | | |
| 122 | 0 | var count = n; |
| 123 | 0 | e.count = function() { |
| 124 | 0 | return count; |
| 125 | | }; |
| 126 | | |
| 127 | 0 | e.next = function() { |
| 128 | 0 | if (count === 0) { |
| 129 | 0 | throw "No more elements"; |
| 130 | | } |
| 131 | | else { |
| 132 | 0 | count -= 1; |
| 133 | 0 | return fn(n - 1 - count); |
| 134 | | } |
| 135 | | }; |
| 136 | | |
| 137 | 0 | e.clone = function() { |
| 138 | 0 | return e.init(count, fn); |
| 139 | | }; |
| 140 | | |
| 141 | 0 | e.fast = true; |
| 142 | | }; |
| 143 | | |
| 144 | 1 | Enum.prototype.map = function(fn) { |
| 145 | 2 | var self = this; |
| 146 | 2 | return (function() { |
| 147 | 2 | this.next = function() { |
| 148 | 4 | return fn(self.next()); |
| 149 | | }; |
| 150 | | |
| 151 | 2 | this.count = self.count; |
| 152 | | |
| 153 | 2 | this.clone = function() { |
| 154 | 1 | return self.clone().map(fn); |
| 155 | | }; |
| 156 | | |
| 157 | 2 | this.fast = self.fast; |
| 158 | | |
| 159 | 2 | return this; |
| 160 | | }).call(new Enum()); |
| 161 | | }; |
| 162 | | |
| 163 | 1 | Enum.prototype.filter = function(fn) { |
| 164 | | |
| 165 | | }; |
| 166 | | |
| 167 | 1 | Enum.fromArray = function(array) { |
| 168 | 19 | return (function() { |
| 169 | 19 | var counter = 0; |
| 170 | 19 | this.next = function() { |
| 171 | 54 | var value = null; |
| 172 | 54 | if (typeof array[counter] !== "undefined") { |
| 173 | 46 | value = array[counter]; |
| 174 | 46 | counter += 1; |
| 175 | | } |
| 176 | | |
| 177 | 54 | return value; |
| 178 | | }; |
| 179 | | |
| 180 | 19 | this.count = function() { |
| 181 | 6 | return array.length - counter; // todo: not good probably |
| 182 | | }; |
| 183 | | |
| 184 | 19 | this.clone = function() { |
| 185 | 2 | var e = Enum.fromArray([1, 2, 3, 4, 5, 6]); |
| 186 | 2 | e._setCounter(counter); |
| 187 | 2 | return e; |
| 188 | | }; |
| 189 | | |
| 190 | 19 | this._setCounter = function(n) { |
| 191 | 2 | counter = n; |
| 192 | | }; |
| 193 | | |
| 194 | 19 | return this; |
| 195 | | }).call(new Enum()); |
| 196 | | }; |
| 197 | | |
| 198 | 1 | Enum.fromString = function(string) { |
| 199 | 0 | return (function() { |
| 200 | 0 | var counter = 0; |
| 201 | 0 | this.next = function() { |
| 202 | 0 | var value = null; |
| 203 | 0 | if (typeof string[counter] !== "undefined") { |
| 204 | 0 | value = string[counter]; |
| 205 | 0 | counter += 1; |
| 206 | | } |
| 207 | | |
| 208 | 0 | return value; |
| 209 | | }; |
| 210 | | |
| 211 | 0 | this.count = function() { |
| 212 | 0 | return array.length - counter; // todo: not good probably |
| 213 | | }; |
| 214 | | |
| 215 | 0 | this.clone = function() { |
| 216 | 0 | var e = Enum.fromString(string); |
| 217 | 0 | e._setCounter(counter); |
| 218 | 0 | return e; |
| 219 | | }; |
| 220 | | |
| 221 | 0 | this._setCounter = function(n) { |
| 222 | 0 | counter = n; |
| 223 | | }; |
| 224 | | |
| 225 | 0 | return this; |
| 226 | | }).call(new Enum()); |
| 227 | | }; |
| 228 | | |