diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/ListFunctions.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/ListFunctions.java index 378691755a..93b9b1b8b4 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/ListFunctions.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/ListFunctions.java @@ -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(); @@ -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())) { @@ -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 diff --git a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java index a812c35cab..76e282b95f 100644 --- a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java +++ b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java @@ -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&)", // diff --git a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/PatternsTest.java b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/PatternsTest.java index 97bed8b8e1..e13aa1ac37 100644 --- a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/PatternsTest.java +++ b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/PatternsTest.java @@ -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}},"//