Coder Social home page Coder Social logo

xueshiqiao / easysequence Goto Github PK

View Code? Open in Web Editor NEW

This project forked from king-roc/easysequence

0.0 0.0 0.0 171 KB

EasySequence is a powerful fundamental library to process sequcence type, such as array, set, dictionary. All type object which conforms to NSFastEnumeration protocol can be initialzed to an EZSequence instance, then you can operation with them. Finally, you can transfer them back to the original type.

Home Page: https://cocoapods.org/pods/EasySequence

License: MIT License

Ruby 5.61% Objective-C 91.29% C 0.71% Swift 2.39%

easysequence's Introduction

EasySequence

Build Status Version License Platform codecov

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.

Requirements

iOS 8.0 or later.

Installation

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.

Features

Creating

  • can be initialized from any object which conforms to NSFastEnumeration protocol.

Enumerating

  • enumerate using forEach:
  • enumerate using forEachWithIndex:
  • enumerate using fast enumeration
  • enumerate using NSEnumerator

Operations

  • map
  • flatten map
  • filter
  • select
  • reject
  • take
  • skip
  • any
  • zip
  • reduce
  • group by
  • first object
  • first object where
  • first index where

Transfer

  • An object which conforms to EZSTransfer protocol can get an instance which transfer from EZSequence.

Thread-Safety Type

We provides thread-safety types as below:

  • EZSArray
  • EZSWeakArray
  • EZSOrderedSet
  • EZSWeakOrderedSet
  • EZSQueue

Weak Container

EZSWeakArray and EZSWeakOrderedSet form weak reference to their's items.

Example

Create a sequence

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];

Enumerating

  • 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];
}];

Operations

  • 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.

Transfer Protocol

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

Author

William Zang, [email protected]
姜沂, [email protected]
Qin Hong, [email protected]
SketchK, [email protected]

License

EasySequence is available under the MIT license. See the LICENSE file for more info.

easysequence's People

Contributors

valiantcat avatar williamzang avatar drummerchin avatar xueshiqiao avatar tamarous avatar sketchk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.