//
//  MPSumTerm.m
//  MathKit
//
//  Created by Kim Wittenburg on 14.11.14.
//  Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//

#import "MPSumTerm.h"



@implementation MPSumTerm

- (instancetype)initWithSummands:(NSArray *)summands
{
    self = [super init];
    if (self) {
        NSAssert(summands != nil, @"summands must not be nil.");
        NSAssert(summands.count > 0, @"summands must not be empty.");
        _summands = summands.copy;
    }
    return self;
}


- (NSDecimalNumber *)doEvaluation:(NSError *__autoreleasing *)error
{
    NSDecimalNumber *value = [NSDecimalNumber zero];
    for (MPTerm *summand in self.summands) {
        NSDecimalNumber *currentValue = [summand evaluate:error];
        if (!currentValue) {
            return nil;
        }
        value = [value decimalNumberByAdding:currentValue];
    }
    return value;
}

@end
