Giter Site home page Giter Site logo

zmlucking / octotallylazy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stuartervine/octotallylazy

0.0 2.0 0.0 1.38 MB

Functional extensions to Objective C's collections

License: Apache License 2.0

Ruby 1.03% Objective-C 97.76% Shell 1.21%

octotallylazy's Introduction

OCTotallyLazy - Functional extensions to Objective-C.

OCTotallyLazy is a framework that adds functional behaviour to Objective C collection objects, as well as a lazy collection object called Sequence. It's a partial port of Dan Bodart's TotallyLazy Java library, available here: http://code.google.com/p/totallylazy/

The best place to look for full behaviour is in the test classes for now, checkout https://github.com/stuartervine/OCTotallyLazy/blob/master/src/test-unit/SequenceTest.m

Importing OCTotallyLazy in your code.

import <OCTotallyLazy/OCTotallyLazy.h>

What's available?

NSArray

drop:
dropWhile:
filter:
find:
flatMap:
flatten
fold: with:
foreach:
isEmpty
groupBy:
grouped:
head
headOption
join:
map:
mapWithIndex:
merge:
partition:
reduce:
reverse
splitAt:
splitOn:
splitWhen:
tail
take:
takeWhile:
takeRight:
toString
toString:
toString: separator: end:
zip:
zipWithIndex

asSequence
asSet
asDictionary

Some basic examples.

Mapping (Sequence, NSArray, partially on NSSet, NSDictionary)

[sequence(@"one", @"two", @"three", nil) map:^(NSString *item){
    return [item uppercaseString];
}]
// returns sequence(@"ONE", @"TWO", @"THREE", nil)

[@[@"one", @[@"two"], @"three"] flatMap:^(NSString *item){
    return [item uppercaseString];
}];
// returns array(@"ONE", @"TWO", @"THREE", nil)

Filtering (Sequence, NSArray, NSSet, NSDictionary)

[sequence(@"1", @"12", @"123", @"1234", nil) filter:^(NSString *item){
    return item.length > 2;
}]
//returns sequence(@"123", @"1234", nil)

Options

[Option option:@"something"];
//Outputs [Some some:@"something"];

[Option option:nil];
//Outputs [None none];

[[Option option:@"something"] map:^(NSString *item){
    return [item uppercaseString];
}];
//Outputs [Some some:@"SOMETHING"];

[Option option:nil] map:^(NSString *item){
    return [item uppercaseString];
}];
//Outputs [None none];

Shorthand, for the totally lazy

The above examples are still quite noisy. There is shorthand syntax available too. Include the following above the framework import.

#define TL_SHORTHAND
#define TL_LAMBDA
#define TL_LAMBDA_SHORTHAND
import <OCTotallyLazy/OCTotallyLazy.h>

Then you can do fun stuff such as:

[sequence(num(1), num(2), num(3), nil) find:not(eqTo(num(1))]; //outputs [Some some:num(2)];

Lambda craziness

Verbose:

[sequence(@"bob", @"fred", @"wilma", nil) map:^(NSString *item){return [item uppercaseString];}] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)

A bit more sane:

[sequence(@"bob", @"fred", @"wilma", nil) map:lambda(s, [s uppercaseString])] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)

A bit mental (but a bit like scala):

[sequence(@"bob", @"fred", @"wilma", nil) map:_([_ uppercaseString])] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)

I like it - how do I get it?

So I'm a bit fed up with using 'libraries' that say, just include our source code in your project, or attach our xcode project to your project. So to use this:

  • Clone the repo.
  • Run <CHECKOUT_DIR>/build.sh test //optional, but if it fails shout at me!
  • Run <CHECKOUT_DIR>/build.sh release
  • Copy <CHECKOUT_DIR>/build/Release-iphoneos/OCTotallyLazy.framework to your external libraries folder.
  • Import the framework to your project.
  • Jobsa good 'un.

Or if you're using CocoaPods, add the following line to your Podfile

pod 'OCTotallyLazy'

OCTotallyLazy is also Carthage compatible now, just add the following to your Cartfile:

github "stuartervine/OCTotallyLazy" >= 2.0.0

Carthage compatible

octotallylazy's People

Contributors

stuartervine avatar frankleonrose avatar joshkalpin avatar andrewjackman avatar texastoland avatar

Watchers

James Cloos avatar zhangmeng 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.