Skip to content

Commit 2115d9a

Browse files
committed
jme.isDeterministic looks at the expression of lambda tokens
This isn't *strictly* correct: an anonymous function `a -> random(0,1)` *is* deterministic as an expression that produces a lambda token, but the output of the lambda is not deterministic. To be really accurate, a lambda should only count as non-deterministic when it's applied, e.g. `i -> random(0,i)` is deterministic but `(i -> random(0,i))(4)` is not. But I'd have to remember that for every function that takes lambdas. This was the easiest way of ensuring that jme.isDeterministic doesn't think that expressions using non-deterministic lambdas are deterministic, e.g. `map(i->random(i..2i), 0..3)`
1 parent 68fd1dc commit 2115d9a

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

runtime/scripts/jme.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
931931
}
932932
}
933933
return true;
934+
case 'lambda':
935+
return jme.isDeterministic(expr.tok.expr, scope);
934936
default:
935937
if(!expr.args) {
936938
return true;

tests/jme-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9754,6 +9754,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
97549754
}
97559755
}
97569756
return true;
9757+
case 'lambda':
9758+
return jme.isDeterministic(expr.tok.expr, scope);
97579759
default:
97589760
if(!expr.args) {
97599761
return true;

tests/jme/jme-tests.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,9 @@ Numbas.queueScript('jme_tests',['qunit','jme','jme-rules','jme-display','jme-cal
15141514
check('"{a} then {random(1,2)}"',false);
15151515
check('"{a} then {b}"',true);
15161516
check('safe("these empty braces: {}")', true);
1517+
check('a -> 1', true);
1518+
check('a -> random(a,0)', false);
1519+
check('map(a -> random(a..2a), 0..3)', false);
15171520

15181521
var scope = new jme.Scope([Numbas.jme.builtinScope]);
15191522
var fn = new jme.funcObj('fn',[],jme.types.TNum,function() { return 1; });

tests/numbas-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9090,6 +9090,8 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
90909090
}
90919091
}
90929092
return true;
9093+
case 'lambda':
9094+
return jme.isDeterministic(expr.tok.expr, scope);
90939095
default:
90949096
if(!expr.args) {
90959097
return true;

0 commit comments

Comments
 (0)