Signal-iOS/SignalServiceKit/src/Util/FunctionalUtil.h
Michelle Linington 80c6a4e6be Add support for lightweight generics in ObjC functional utilities
NSArray supports lightweight generics in ObjC. Our extension on NSArray
for map/filter/reduce does not. This change tweaks the interface to
better support generics.

Also, fixes some tests that weren't compiling.
2020-12-11 12:30:44 -08:00

29 lines
1.0 KiB
Objective-C

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@interface NSArray <ObjectType>(FunctionalUtil)
/// Returns the first item in the array satisfying the predicate
- (nullable ObjectType)firstSatisfying:(BOOL (^)(ObjectType item))predicate;
/// Returns true when any of the items in this array match the given predicate.
- (BOOL)anySatisfy:(BOOL (^)(ObjectType item))predicate;
/// Returns true when all of the items in this array match the given predicate.
- (BOOL)allSatisfy:(BOOL (^)(ObjectType item))predicate;
/// Returns an array of all the results of passing items from this array through the given projection function.
- (NSArray *)map:(id (^)(ObjectType item))projection;
/// Returns an array of all the results of passing items from this array through the given projection function.
- (NSArray<ObjectType> *)filter:(BOOL (^)(ObjectType item))predicate;
- (NSDictionary<id, NSArray<ObjectType> *> *)groupBy:(id (^)(id value))keySelector;
@end
NS_ASSUME_NONNULL_END