Skip to content

Commit

Permalink
Improve error messages in ReplaceList,Select
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Dec 8, 2024
1 parent fd72240 commit 8a28332
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6078,8 +6078,15 @@ public IExpr evaluate(IAST ast, EvalEngine engine) {
IExpr rules = ast.arg2();
if (ast.isAST3()) {
IExpr arg3 = engine.evaluate(ast.arg3());
if (arg3.isReal()) {
maxNumberOfResults = ((IReal) arg3).toInt();
if (!ast.arg3().isInfinity()) {
maxNumberOfResults = arg3.toIntDefault();
}
if (maxNumberOfResults < 0) {
// Non-negative integer or Infinity expected at position `1` in `2`.
return Errors.printMessage(S.ReplaceList, "innf", F.List(F.C3, ast), engine);
}
if (maxNumberOfResults == 0) {
return F.CEmptyList;
}
}
IASTAppendable result = F.ListAlloc();
Expand Down Expand Up @@ -6192,7 +6199,7 @@ public IExpr evaluate(IAST ast, int argSize, IExpr[] option, EvalEngine engine,
}
}
if (argSize == 3) {
IExpr lhs = ast.arg3();
IExpr lhs = ast.arg3();
IExpr rhs = ast.arg2();
if (lhs.isList()) {
if (lhs.exists(x -> !x.isInteger())) {
Expand Down Expand Up @@ -6765,33 +6772,37 @@ private static final class Select extends AbstractEvaluator {

@Override
public IExpr evaluate(IAST ast, EvalEngine engine) {
int size = ast.size();
if (size >= 3) {
try {

if (ast.arg1().isASTOrAssociation()) {
IAST list = (IAST) ast.arg1();
IExpr predicateHead = ast.arg2();
if (size == 3) {
return list.select(x -> engine.evalTrue(predicateHead, x));
} else if ((size == 4) && ast.arg3().isInteger()) {
final int resultLimit = Validate.checkIntType(ast, 3);
if (resultLimit == 0) {
try {
if (ast.arg1().isASTOrAssociation()) {
int maxNumberOfResults = Integer.MAX_VALUE;
IAST list = (IAST) ast.arg1();
IExpr predicateHead = ast.arg2();
if (ast.isAST2()) {
return list.select(x -> engine.evalTrue(predicateHead, x));
} else if (ast.isAST3()) {
IExpr arg3 = engine.evaluate(ast.arg3());
if (!ast.arg3().isInfinity()) {
maxNumberOfResults = arg3.toIntDefault();
if (maxNumberOfResults < 0) {
// Non-negative integer or Infinity expected at position `1` in `2`.
return Errors.printMessage(S.Select, "innf", F.List(F.C3, ast), engine);
}
if (maxNumberOfResults == 0) {
return F.CEmptyList;
}
return list.select(x -> engine.evalTrue(predicateHead, x), resultLimit);
}
return list.select(x -> engine.evalTrue(predicateHead, x), maxNumberOfResults);
}
} catch (IllegalArgumentException iae) {
return Errors.printMessage(S.Select, iae, engine);
}
} catch (IllegalArgumentException iae) {
return Errors.printMessage(S.Select, iae, engine);
}
return F.NIL;
}

@Override
public int[] expectedArgSize(IAST ast) {
return ARGS_1_3_1;
return ARGS_2_3_1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21710,6 +21710,10 @@ public void testSelect() {
"{}");
check("Select({},#1>2&)", //
"{}");
// Select: Non-negative integer or Infinity expected at position 3 in
// Select({1,2,4,7,6,2},#1>2&,-1).
check("Select({1,2,4,7,6,2},#1>2&,-1)", //
"Select({1,2,4,7,6,2},#1>2&,-1)");
check("Select({1,2,4,7,6,2},#1>2&,0)", //
"{}");
check("Select({-3, 0}, #>10&)", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,14 @@ public void testReplaceAll() {

@Test
public void testReplaceList() {
check("ReplaceList(a,b->x)", //
"{}");
// ReplaceList: Non-negative integer or Infinity expected at position 3 in
// ReplaceList(a+b+c+d+e+f,x_+y_+z_:>{{x},{y},{z}},-1).
check("ReplaceList(a+b+c+d+e+f,(x_+y_+z_) :> {{x},{y},{z}},-1)", //
"ReplaceList(a+b+c+d+e+f,x_+y_+z_:>{{x},{y},{z}},-1)");
check("ReplaceList(a+b+c+d+e+f,(x_+y_+z_) :> {{x},{y},{z}}, 0)", //
"{}");
check("ReplaceList(a+b+c+d+e+f,(x_+y_+z_) :> {{x},{y},{z}}, 3)", //
"{{{a},{b},{c+d+e+f}},"//
+ "{{a},{c},{b+d+e+f}},"//
Expand Down

0 comments on commit 8a28332

Please sign in to comment.