public
Description: Go figure
Clone URL: git://github.com/matthieu/witty.git
Tests for ncurry. Fixed a bug when strings get multi evaluated.
matthieu (author)
Sun Jul 20 17:55:04 -0700 2008
commit  315197770ea0704c20908345b5fc7b8311567104
tree    4045b9ae30bcdb773a344fc5acf258e46e339eea
parent  be1623e72805096aae4019462cb4ffc2404bec65
...
21
22
23
24
 
25
26
27
...
21
22
23
 
24
25
26
27
0
@@ -21,7 +21,7 @@ var wArray = {
0
     var arr = operands.first().slice();
0
     for (var o = 1, opr; opr = operands[o]; o++) {
0
       var idx;
0
- for (var m = 0, el; el = opr[m]; m++)
0
+ for (var m = 0, el; (el = opr[m] || el == 0); m++)
0
         if ((idx = arr.indexOf(el)) != null) arr.splice(idx, 1);
0
     }
0
     return arr;
...
19
20
21
22
 
23
24
25
...
41
42
43
44
 
 
 
 
 
 
 
 
45
46
47
...
157
158
159
160
161
162
163
164
165
...
169
170
171
172
 
173
174
175
...
280
281
282
283
284
285
286
...
290
291
292
293
294
 
 
295
296
297
...
308
309
310
311
 
312
313
314
...
19
20
21
 
22
23
24
25
...
41
42
43
 
44
45
46
47
48
49
50
51
52
53
54
...
164
165
166
 
167
 
168
169
170
...
174
175
176
 
177
178
179
180
...
285
286
287
 
288
289
290
...
294
295
296
 
 
297
298
299
300
301
...
312
313
314
 
315
316
317
318
0
@@ -19,7 +19,7 @@ function parse(expr) {
0
 function eval_(exp, env) {
0
   //print("eval: " + JSON.stringify(exp) + " :sntx: " + exp.sntx);
0
   if (selfEval(exp)) return eval(exp); // JS eval for native type
0
- else if (quoted(exp)) return eval(exp); // JS eval to unescape
0
+ else if (quoted(exp)) return evalQuoted(exp);
0
   else if (variableRef(exp)) return variableValue(exp, env);
0
   else if (list(exp)) return evalList(exp, env);
0
   else if (sequence(exp)) return evalSeq(exp, env);
0
@@ -41,7 +41,14 @@ function evalList(operands, env) {
0
 }
0
 
0
 function selfEval(exp) { return (!isNaN(exp)); }
0
-function quoted(exp) { return ((typeof exp) == 'string') && exp[0] == '"'; }
0
+function quoted(exp) { return (typeof exp == 'string' || exp instanceof String) && (exp[0] == '"' || exp.alien); }
0
+function evalQuoted(exp) {
0
+ if (exp.alien) return exp;
0
+ // JS eval to unescape
0
+ var str = new String(eval(exp));
0
+ str.alien = true;
0
+ return str;
0
+}
0
 
0
 function variableRef(exp) { return (typeof exp) == 'string'; }
0
 function variableValue(exp, env, isMacro) {
0
@@ -157,9 +164,7 @@ function primitiveParams(operation) { return operation[1]; }
0
 
0
 function applyPrimitive(operation, operands, env) {
0
   var primFn = primitiveBody(operation);
0
- //print("calling prim " + primFn + " with " + JSON.stringify(operands));
0
   if (typeof primFn == "function") return primFn.call(null, operands, env);
0
- //else if (primFn = primitives[primFn]) return primFn.call(null, operands, env);
0
   else throw "Unrecognized primitive: " + operation[1] + ", most certainly a bug.";
0
 }
0
 function opEval(fn) {
0
@@ -169,7 +174,7 @@ function typeDispatch(op) {
0
   return function(operands, env) {
0
     var first = operands.first(), typeFn;
0
     if (typeof first == 'number') typeFn = wNum[op];
0
- else if (typeof first == 'string') typeFn = wString[op];
0
+ else if (typeof first == 'string' || first instanceof String) typeFn = wString[op];
0
     else if (first instanceof Array) typeFn = wArray[op];
0
     else throw "Can't dispatch operation " + op + " to " + first + ", unknown type.";
0
 
0
@@ -280,7 +285,6 @@ addPrimitive('lcurry', ['lambda', 'parameters*'], opEval(
0
 addPrimitive('rcurry', ['lambda', 'parameters*'], opEval(
0
   function(operands, env) {
0
     var fn = operands.first();
0
- print("inprim " + operands);
0
     if (lambda(fn)) return makeCurriedLambda(fn, rightCurry(fn, operands.tail()));
0
     else if (primitive(fn)) return makeCurriedPrimitive(fn, rightCurry(fn, operands.tail()));
0
     else throw "The first parameter of rightCurry should be a function."
0
@@ -290,8 +294,8 @@ addPrimitive('ncurry', ['lambda', 'index', 'parameter'], opEval(
0
     var fn = operands.first();
0
     var idx = operands[1];
0
     if (typeof idx != "number") throw "Second parameter of ncurry must be a number."
0
- if (lambda(fn)) return makeCurriedLambda(fn, nCurry(fn, idx, operands.tail()));
0
- else if (primitive(fn)) return makeCurriedPrimitive(fn, nCurry(fn, idx, operands.tail()));
0
+ if (lambda(fn)) return makeCurriedLambda(fn, nCurry(fn, idx, operands[2]));
0
+ else if (primitive(fn)) return makeCurriedPrimitive(fn, nCurry(fn, idx, operands[2]));
0
     else throw "The first parameter of nurry should be a function."
0
   }));
0
 addPrimitive('=', ['symbol', 'value'], function(operands, env) {
0
@@ -308,7 +312,7 @@ addPrimitive('=', ['symbol', 'value'], function(operands, env) {
0
 addPrimitive('==', ['loperand', 'roperand'], opEval(
0
   function(operands, env) {
0
     if (operands[0] instanceof Array) operands[0].toString() == operands[1].toString();
0
- else return operands[0] == operands[1];
0
+ else return operands[0].valueOf() == operands[1].valueOf();
0
   })); // TODO accept n parameters
0
 addPrimitive('join', ['separator'], opEval(typeDispatch('join')));
0
 addPrimitive('<', ['loperand', 'roperand'], opEval(function(operands, env) { return operands[0] < operands[1]; }));
...
25
26
27
28
 
29
30
31
...
25
26
27
 
28
29
30
31
0
@@ -25,7 +25,7 @@ Array.prototype.reduceFirst = function(fn) {
0
 };
0
 Array.prototype.indexOf = function(value) {
0
   for (var m = 0, el; (el = this[m]) || el == 0; m++)
0
- if (value == el) return m;
0
+ if (value.valueOf() == el.valueOf()) return m;
0
   return null;
0
 }
0
 Array.prototype.truthyLength = function(value) {
...
23
24
25
 
 
 
 
 
...
23
24
25
26
27
28
29
30
0
@@ -23,3 +23,8 @@ assert(fact5() == 120, "Curried factorial of 5 didn't produce expected result.")
0
 testIt = rcurry(if, 1, 0)
0
 assert(testIt(2 == 3) == 0, "Right currying of if failed (0).")
0
 assert(testIt(3 == 3) == 1, "Right currying of if failed (1).")
0
+
0
+successOnly = ncurry(if, 1, "success")
0
+assert(successOnly(1 == 1) == "success", "N currying at pos 1 of if failed in equality case.")
0
+assert(successOnly(1 == 0) != "success", "N currying at pos 1 of if failed in non equality case.")
0
+assert(successOnly(1 == 0, "failed") == "failed", "N currying at pos 1 of if with failure result failed in non equality case.")

Comments

    No one has commented yet.