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

[question] linq_aggregate API to avoid boxing? #12

Open
dodikk opened this issue May 5, 2015 · 3 comments
Open

[question] linq_aggregate API to avoid boxing? #12

dodikk opened this issue May 5, 2015 · 3 comments

Comments

@dodikk
Copy link
Contributor

dodikk commented May 5, 2015

Are you planning to add linq_aggregate API for primitive types?

    NSNumber* totalCount = [shoppingCart linq_aggregate: ^NSNumber*(id<MyCommodity> item, NSNumber* aggregate)
    {
        NSNumber* orderedItemsCount = [item numberOfOrders];
        NSUInteger newResult = [orderedItemsCount unsignedIntegerValue] + [aggregate unsignedIntegerValue];

        return @(newResult);
    }];

In this example there is a lot of boxing/unboxing (NSNumber ==> NSUInteger ==> NSNumber) which is a good thing to avoid.

P.S. I know that integers can be "safely" casted to id. But I strongly dislike this approach. Moreover, it does not work for [NSNumber floatValue].

@dodikk
Copy link
Contributor Author

dodikk commented May 5, 2015

Note : the code example above crashes since aggregate has an unexpected type.

        if (aggregate == nil) {
            aggregate = item;
        }

Why are you checking this condition and assigning a wrong object?
If linq_aggregate is supposed to produce an object or the same type as array elements, please document it explicitly.

@ColinEberhardt what do you think about such use case?

@dodikk
Copy link
Contributor Author

dodikk commented May 5, 2015

NSArray* names = @[@"bill", @"bob", @"brian"];

id aggregate = [names linq_aggregate:^id(id item, id aggregate) {
    return [NSString stringWithFormat:@"%@, %@", aggregate, item];
}];

Eventually, the fix will break the code in the readme and the user will have to handle aggregate == nil situation properly. Still, It's a fair trade-off for the library's flexibility.

dodikk pushed a commit to healthjoy-ios-opensource/LinqToObjectiveC that referenced this issue May 5, 2015
@dodikk
Copy link
Contributor Author

dodikk commented May 5, 2015

@ColinEberhardt I've removed the mentioned check in a patch #13

Please let me know if you find a better approach for my use case.

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

No branches or pull requests

1 participant