Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package jason.functions;
 

Function: math.sum(L): sums all values of L.

Examples:

  • math.sum([1,3]): returns 4.
  • math.sum([3,a,"s",5]): returns 8.
  • math.sum([]): returns 0.

Author(s):
Jomi
See also:
Min
Max
Average
public class Sum extends DefaultArithFunction  {
    public String getName() {
        return "math.sum";
    }
    
    @Override
    public double evaluate(TransitionSystem tsTerm[] argsthrows Exception {
        if (args[0].isList()) {
            double sum = 0;
            for (Term t: (ListTerm)args[0])
                if (t.isNumeric())
                    sum += ((NumberTerm)t).solve();
            return sum;
        }
        throw new JasonException(getName()+" is not implemented for type '"+args[0]+"'.");
    }
    @Override
    public boolean checkArity(int a) {
        return a == 1;
    }
New to GrepCode? Check out our FAQ X