Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASTChain #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add ASTChain #46

wants to merge 1 commit into from

Conversation

robb
Copy link
Owner

@robb robb commented May 22, 2014

This adds ASTChain, a helper macro to reduce boilerplate in cases where intermediate results of a transformation would pollute the current scope.

E.g.

NSArray *messages = ASTPluck(tweets, @keypath(XYTweet.new, message));

NSArray *strings = ASTFilter(values, ^(id obj) {
    return [obj isKindOfClass:NSString.class];
});

NSArray *result = ASTMap(values, ^(NSString *string) {
    return string.resultString;
});

NSLog(@"%@", result);

could be replaced with

NSArray *result = ASTChain(
    ASTPluck(tweets, @keypath(XYTweet.new, message)),
    ASTFilter(_, ^(id obj) {
        return [obj isKindOfClass:NSString.class];
    }),
    ASTMap(_, ^(NSString *string) {
        return string.capitalizedString;
    })
);

NSLog(@"%@", result);

removing the need for temporary messages and strings variables.

I think it's worth pointing out that the _ variable contains the result and has the type of the previous expression. In the above example, this makes sure that the correct ASTMap version is picked (in this case the one for arrays).

Would love to hear some opinions on this, since it may just be a little to much macro-magic…

@felixvisee
Copy link
Collaborator

Should be ok?! I'm wondering whether it would make (style-)sense to pass the initial value separately, giving all transformations the same form:

NSArray *result = ASTChain(tweets,
    ASTPluck(_, @keypath(XYTweet.new, message)),
    ASTFilter(_, ^(id obj) {
        return [obj isKindOfClass:NSString.class];
    }),
    ASTMap(_, ^(NSString *string) {
        return string.capitalizedString;
    })
);

NSLog(@"%@", result);

@robb
Copy link
Owner Author

robb commented May 22, 2014

That works already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants