Code Monkey home page Code Monkey logo

higher-order-function-objc's Introduction

Higher Order Functions in Objective-C

The project is trying to implement higher order functions with similar manners in Swift standard library. Currently Map, Filter, Reduce, Contains, ForEach and FlatMap with/without class restrictor have been implemented for NSArray.

Map:

Iterate an array and applies the same block operation to each element in it.

Without class restrictor

[array map:^id(id obj) { return [(NSString *)obj uppercaseString]; }]

With class restrictor

[array map:^id(id obj) { return [(NSString *)obj uppercaseString]; } class:[NSString class]]

Filter:

Iterate an array and return elements that meet a condition.

Without class restrictor

[array filter:^BOOL(id obj) { return [(NSString *)obj isEqualToString:@"o"]; }]

With class restrictor

[array filter:^BOOL(id obj) { return [(NSString *)obj isEqualToString:@"o"]; } class:[NSString class]]

Reduce:

Combine all elements in an array to create a single output.

Without class restrictor

[array reduce:@"Hey, " block:^id(id obj1, id obj2) { return [NSString stringWithFormat:@"%@%@", obj1, obj2]; }]

With class restrictor

[array reduce:@"Hey, " block:^id(id obj1, id obj2) { return [NSString stringWithFormat:@"%@%@", obj1, obj2]; } class:[NSString class]]

Contains:

Iterate an array and check if any element satisfies a condition.

Without class restrictor

[array contains:^BOOL(id obj) { return [(NSString *)obj isEqualToString:@"H"]; }]

With class restrictor

[array contains:^BOOL(id obj) { return [(NSString *)obj isEqualToString:@"H"]; } class:[NSString class]]

ForEach:

A short-hand for the for loop.

Without class restrictor

[array forEach:^(id obj) { NSLog(@"%@", obj); } ]

With class restrictor

[array forEach:^(id obj) { NSLog(@"%@", obj); } class:[NSString class]]

FlatMap:

Flatten an array of arrays.

Without class restrictor

[array flatMap:^id(id obj) { return obj; }]

With class restrictor

[array flatMap:^id(id obj) { return obj; } class:[NSString class]]

higher-order-function-objc's People

Contributors

romanpodymov avatar weijentu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

higher-order-function-objc's Issues

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.