From 29e4bcffabf6d3de06d57cda4dc229dbcc1e45d7 Mon Sep 17 00:00:00 2001 From: "blehner@10.100.20.62" Date: Fri, 17 Oct 2014 10:41:53 -0500 Subject: [PATCH] Added the 'sum' operation to the NSArray method extensions and unit tests. --- .../LinqToObjectiveCTestTests/NSArrayLinqExtensionsTest.m | 8 ++++++++ NSArray+LinqExtensions.h | 6 ++++++ NSArray+LinqExtensions.m | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/LinqToObjectiveCTest/LinqToObjectiveCTestTests/NSArrayLinqExtensionsTest.m b/LinqToObjectiveCTest/LinqToObjectiveCTestTests/NSArrayLinqExtensionsTest.m index 4c008f5..c25a0ce 100755 --- a/LinqToObjectiveCTest/LinqToObjectiveCTestTests/NSArrayLinqExtensionsTest.m +++ b/LinqToObjectiveCTest/LinqToObjectiveCTestTests/NSArrayLinqExtensionsTest.m @@ -478,4 +478,12 @@ - (void) testReverse STAssertEqualObjects(result[1], @25, nil); } +- (void) testSum +{ + NSArray* input = @[@25, @35]; + + NSNumber* sum = [input linq_sum]; + STAssertEqualObjects(sum, @60, nil); +} + @end diff --git a/NSArray+LinqExtensions.h b/NSArray+LinqExtensions.h index 8d51020..10478ba 100644 --- a/NSArray+LinqExtensions.h +++ b/NSArray+LinqExtensions.h @@ -194,4 +194,10 @@ typedef id (^LINQAccumulator)(id item, id aggregate); */ - (NSArray*) linq_reverse; +/** Sums the elements in the array. + + @return The sum of elements within this array. + */ +- (NSNumber*)linq_sum; + @end diff --git a/NSArray+LinqExtensions.m b/NSArray+LinqExtensions.m index ceba6b8..9801d8d 100644 --- a/NSArray+LinqExtensions.m +++ b/NSArray+LinqExtensions.m @@ -237,5 +237,9 @@ - (NSArray *)linq_reverse return result; } +- (NSNumber *)linq_sum +{ + return [self valueForKeyPath: @"@sum.self"]; +} @end