EasySequence is a powerful fundamental library to process sequence type, such as array, set, dictionary. Each type of object which conforms to NSFastEnumeration protocol can be initialzed to an EZSequence instance, then you can operate with it. Finally, you can transfer it back to the original type.
iOS 8.0 or later.
EasySequence is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EasySequence'
To run the example project, clone the repo, and run pod install
from the Example directory first.
- can be initialized from any object which conforms to
NSFastEnumeration
protocol.
- enumerate using
forEach:
- enumerate using
forEachWithIndex:
- enumerate using fast enumeration
- enumerate using NSEnumerator
- map
- flatten map
- filter
- select
- reject
- take
- skip
- any
- zip
- reduce
- group by
- first object
- first object where
- first index where
- An object which conforms to
EZSTransfer
protocol can get an instance which transfer fromEZSequence
.
We provides thread-safety types as below:
- EZSArray
- EZSWeakArray
- EZSOrderedSet
- EZSWeakOrderedSet
- EZSQueue
EZSWeakArray
and EZSWeakOrderedSet
form weak reference to their's items.
A sequence, represented by the EZSequence
type, is a particular order in which ralated objects, or objects follow each other. For example, a NSArray
or a NSSet
is sequence ather than EZSequence
.
An EZSequence
can be initialized from any object which conforms to NSFastEnumeration
protocol.
NSArray *array = @[@1, @2, @3];
EZSequence *sequence = [[EZSequence alloc] initWithOriginSequence:array];
- Enumerate using fast enumeration
EZSequence *sequence = [[EZSequence alloc] initWithOriginSequence:@[@1, @2, @3]];
NSMutableArray *receive = [NSMutableArray array];
for (id item in sequence) {
[receive addObject:item];
}
- Enumerate using NSEnumerator
EZSequence<NSNumber *> *sequence = [[EZSequence alloc] initWithOriginSequence:@[@1, @2, @3]];
NSMutableArray<NSNumber *> *receive = [NSMutableArray array];
NSEnumerator<NSNumber *> *enumerator = [sequence objectEnumerator];
for (id item in enumerator) {
[receive addObject:item];
}
- Enumerate using block-based enumeration
EZSequence<NSNumber *> *sequence = [[EZSequence alloc] initWithOriginSequence:@[@1, @2, @3]];
NSMutableArray *receive = [NSMutableArray array];
[sequence forEach:^(NSNumber *item) {
[receive addObject:item];
}];
[sequence forEachWithIndex:^(NSNumber * _Nonnull item, NSUInteger index) {
[receive addObject:item];
}];
[sequence forEachWithIndexAndStop:^(NSNumber * _Nonnull item, NSUInteger index, BOOL * _Nonnull stop) {
if (index == 2) {
*stop = YES;
}
[receive addObject:item];
}];
- map
EZSequence<NSString *> *sequence = EZS_Sequence(@[@"111", @"222", @"333"]);
EZSequence<NSNumber *> *mappedSequence = [sequence map:^id _Nonnull(NSString * _Nonnull value) {
return @(value.integerValue);
}];
// mappedSequence => @111, @222, @333
- select
EZSequence<NSNumber *> *sequence = [@[@1, @2, @3, @4, @5, @6] EZS_asSequence];
EZSequence<NSNumber *> *oddNumbers = [sequence select:^BOOL(NSNumber * _Nonnull value) {
return value.integerValue % 2 == 1;
}];
// oddNumbers => @1, @3, @5
- filter
EZSequence *seq = EZS_Sequence(@[@"1", @"2", @"3", @"4", @"5"]);
EZSequence *filteredSeq = [seq filter:EZS_not(EZS_isEqual(@"4"))];
// filteredSeq = > @"1", @"2", @"3", @"5"
- zip
NSArray *a = @[@1, @2, @3];
NSArray *b = @[@"a", @"b"];
EZSequence<EZSequence *> *zippedSeq = [EZSequence zip:@[a, b]];
// zippedSeq => @[@1, @"a"], @[@2, @"b"]
For more examples and help, see also our unit test and API comments.
An object which conforms to EZSTransfer
protocol can get an instance which transfer from EZSequence
.
EZSequence *sequence = [[EZSequence alloc] initWithOriginSequence:@[@1, @2, @3, @2]];
// transfer to a `NSArray`
NSArray *array = [sequence as:NSArray.class];
// array => @1, @2, @3, @2
// transfer to a `NSSet`
NSSet *set = [sequence as:NSSet.class];
// set => @1, @2, @3
// transfer to a `NSOrderedSet`
NSOrderedSet *orderedSet = [sequence as:NSOrderedSet.class];
// orderedSet => @1, @2, @3
William Zang, chengwei.zang.1985@gmail.com
姜沂, nero_jy@qq.com
Qin Hong, qinhong@face2d.com
SketchK, zhangsiqi1988@gmail.com
EasySequence is available under the MIT license. See the LICENSE file for more info.