on master help sign in
List.js change: Nil checks all use .ctor
aa7a67fngunn
libraries/Native/List.js+18−9
@@ -6,6 +6,15 @@ Elm.Native.List = function(elm) {
6
6
7 var Utils = Elm.Native.Utils(elm);
7 var Utils = Elm.Native.Utils(elm);
8
8
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+
9 // freeze is universally supported and as a singleton introduces little performance penalty
18 // freeze is universally supported and as a singleton introduces little performance penalty
10 // for a small amount of object safety
19 // for a small amount of object safety
11 var Nil = Object.freeze({ ctor:'Nil' });
20 var Nil = Object.freeze({ ctor:'Nil' });
@@ -44,7 +53,7 @@ Elm.Native.List = function(elm) {
44
53
45 function append(xs,ys) {
54 function append(xs,ys) {
46 if (typeof xs === "string") { return xs.concat(ys); }
55 if (typeof xs === "string") { return xs.concat(ys); }
47− if (xs === Nil) { return ys; }
56+ if (xs.ctor === 'Nil') { return ys; }
48 var root = Cons(xs._0, Nil);
57 var root = Cons(xs._0, Nil);
49 var curr = root;
58 var curr = root;
50 xs = xs._1;
59 xs = xs._1;
@@ -57,11 +66,11 @@ Elm.Native.List = function(elm) {
57 return root;
66 return root;
58 }
67 }
59
68
60− function head(v) { return v === Nil ? throwError('head') : v._0; }
69+ function head(v) { return v.ctor === 'Nil' ? throwError('head') : v._0; }
61− function tail(v) { return v === Nil ? throwError('tail') : v._1; }
70+ function tail(v) { return v.ctor === 'Nil' ? throwError('tail') : v._1; }
62
71
63 function last(xs) {
72 function last(xs) {
64− if (xs === Nil) { throwError('last'); }
73+ if (xs.ctor === 'Nil') { throwError('last'); }
65 var out = xs._0;
74 var out = xs._0;
66 while (xs.ctor !== 'Nil') {
75 while (xs.ctor !== 'Nil') {
67 out = xs._0;
76 out = xs._0;
@@ -100,11 +109,11 @@ Elm.Native.List = function(elm) {
100 }
109 }
101
110
102 function foldl1(f, xs) {
111 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);
104 }
113 }
105
114
106 function foldr1(f, xs) {
115 function foldr1(f, xs) {
107− if (xs === Nil) { throwError('foldr1'); }
116+ if (xs.ctor === 'Nil') { throwError('foldr1'); }
108 var arr = toArray(xs);
117 var arr = toArray(xs);
109 var acc = arr.pop();
118 var acc = arr.pop();
110 for (var i = arr.length; i--; ) {
119 for (var i = arr.length; i--; ) {
@@ -124,7 +133,7 @@ Elm.Native.List = function(elm) {
124 }
133 }
125
134
126 function scanl1(f, xs) {
135 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);
128 }
137 }
129
138
130 function filter(pred, xs) {
139 function filter(pred, xs) {
@@ -156,7 +165,7 @@ Elm.Native.List = function(elm) {
156 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
165 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
157
166
158 function concat(xss) {
167 function concat(xss) {
159− if (xss === Nil) return xss;
168+ if (xss.ctor === 'Nil') return xss;
160 var arr = toArray(xss);
169 var arr = toArray(xss);
161 var xs = arr[arr.length-1];
170 var xs = arr[arr.length-1];
162 for (var i = arr.length-1; i--; ) {
171 for (var i = arr.length-1; i--; ) {
@@ -228,7 +237,7 @@ Elm.Native.List = function(elm) {
228 }
237 }
229
238
230 function join(sep, xss) {
239 function join(sep, xss) {
231− if (xss === Nil) return Nil;
240+ if (xss.ctor === 'Nil') return Nil;
232 var s = toArray(sep);
241 var s = toArray(sep);
233 var out = toArray(xss._0);
242 var out = toArray(xss._0);
234 xss = xss._1;
243 xss = xss._1;