package org.matheclipse.core.reflection.system;

import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.expression.IntegerSym;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;

import com.google.common.base.Predicate;

/**
 * Predicate function
 * 
 * Returns True if the 1st argument is an odd integer number;
 * False otherwise
 */
public class OddQ extends AbstractFunctionEvaluator implements Predicate {
	/**
	 * Constructor for the unary predicate
	 */
	public final static OddQ CONST = new OddQ();

	public OddQ() {
	}

	@Override
	public IExpr evaluate(final IAST ast) {
		Validate.checkSize(ast, 2);
		return F.bool(apply(ast.arg1()));
	}

	@Override
	public void setUp(final ISymbol symbol) {
		symbol.setAttributes(ISymbol.LISTABLE);
	}

	public boolean apply(final IExpr expr) {
		return expr.isInteger() && ((IntegerSym) expr).isOdd();
	}

}

 

Ads help maintain this website.