package org.matheclipse.core.reflection.system;
import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.exception.WrongNumberOfArguments;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.IStringX;
import org.matheclipse.core.interfaces.ISymbol;
import com.google.common.base.Predicate;
/**
* Returns True, if the given expression is a string which only
* contains digits.
*
*/
public class DigitQ extends AbstractFunctionEvaluator implements Predicate {
public DigitQ() {
}
@Override
public IExpr evaluate(final IAST ast) {
Validate.checkSize(ast, 2);
IStringX arg1 = Validate.checkStringType(ast, 1);
return F.bool(apply(arg1));
}
@Override
public void setUp(final ISymbol symbol) {
symbol.setAttributes(ISymbol.LISTABLE);
}
public boolean apply(final IExpr obj) {
final String str = obj.toString();
char ch;
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (!((ch >= '0') && (ch <= '9'))) {
return false;
}
}
return true;
}
}
Ads help maintain this website.