mirror of
https://github.com/oxen-io/session-ios.git
synced 2023-12-13 21:30:14 +01:00
106 lines
2.3 KiB
Objective-C
106 lines
2.3 KiB
Objective-C
//
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
#import "FunctionalUtil.h"
|
|
|
|
@implementation NSArray (FunctionalUtil)
|
|
- (bool)any:(int (^)(id item))predicate {
|
|
OWSAssert(predicate != nil);
|
|
for (id e in self) {
|
|
if (predicate(e)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
- (bool)all:(int (^)(id item))predicate {
|
|
OWSAssert(predicate != nil);
|
|
for (id e in self) {
|
|
if (!predicate(e)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
- (id)firstMatchingElseNil:(int (^)(id item))predicate {
|
|
OWSAssert(predicate != nil);
|
|
for (id e in self) {
|
|
if (predicate(e)) {
|
|
return e;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
- (NSArray *)map:(id (^)(id item))projection {
|
|
OWSAssert(projection != nil);
|
|
|
|
NSMutableArray *r = [NSMutableArray arrayWithCapacity:self.count];
|
|
for (id e in self) {
|
|
[r addObject:projection(e)];
|
|
}
|
|
return r;
|
|
}
|
|
- (NSArray *)filter:(int (^)(id item))predicate {
|
|
OWSAssert(predicate != nil);
|
|
|
|
NSMutableArray *r = [NSMutableArray array];
|
|
for (id e in self) {
|
|
if (predicate(e)) {
|
|
[r addObject:e];
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
- (double)sumDouble {
|
|
double s = 0.0;
|
|
for (NSNumber *e in self) {
|
|
s += [e doubleValue];
|
|
}
|
|
return s;
|
|
}
|
|
- (NSUInteger)sumNSUInteger {
|
|
NSUInteger s = 0;
|
|
for (NSNumber *e in self) {
|
|
s += [e unsignedIntegerValue];
|
|
}
|
|
return s;
|
|
}
|
|
- (NSInteger)sumNSInteger {
|
|
NSInteger s = 0;
|
|
for (NSNumber *e in self) {
|
|
s += [e integerValue];
|
|
}
|
|
return s;
|
|
}
|
|
- (NSDictionary *)keyedBy:(id (^)(id value))keySelector {
|
|
OWSAssert(keySelector != nil);
|
|
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary];
|
|
|
|
for (id value in self) {
|
|
result[keySelector(value)] = value;
|
|
}
|
|
OWSAssert(result.count == self.count);
|
|
|
|
return result;
|
|
}
|
|
- (NSDictionary *)groupBy:(id (^)(id value))keySelector {
|
|
OWSAssert(keySelector != nil);
|
|
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary];
|
|
|
|
for (id item in self) {
|
|
id key = keySelector(item);
|
|
|
|
NSMutableArray *group = result[key];
|
|
if (group == nil) {
|
|
group = [NSMutableArray array];
|
|
result[key] = group;
|
|
}
|
|
[group addObject:item];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
@end
|