on master help sign in
Merge pull request #134 from ngunn/l-dev

List.js change: Nil checks all use .ctor

commit7044dd511f2be18e8545dac5aee1f4667e5bb3a9 authorEvan Czaplicki <info@elm-lang.org> dateWed 27 Mar 2013, 14:09 (14 years ago) parent10242dbaa7a67f
1 file changed+18−9
libraries/Native/List.js+18−9
libraries/Native/List.js+18−9
@@ -6,6 +6,15 @@ Elm.Native.List = function(elm) {
66
77 var Utils = Elm.Native.Utils(elm);
88
9+ // TODO: Improve Nil handling
10+ // We can change places like: if (xs.ctor === 'Nil') ... to if (xs === Nil) ...
11+ // but only if we're confident Nil can only be defined once.
12+ // Currently (27Mar2013) each module can have different instantiations, so multiple Nil objects can exist
13+ // (and if they're used interchangeably then direct object comparison fails where ctor doesn't).
14+ // So, this can only be fixed when modules initialisation is also fixed.
15+ // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
16+ // (on firefox 19)
17+
918 // freeze is universally supported and as a singleton introduces little performance penalty
1019 // for a small amount of object safety
1120 var Nil = Object.freeze({ ctor:'Nil' });
@@ -44,7 +53,7 @@ Elm.Native.List = function(elm) {
4453
4554 function append(xs,ys) {
4655 if (typeof xs === "string") { return xs.concat(ys); }
47− if (xs === Nil) { return ys; }
56+ if (xs.ctor === 'Nil') { return ys; }
4857 var root = Cons(xs._0, Nil);
4958 var curr = root;
5059 xs = xs._1;
@@ -57,11 +66,11 @@ Elm.Native.List = function(elm) {
5766 return root;
5867 }
5968
60− function head(v) { return v === Nil ? throwError('head') : v._0; }
61− function tail(v) { return v === Nil ? throwError('tail') : v._1; }
69+ function head(v) { return v.ctor === 'Nil' ? throwError('head') : v._0; }
70+ function tail(v) { return v.ctor === 'Nil' ? throwError('tail') : v._1; }
6271
6372 function last(xs) {
64− if (xs === Nil) { throwError('last'); }
73+ if (xs.ctor === 'Nil') { throwError('last'); }
6574 var out = xs._0;
6675 while (xs.ctor !== 'Nil') {
6776 out = xs._0;
@@ -100,11 +109,11 @@ Elm.Native.List = function(elm) {
100109 }
101110
102111 function foldl1(f, xs) {
103− return xs === Nil ? throwError('foldl1') : foldl(f, xs._0, xs._1);
112+ return xs.ctor === 'Nil' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
104113 }
105114
106115 function foldr1(f, xs) {
107− if (xs === Nil) { throwError('foldr1'); }
116+ if (xs.ctor === 'Nil') { throwError('foldr1'); }
108117 var arr = toArray(xs);
109118 var acc = arr.pop();
110119 for (var i = arr.length; i--; ) {
@@ -124,7 +133,7 @@ Elm.Native.List = function(elm) {
124133 }
125134
126135 function scanl1(f, xs) {
127− return xs === Nil ? throwError('scanl1') : scanl(f, xs._0, xs._1);
136+ return xs.ctor === 'Nil' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
128137 }
129138
130139 function filter(pred, xs) {
@@ -156,7 +165,7 @@ Elm.Native.List = function(elm) {
156165 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
157166
158167 function concat(xss) {
159− if (xss === Nil) return xss;
168+ if (xss.ctor === 'Nil') return xss;
160169 var arr = toArray(xss);
161170 var xs = arr[arr.length-1];
162171 for (var i = arr.length-1; i--; ) {
@@ -228,7 +237,7 @@ Elm.Native.List = function(elm) {
228237 }
229238
230239 function join(sep, xss) {
231− if (xss === Nil) return Nil;
240+ if (xss.ctor === 'Nil') return Nil;
232241 var s = toArray(sep);
233242 var out = toArray(xss._0);
234243 xss = xss._1;