Skip to content

Commit

Permalink
Merge pull request #3440 from quantified-uncertainty/truncate-long-va…
Browse files Browse the repository at this point in the history
…rray-strings

Truncate long VArray.toString values
  • Loading branch information
OAGr authored Nov 15, 2024
2 parents 080485a + 2bcdf71 commit 034e126
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 78 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-worms-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@quri/squiggle-lang": patch
---

Truncate long lists when converting to strings
2 changes: 1 addition & 1 deletion packages/squiggle-lang/__tests__/SqValue/SqLambda_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("SqLambda", () => {
);

expect(result.ok).toBe(true);
expect(result.value.toString()).toBe("[1,2,3,4,5]");
expect(result.value.toString()).toBe("[1, 2, 3, 4, 5]");
});

test("createFromStdlibName for squiggle definition", () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/squiggle-lang/__tests__/library/danger_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { testEvalToBe } from "../helpers/reducerHelpers.js";
describe("Danger functions", () => {
describe("combinations", () => {
testEvalToBe("Danger.combinations([3,5,8],0)", "[[]]");
testEvalToBe("Danger.combinations([3,5,8],1)", "[[3],[5],[8]]");
testEvalToBe("Danger.combinations([3,5,8],2)", "[[3,5],[3,8],[5,8]]");
testEvalToBe("Danger.combinations([3,5,8],3)", "[[3,5,8]]");
testEvalToBe("Danger.combinations([3,5,8],1)", "[[3], [5], [8]]");
testEvalToBe("Danger.combinations([3,5,8],2)", "[[3, 5], [3, 8], [5, 8]]");
testEvalToBe("Danger.combinations([3,5,8],3)", "[[3, 5, 8]]");
testEvalToBe(
"Danger.combinations([3,5,8],4)",
"Error(Argument Error: Combinations of length 4 were requested, but full list is only 3 long.)"
Expand All @@ -14,11 +14,11 @@ describe("Danger functions", () => {
describe("allCombinations", () => {
testEvalToBe(
"Danger.allCombinations([3,5,8])",
"[[3],[5],[8],[3,5],[3,8],[5,8],[3,5,8]]"
"[[3], [5], [8], [3, 5], [3, 8], [5, 8], [3, 5, 8]]"
);
testEvalToBe(
"Danger.allCombinations([3,5,8])",
"[[3],[5],[8],[3,5],[3,8],[5,8],[3,5,8]]"
"[[3], [5], [8], [3, 5], [3, 8], [5, 8], [3, 5, 8]]"
);
testEvalToBe("Danger.allCombinations([3])", "[[3]]");
testEvalToBe("Danger.allCombinations([])", "[]");
Expand All @@ -29,7 +29,7 @@ describe("Danger functions", () => {
});
describe("json", () => {
testEvalToBe("Danger.json(1)", "1");
testEvalToBe("Danger.json([1,2,3])", "[1,2,3]");
testEvalToBe("Danger.json([1,2,3])", "[1, 2, 3]");
testEvalToBe(
"Danger.json({foo: 'bar'})",
'{vtype: "Dict", value: {foo: "bar"}}'
Expand Down
6 changes: 3 additions & 3 deletions packages/squiggle-lang/__tests__/library/dict_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ describe("Dict", () => {
"Dict.mergeMany([{a: 1, b: 2}, {c: 3, d: 4}, {c: 5, e: 6}])",
"{a: 1, b: 2, c: 5, d: 4, e: 6}"
);
testEvalToBe("Dict.keys({a: 1, b: 2})", '["a","b"]');
testEvalToBe("Dict.values({a: 1, b: 2})", "[1,2]");
testEvalToBe("Dict.toList({a: 1, b: 2})", '[["a",1],["b",2]]');
testEvalToBe("Dict.keys({a: 1, b: 2})", '["a", "b"]');
testEvalToBe("Dict.values({a: 1, b: 2})", "[1, 2]");
testEvalToBe("Dict.toList({a: 1, b: 2})", '[["a", 1], ["b", 2]]');
testEvalToBe("Dict.fromList([['a', 1], ['b', 2]])", "{a: 1, b: 2}");
testEvalToBe("Dict.map({a: 1, b: 2}, {|x| x * 2})", "{a: 2, b: 4}");
testEvalToBe(
Expand Down
6 changes: 3 additions & 3 deletions packages/squiggle-lang/__tests__/library/dist_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe("Dist", () => {
);
testEvalToBe(
"cumsum([Dist.make(1), Dist.make(5), Dist.make(3)])",
"[PointMass(1),PointMass(6),PointMass(9)]"
"[PointMass(1), PointMass(6), PointMass(9)]"
);
testEvalToBe(
"cumprod([Dist.make(1),Dist.make(5),Dist.make(3)])",
"[PointMass(1),PointMass(5),PointMass(15)]"
"[PointMass(1), PointMass(5), PointMass(15)]"
);
testEvalToBe(
"diff([Dist.make(1),Dist.make(5),Dist.make(3)])",
"[PointMass(4),PointMass(-2)]"
"[PointMass(4), PointMass(-2)]"
);
testEvalToBe(
"Dist.logScore({estimate: mx(Sym.normal(5,2), Sym.uniform(-1000, 1000), [.5, .5]), answer: Sym.normal(5.2,2.2)})",
Expand Down
86 changes: 43 additions & 43 deletions packages/squiggle-lang/__tests__/library/list_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe("List functions", () => {
});

describe("make", () => {
testEvalToBe("List(2, 5)", "[5,5]");
testEvalToBe("List.make(3, 'HI')", '["HI","HI","HI"]');
testEvalToBe("List.make(3, {|e| e})", "[0,1,2]");
testEvalToBe("List.make(3, {|| 1})", "[1,1,1]");
testEvalToBe("List.make(3, {|index| 1 + index})", "[1,2,3]");
testEvalToBe("List(2, 5)", "[5, 5]");
testEvalToBe("List.make(3, 'HI')", '["HI", "HI", "HI"]');
testEvalToBe("List.make(3, {|e| e})", "[0, 1, 2]");
testEvalToBe("List.make(3, {|| 1})", "[1, 1, 1]");
testEvalToBe("List.make(3, {|index| 1 + index})", "[1, 2, 3]");
testEvalToBe(
"List.make(3.5, 'HI')",
"Error(Argument Error: Number must be an integer)"
Expand All @@ -31,7 +31,7 @@ describe("List functions", () => {
});

describe("upTo", () => {
testEvalToBe("List.upTo(1,3)", "[1,2,3]");
testEvalToBe("List.upTo(1,3)", "[1, 2, 3]");
testEvalToBe(
"List.upTo(1.5,3)",
"Error(Argument Error: Low and high values must both be integers)"
Expand All @@ -56,19 +56,19 @@ describe("List functions", () => {
});

describe("concat", () => {
testEvalToBe("List.concat([1, 2, 3], [4, 5, 6])", "[1,2,3,4,5,6]");
testEvalToBe("List.concat([], [1, 2, 3])", "[1,2,3]");
testEvalToBe("List.concat(['cake'], [1, 2, 3])", '["cake",1,2,3]');
testEvalToBe("List.concat([1, 2, 3], [4, 5, 6])", "[1, 2, 3, 4, 5, 6]");
testEvalToBe("List.concat([], [1, 2, 3])", "[1, 2, 3]");
testEvalToBe("List.concat(['cake'], [1, 2, 3])", '["cake", 1, 2, 3]');
});

describe("sortBy", () => {
testEvalToBe(
"arr=[5, 2, 3, 1, 4]; List.sortBy(arr, {|n| n})",
"[1,2,3,4,5]"
"[1, 2, 3, 4, 5]"
);
testEvalToBe(
"arr=[{a: 3}, {a: 1}, {a: 2}]; List.sortBy(arr, {|obj| obj.a})",
"[{a: 1},{a: 2},{a: 3}]"
"[{a: 1}, {a: 2}, {a: 3}]"
);
testEvalToBe(
"arr=[{a: '3'}, {a: '1'}, {a: '2'}]; List.sortBy(arr, {|obj| obj.a})",
Expand All @@ -78,8 +78,8 @@ describe("List functions", () => {
// Edge cases
testEvalToBe("arr=[]; List.sortBy(arr, {|n| n})", "[]"); // Empty array
testEvalToBe("arr=[1]; List.sortBy(arr, {|n| n})", "[1]"); // Single-element array
testEvalToBe("arr=[3, 2, 1]; List.sortBy(arr, {|n| -n})", "[3,2,1]"); // Descending order
testEvalToBe("arr=[1, 2, 3]; List.sortBy(arr, {|n| 0})", "[1,2,3]"); // Lambda function always returns the same value
testEvalToBe("arr=[3, 2, 1]; List.sortBy(arr, {|n| -n})", "[3, 2, 1]"); // Descending order
testEvalToBe("arr=[1, 2, 3]; List.sortBy(arr, {|n| 0})", "[1, 2, 3]"); // Lambda function always returns the same value
});

describe("minBy", () => {
Expand Down Expand Up @@ -107,74 +107,74 @@ describe("List functions", () => {
});

describe("reverse", () => {
testEvalToBe("List.reverse([3,5,8])", "[8,5,3]");
testEvalToBe("List.reverse([3, 5, 8])", "[8, 5, 3]");
testEvalToBe("List.reverse([])", "[]");
});

describe("append", () => {
testEvalToBe("List.append([3,5,8], 8)", "[3,5,8,8]");
testEvalToBe("List.append([3, 5, 8], 8)", "[3, 5, 8, 8]");
testEvalToBe("List.append([], 8)", "[8]");
});

describe("map", () => {
testEvalToBe("arr=[1,2,3]; map(arr, {|x| x*2})", "[2,4,6]");
testEvalToBe("arr=[1,2,3]; map(arr, {|x| x*2})", "[2, 4, 6]");
testEvalToBe(
"double(x)=2*x; arr=[1,2,3]; List.map(arr, double)",
"[2,4,6]"
"[2, 4, 6]"
);
testEvalToBe("double(x)=2*x; arr=[1,2,3]; map(arr, double)", "[2,4,6]");
testEvalToBe("double(x)=2*x; arr=[1,2,3]; map(arr, double)", "[2, 4, 6]");

// wrong arg types
testEvalError("addone(x)=x+1; map(2, addone)");
testEvalError("addone(x)=x+1; map(2, {x: addone})");

// two-arg callback
testEvalToBe("[10,20,30] -> List.map({|x,i|x+i+1})", "[11,22,33]");
testEvalToBe("[10, 20, 30] -> List.map({|x,i|x+i+1})", "[11, 22, 33]");
testEvalToBe("List.map([[1]], Number.sum)", "[1]");
});

describe("slice", () => {
testEvalToBe("List.slice([1,2,3,4,5,6], 2)", "[3,4,5,6]");
testEvalToBe("List.slice([1,2,3,4,5,6], 2, 4)", "[3,4]");
testEvalToBe("List.slice([1,2,3,4,5,6], 8, 3)", "[]");
testEvalToBe("List.slice([1, 2, 3, 4, 5, 6], 2)", "[3, 4, 5, 6]");
testEvalToBe("List.slice([1, 2, 3, 4, 5, 6], 2, 4)", "[3, 4]");
testEvalToBe("List.slice([1, 2, 3, 4, 5, 6], 8, 3)", "[]");
testEvalToBe("List.slice([], 8, 3)", "[]");
testEvalToBe("List.slice([1,2,3,4,5,6], -4)", "[3,4,5,6]");
testEvalToBe("List.slice([1,2,3,4,5,6], 2, -1)", "[3,4,5]");
testEvalToBe("List.slice([1, 2, 3, 4, 5, 6], -4)", "[3, 4, 5, 6]");
testEvalToBe("List.slice([1, 2, 3, 4, 5, 6], 2, -1)", "[3, 4, 5]");
testEvalToBe(
"List.slice([], 3.5, 3)",
"Error(Argument Error: Number 3.5 must be an integer)"
);
});

describe("uniq", () => {
testEvalToBe("arr=[1,2,3,1,2,3]; List.uniq(arr)", "[1,2,3]");
testEvalToBe("arr=[1,'1']; List.uniq(arr)", '[1,"1"]');
testEvalToBe("arr=[1, 2, 3, 1, 2, 3]; List.uniq(arr)", "[1, 2, 3]");
testEvalToBe("arr=[1, '1']; List.uniq(arr)", '[1, "1"]');
testEvalToBe(
"arr=[1,1, 'test', 'test', false, false, true]; List.uniq(arr)",
'[1,"test",false,true]'
'[1, "test", false, true]'
);
testEvalToBe(
"arr=[1,2,normal(50,1)]; List.uniq(arr)",
"[1,2,Sample Set Distribution]"
"[1, 2, Sample Set Distribution]"
);
});

describe("uniqBy", () => {
testEvalToBe(
"arr=[1.2, 1.6, 2.3, 2.8]; List.uniqBy(arr, floor)",
"[1.2,2.3]"
"[1.2, 2.3]"
);
testEvalToBe(
"arr=[{a: 1, b: 2}, {a: 1, b: 3}, {a:2, b:5}]; List.uniqBy(arr, {|e| e.a})",
"[{a: 1, b: 2},{a: 2, b: 5}]"
"[{a: 1, b: 2}, {a: 2, b: 5}]"
);
testEvalToBe(
"arr=[{a: normal(5,2), b: 2}, {a: 1, b: 3}, {a:2, b:5}]; List.uniqBy(arr, {|e| e.a})",
"[{a: Sample Set Distribution, b: 2},{a: 1, b: 3},{a: 2, b: 5}]"
"[{a: Sample Set Distribution, b: 2}, {a: 1, b: 3}, {a: 2, b: 5}]"
);
testEvalToBe(
"arr=[{a: normal(5,2), b: 2}, {a: 1, b: 3}, {a:2, b:3}]; List.uniqBy(arr, {|e| e.b})",
"[{a: Sample Set Distribution, b: 2},{a: 1, b: 3}]"
"[{a: Sample Set Distribution, b: 2}, {a: 1, b: 3}]"
);
});

Expand Down Expand Up @@ -243,11 +243,11 @@ describe("List functions", () => {
});

describe("reverse", () => {
testEvalToBe("arr=[1,2,3]; List.reverse(arr)", "[3,2,1]");
testEvalToBe("arr=[1,2,3]; List.reverse(arr)", "[3, 2, 1]");
});

describe("filter", () => {
testEvalToBe("arr=[1,2,3]; List.filter(arr,{|e| e > 1})", "[2,3]");
testEvalToBe("arr=[1,2,3]; List.filter(arr,{|e| e > 1})", "[2, 3]");
testEvalToBe("arr=[1,2,3]; List.filter(arr,{|e| e > 5})", "[]");
});

Expand Down Expand Up @@ -283,19 +283,19 @@ describe("List functions", () => {
});

describe("flatten", () => {
testEvalToBe("List.flatten([[1,2], [3,4]])", "[1,2,3,4]");
testEvalToBe("List.flatten([[1,2], [3,[4,5]]])", "[1,2,3,[4,5]]");
testEvalToBe("List.flatten([[1, 2], [3, 4]])", "[1, 2, 3, 4]");
testEvalToBe("List.flatten([[1, 2], [3, [4, 5]]])", "[1, 2, 3, [4, 5]]");
testEvalToBe("List.flatten([])", "[]");
testEvalToBe("List.flatten([[],[],[]])", "[]");
testEvalToBe("List.flatten([[], [], []])", "[]");
});

describe("zip", () => {
testEvalToBe("List.zip([1,2], [3,4])", "[[1,3],[2,4]]");
testEvalToBe("List.zip([1, 2], [3, 4])", "[[1, 3], [2, 4]]");
testEvalToBe(
"List.zip([1,2,4], [3,4])",
"List.zip([1, 2, 4], [3, 4])",
"Error(Argument Error: List lengths must be equal)"
);
testEvalToBe("List.zip([1,2], [3,[4,5]])", "[[1,3],[2,[4,5]]]");
testEvalToBe("List.zip([1, 2], [3, [4, 5]])", "[[1, 3], [2, [4, 5]]]");
testEvalToBe(
"List.zip([1,2], [3,[4,5], [5]])",
"Error(Argument Error: List lengths must be equal)"
Expand All @@ -304,9 +304,9 @@ describe("List functions", () => {
});

describe("unzip", () => {
testEvalToBe("List.unzip([[1,3],[2,4]])", "[[1,2],[3,4]]");
testEvalToBe("List.unzip([[1,3],[2,4],[5,6]])", "[[1,2,5],[3,4,6]]");
testEvalToBe("List.unzip([])", "[[],[]]");
testEvalToBe("List.unzip([[1,3],[2,4]])", "[[1, 2], [3, 4]]");
testEvalToBe("List.unzip([[1,3],[2,4],[5,6]])", "[[1, 2, 5], [3, 4, 6]]");
testEvalToBe("List.unzip([])", "[[], []]");
});

describe("sample", () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/squiggle-lang/__tests__/library/number_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ describe("Numbers", () => {
testEvalToBe("Number.quantile([0,5,10,15,20], 0.25)", "5");
testEvalToBe("Number.median([0,5,10,15,20])", "10");
testEvalToBe("Number.sort([])", "[]");
testEvalToBe("Number.sort([10,0,15,5])", "[0,5,10,15]");
testEvalToBe("Number.sort([10,0,15,5])", "[0, 5, 10, 15]");
testEvalToBe("Number.cumsum([])", "[]");
testEvalToBe("Number.cumsum([1,5,3])", "[1,6,9]");
testEvalToBe("Number.cumsum([1,5,3])", "[1, 6, 9]");
testEvalToBe("Number.cumprod([])", "[]");
testEvalToBe("Number.cumprod([1,5,3])", "[1,5,15]");
testEvalToBe("Number.cumprod([1,5,3])", "[1, 5, 15]");
testEvalToBe("Number.diff([1])", "[]");
testEvalToBe("Number.diff([1,5,3])", "[4,-2]");
testEvalToBe("Number.diff([1,5,3])", "[4, -2]");
testEvalToBe("Number.mod(10, 3)", "1");
testEvalToBe("mod(10, 3)", "1");
testEvalToBe("Number.mod(15, 4)", "3");
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/__tests__/library/pointset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ describe("Discrete", () => {
describe("MixedSet", () => {
testEvalToBe(
"MixedSet.difference(PointSet.support(mx(Sym.uniform(1, 5), Sym.uniform(10, 30))), PointSet.support(mx(Sym.uniform(3, 8), Sym.uniform(20, 22))))",
"{points: [], segments: [[0.9999999996,2.9999999995],[9.999999998,19.9999999998],[22.0000000002,30.000000002]]}"
"{points: [], segments: [[0.9999999996, 2.9999999995], [9.999999998, 19.9999999998], [22.0000000002, 30.000000002]]}"
);
});
8 changes: 4 additions & 4 deletions packages/squiggle-lang/__tests__/library/sampleSet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Various SampleSet functions", () => {
);
testEvalToBe(
"addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))",
"[2,3,4,5,6,7]"
"[2, 3, 4, 5, 6, 7]"
);
testEvalToBe(
"SampleSet.fromList([1, 2, 3])",
Expand All @@ -46,17 +46,17 @@ describe("mapN", () => {
// equal length
testEvalToBe(
sq`SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6]), SampleSet.fromList([6,5,4,3,2,1])], {|x| x[0] > x[1] ? x[0] : x[1]}) -> SampleSet.toList`,
"[6,5,4,4,5,6]"
"[6, 5, 4, 4, 5, 6]"
);

// unequal length
testEvalToBe(
sq`SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6]), SampleSet.fromList([6,5,4,3,2,1,1,1])], {|x| x[0] > x[1] ? x[0] : x[1]}) -> SampleSet.toList`,
"[6,5,4,4,5,6]"
"[6, 5, 4, 4, 5, 6]"
);
testEvalToBe(
sq`SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6,1,1]), SampleSet.fromList([6,5,4,3,2,1])], {|x| x[0] > x[1] ? x[0] : x[1]}) -> SampleSet.toList`,
"[6,5,4,4,5,6]"
"[6, 5, 4, 4, 5, 6]"
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/__tests__/reducer/array_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { testEvalToBe } from "../helpers/reducerHelpers.js";

describe("Array", () => {
testEvalToBe("[1, 2]", "[1,2]");
testEvalToBe("[1, 2]", "[1, 2]");
testEvalToBe("[]", "[]");
testEvalToBe(
"f(x)=x; g(x)=x; [f, g]",
"[(x) => internal code,(x) => internal code]"
"[(x) => internal code, (x) => internal code]"
);
});
10 changes: 5 additions & 5 deletions packages/squiggle-lang/__tests__/reducer/various_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("eval", () => {
testEvalToBe("!true", "false");
testEvalToBe("1 + -1", "0");
testEvalToBe('"hello"', '"hello"');
testEvalToBe("concat([3,4], [5,6,7])", "[3,4,5,6,7]");
testEvalToBe("concat([3,4], [5,6,7])", "[3, 4, 5, 6, 7]");
testEvalToBe("log(10)", "2.302585092994046");
testEvalToBe("Math.cos(10)", "-0.8390715290764524");
// most library tests are in __tests__/library/
Expand All @@ -39,22 +39,22 @@ describe("eval", () => {
test("empty array", async () => {
await expectEvalToBe("[]", "[]");
});
testEvalToBe("[1, 2, 3]", "[1,2,3]");
testEvalToBe("['hello', 'world']", '["hello","world"]');
testEvalToBe("[1, 2, 3]", "[1, 2, 3]");
testEvalToBe("['hello', 'world']", '["hello", "world"]');
testEvalToBe("([0,1,2])[1]", "1");
testDescriptionEvalToBe(
"index not found",
"([0,1,2])[10]",
"Error(Array index not found: 10)"
);
test("trailing comma", async () => {
await expectEvalToBe(`[3,4,]`, "[3,4]");
await expectEvalToBe(`[3,4,]`, "[3, 4]");
await expectEvalToBe(
`[
3,
4,
]`,
"[3,4]"
"[3, 4]"
);
});
});
Expand Down
Loading

0 comments on commit 034e126

Please sign in to comment.