Skip to content
Permalink
Browse files

list slice with step size

list[a..b#c] picks every c^th element in the list between indices a and
b.

For example, [0,1,2,3,4,5,6,7][1..8#2] = [1,3,5,7]
  • Loading branch information...
christianp committed Jun 28, 2018
1 parent 8310a3f commit 36621f642a634dd2d25379adca3990e8c6684813
Showing with 37 additions and 3 deletions.
  1. +12 −1 runtime/scripts/jme-builtins.js
  2. +12 −1 tests/jme-runtime.js
  3. +1 −0 tests/jme.html
  4. +12 −1 tests/numbas-runtime.js
@@ -689,7 +689,18 @@ newBuiltin('listval',[TList,TRange],TList, null, {
var size = list.vars;
var start = util.wrapListIndex(range[0],size);
var end = util.wrapListIndex(range[1]),size;
var value = list.value.slice(start,end);
var step = range[2];
var value;
if(step!=1) {
value = [];
for(var i=start;i<end;i += step) {
if(i%1==0) {
value.push(list.value[i]);
}
}
} else {
value = list.value.slice(start,end);
}
return new TList(value);
}
});
@@ -8423,7 +8423,18 @@ newBuiltin('listval',[TList,TRange],TList, null, {
var size = list.vars;
var start = util.wrapListIndex(range[0],size);
var end = util.wrapListIndex(range[1]),size;
var value = list.value.slice(start,end);
var step = range[2];
var value;
if(step!=1) {
value = [];
for(var i=start;i<end;i += step) {
if(i%1==0) {
value.push(list.value[i]);
}
}
} else {
value = list.value.slice(start,end);
}
return new TList(value);
}
});
@@ -796,6 +796,7 @@ <h2 id="qunit-userAgent"></h2>
deepCloseEqual(assert, evaluate('["a","b","c"] except "a"').value.map(getValue),['b','c'],'["a","b","c"] except "a"');
deepCloseEqual(assert, evaluate('["a","b","c"] except ["a","c","f"]').value.map(getValue),['b'],'["a","b","c"] except ["a","c","f"]');
deepCloseEqual(assert, evaluate('["a","b","c","d","e"][0..2]').value.map(getValue),['a','b'],'["a","b","c","d","e"][0..2]');
deepCloseEqual(assert, evaluate('["a","b","c","d","e"][0..5#2]').value.map(getValue),['a','c','e'],'["a","b","c","d","e"][0..5#2]');
assert.equal(evaluate("all([])").value,true,"all([])");
assert.equal(evaluate("all([true])").value,true,"all([true])");
@@ -8423,7 +8423,18 @@ newBuiltin('listval',[TList,TRange],TList, null, {
var size = list.vars;
var start = util.wrapListIndex(range[0],size);
var end = util.wrapListIndex(range[1]),size;
var value = list.value.slice(start,end);
var step = range[2];
var value;
if(step!=1) {
value = [];
for(var i=start;i<end;i += step) {
if(i%1==0) {
value.push(list.value[i]);
}
}
} else {
value = list.value.slice(start,end);
}
return new TList(value);
}
});

0 comments on commit 36621f6

Please sign in to comment.
You can’t perform that action at this time.