Giter Site home page Giter Site logo

arfunctional's Introduction

Functional categories for NSArray, NSDictionary, NSSet.

NSArray, NSDictionary, NSSet

  • and returns true if every object of the collection returns true for the given block.
  • each: calls the block passing each of the objects.
  • find returns the first object for which the block returns true.
  • reduce folds the collection to one value by running result=block(first,second), then result=block(result,next) until the end of the list.
  • map apply the block to each object and return the results in a collection of the same type.
  • or returns true if any object of the collection returns true for the given block.
  • pluck: returns an array containing the objects for the given key path.
  • split: returns an array of partitions of the original collection with up to 'size' elements each.
  • where: returns a collection with the objects for which the block returns true.
[@[@1,@2,@3] and:^BOOL(id object) { 
    return [(NSNumber*)object intValue] %2 == 0; 
}]; // false

[@[@1,@2,@3] or:^BOOL(id object) { 
    return [(NSNumber*)object intValue] %2 == 0;
}]; // true

[@[@"apple"] each:^(id object) {
    NSLog(@"%@", [object capitalizedString]); 
}]; // Apple

[@[@1,@2,@3] find:^BOOL(id object) { 
    return [(NSNumber*)object intValue] %2 == 0; 
}]; // @2

[@[@1,@2,@3] reduce:^(id a, id b){
    return [NSNumber numberWithInt:[a intValue]+[b intValue]];
}]; // 6

[@[@1,@2,@3] map:^id(id object) {
    return [NSNumber numberWithInt:[(NSNumber*)object intValue]-1];
}]; // @0,@1,@2

[@[@1,@2,@3] pluck:@"@count"]; // 3

[ [@[@1,@2,@3,@4,@5]split:3] isEqualToArray:@[ @[@1,@2,@3], @[@4,@5] ] ]; // true

[@[@1,@2,@3,@4] where:^BOOL(id object) {
	return [(NSNumber*)object intValue]%2==0;
}]; // @[@2,@4]

NSArray

  • eachWithIndex: calls the block passing each of the objects and the index of the object in the iteration.
  • head returns a number of elements at the beginning of the collection.
  • tail returns a number of elements at the end of the collection.
  • until calls the block for each object until the block returns true.
[@[@"apple"] eachWithIndex:^(id object, int index) {
    NSLog(@"%d,%@", index, [object capitalizedString]); 
}]; // 1,Apple

[@[@1,@2,@3] head:2]; // @[@1,@2]

[@[@1,@2,@3] tail:2]; // @[@2,@3]

[@[@1,@2,@3] until:^(id object) {
    BOOL result = [(NSNumber*)object intValue] %2 == 0;
    if (!result) NSLog(@"%@",object);
    return result;
}]; // 1

arfunctional's People

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.