spec: sequence protocol tests — 45 tests, all passing on JS and OCaml
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
// =========================================================================
|
||||
|
||||
var NIL = Object.freeze({ _nil: true, toString: function() { return "nil"; } });
|
||||
var SX_VERSION = "2026-05-01T09:26:26Z";
|
||||
var SX_VERSION = "2026-05-01T10:16:10Z";
|
||||
|
||||
function isNil(x) { return x === NIL || x === null || x === undefined; }
|
||||
function isSxTruthy(x) { return x !== false && !isNil(x); }
|
||||
@@ -806,6 +806,10 @@
|
||||
var hasKey = PRIMITIVES["has-key?"];
|
||||
var vectorToList = PRIMITIVES["vector->list"];
|
||||
var listToVector = PRIMITIVES["list->vector"];
|
||||
var isVector = PRIMITIVES["vector?"];
|
||||
var vectorLength = PRIMITIVES["vector-length"];
|
||||
var vectorRef = PRIMITIVES["vector-ref"];
|
||||
var reverse = PRIMITIVES["reverse"];
|
||||
function zip(a, b) { var r = []; for (var i = 0; i < Math.min(a.length, b.length); i++) r.push([a[i], b[i]]); return r; }
|
||||
function append_b(arr, x) { arr.push(x); return arr; }
|
||||
var apply = function(f, args) {
|
||||
@@ -3045,7 +3049,7 @@ PRIMITIVES["ho-fn?"] = hoFn_p;
|
||||
PRIMITIVES["ho-swap-args"] = hoSwapArgs;
|
||||
|
||||
// seq-to-list
|
||||
var seqToList = function(x) { return (isSxTruthy(sxEq(x, NIL)) ? [] : (isSxTruthy(isList(x)) ? x : (isSxTruthy(vector_p(x)) ? vectorToList(x) : (isSxTruthy(isString(x)) ? (function() {
|
||||
var seqToList = function(x) { return (isSxTruthy(sxEq(x, NIL)) ? [] : (isSxTruthy(isList(x)) ? x : (isSxTruthy(isVector(x)) ? vectorToList(x) : (isSxTruthy(isString(x)) ? (function() {
|
||||
var n = len(x);
|
||||
var loop = function(i, acc) { return (isSxTruthy((i < 0)) ? acc : loop((i - 1), cons(slice(x, i, (i + 1)), acc))); };
|
||||
PRIMITIVES["loop"] = loop;
|
||||
@@ -3101,23 +3105,27 @@ PRIMITIVES["sequence-to-list"] = sequenceToList;
|
||||
PRIMITIVES["sequence-to-vector"] = sequenceToVector;
|
||||
|
||||
// sequence-length
|
||||
var sequenceLength = function(s) { return (isSxTruthy(sxOr(sxEq(s, NIL), isList(s))) ? len(s) : (isSxTruthy(vector_p(s)) ? vectorLength(s) : (isSxTruthy(isString(s)) ? len(s) : len(seqToList(s))))); };
|
||||
var sequenceLength = function(s) { return (isSxTruthy(sxEq(s, NIL)) ? 0 : (isSxTruthy(isList(s)) ? len(s) : (isSxTruthy(isVector(s)) ? vectorLength(s) : (isSxTruthy(isString(s)) ? len(s) : len(seqToList(s)))))); };
|
||||
PRIMITIVES["sequence-length"] = sequenceLength;
|
||||
|
||||
// sequence-ref
|
||||
var sequenceRef = function(s, i) { return (isSxTruthy(sxOr(sxEq(s, NIL), isList(s))) ? nth(s, i) : (isSxTruthy(vector_p(s)) ? vectorRef(s, i) : (isSxTruthy(isString(s)) ? slice(s, i, (i + 1)) : nth(seqToList(s), i)))); };
|
||||
var sequenceRef = function(s, i) { return (isSxTruthy(sxOr(sxEq(s, NIL), isList(s))) ? nth(s, i) : (isSxTruthy(isVector(s)) ? vectorRef(s, i) : (isSxTruthy(isString(s)) ? slice(s, i, (i + 1)) : nth(seqToList(s), i)))); };
|
||||
PRIMITIVES["sequence-ref"] = sequenceRef;
|
||||
|
||||
// sequence-append
|
||||
var sequenceAppend = function(s1, s2) { return (isSxTruthy((isSxTruthy(vector_p(s1)) && vector_p(s2))) ? listToVector(concat(vectorToList(s1), vectorToList(s2))) : (isSxTruthy((isSxTruthy(isString(s1)) && isString(s2))) ? (String(s1) + String(s2)) : concat(seqToList(s1), seqToList(s2)))); };
|
||||
var sequenceAppend = function(s1, s2) { return (isSxTruthy((isSxTruthy(isVector(s1)) && isVector(s2))) ? listToVector(concat(vectorToList(s1), vectorToList(s2))) : (isSxTruthy((isSxTruthy(isString(s1)) && isString(s2))) ? (String(s1) + String(s2)) : concat(seqToList(s1), seqToList(s2)))); };
|
||||
PRIMITIVES["sequence-append"] = sequenceAppend;
|
||||
|
||||
// build-range
|
||||
var buildRange = function(i, end, step, acc) { return (isSxTruthy((isSxTruthy((step > 0)) ? (i >= end) : (i <= end))) ? reverse(acc) : buildRange((i + step), end, step, cons(i, acc))); };
|
||||
PRIMITIVES["build-range"] = buildRange;
|
||||
|
||||
// in-range
|
||||
var inRange = function(a) { var rest = Array.prototype.slice.call(arguments, 1); return (function() {
|
||||
var end = (isSxTruthy(isEmpty(rest)) ? a : first(rest));
|
||||
var step = (isSxTruthy((len(rest) >= 2)) ? nth(rest, 1) : 1);
|
||||
var realStart = (isSxTruthy(isEmpty(rest)) ? 0 : a);
|
||||
return (isSxTruthy(sxEq(step, 0)) ? error("in-range: step cannot be zero") : (define(build, function(i, acc) { return (isSxTruthy((isSxTruthy((step > 0)) ? (i >= end) : (i <= end))) ? reverse(acc) : build((i + step), cons(i, acc))); }), build(realStart, [])));
|
||||
return (isSxTruthy(sxEq(step, 0)) ? error("in-range: step cannot be zero") : buildRange(realStart, end, step, []));
|
||||
})(); };
|
||||
PRIMITIVES["in-range"] = inRange;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user