Declared in NSIndexSet+BlocksKit.h

Overview

Block extensions for NSIndexSet.

Both inspired by and resembling Smalltalk syntax, these utilities allows for iteration of an array in a concise way that saves quite a bit of boilerplate code for filtering or finding objects or an object.

Includes code by the following:

Tasks

  • – bk_each:

    Loops through an index set and executes the given block at each index.

  • – bk_apply:

    Enumerates each index in an index set concurrently and executes the given block once per index.

  • – bk_match:

    Loops through an array and returns the index matching the block.

  • – bk_select:

    Loops through an index set and returns an all indexes matching the block.

  • – bk_reject:

    Loops through an index set and returns an all indexes but the ones matching the block.

  • – bk_map:

    Call the block once for each index and create an index set with the new values.

  • – bk_mapIndex:

    Call the block once for each index and create an array of the return values.

  • – bk_any:

    Loops through an index set to find whether any of the indexes matche the block.

  • – bk_all:

    Loops through an index set to find whether all objects match the block.

  • – bk_none:

    Loops through an index set to find whether no objects match the block.

Instance Methods

bk_all:

- (BOOL)bk_all:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set to find whether all objects match the block.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

YES if the block returns YES for all indexes in the array, NO otherwise.

Declared In

NSIndexSet+BlocksKit.h

bk_any:

- (BOOL)bk_any:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set to find whether any of the indexes matche the block.

This method is similar to the Scala list exists. It is functionally identical to bk_match: but returns a BOOL instead. It is not recommended to use bk_any: as a check condition before executing bk_match:, since it would require two loops through the index set.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

YES for the first time the block returns YES for an index, NO otherwise.

Declared In

NSIndexSet+BlocksKit.h

bk_apply:

- (void)bk_apply:(void ( ^ ) ( NSUInteger index ))block
Discussion

Enumerates each index in an index set concurrently and executes the given block once per index.

Enumeration will occur on appropriate background queues. Be aware that the block will not necessarily be executed in order for each index.

Parameters

block

A single-argument, void-returning code block.

Declared In

NSIndexSet+BlocksKit.h

bk_each:

- (void)bk_each:(void ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set and executes the given block at each index.

Parameters

block

A single-argument, void-returning code block.

Declared In

NSIndexSet+BlocksKit.h

bk_map:

- (NSIndexSet *)bk_map:(NSUInteger ( ^ ) ( NSUInteger index ))block
Discussion

Call the block once for each index and create an index set with the new values.

Parameters

block

A block that returns a new index for an index.

Return Value

An index set of the indexes returned by the block.

Declared In

NSIndexSet+BlocksKit.h

bk_mapIndex:

- (NSArray *)bk_mapIndex:(id ( ^ ) ( NSUInteger index ))block
Discussion

Call the block once for each index and create an array of the return values.

This method allows transforming indexes into objects: int values[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }; NSIndexSet idxs = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 10)]; NSArray new = [idxs mapIndex:^id(NSUInteger index) { return [NSNumber numberWithInt:values[index]]); }];

Parameters

block

A block that returns an object for an index.

Return Value

Returns an array of the objects returned by the block.

Declared In

NSIndexSet+BlocksKit.h

bk_match:

- (NSUInteger)bk_match:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an array and returns the index matching the block.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

Returns the index if found, NSNotFound otherwise.

See Also

Declared In

NSIndexSet+BlocksKit.h

bk_none:

- (BOOL)bk_none:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set to find whether no objects match the block.

This selector performs literally the exact same function as bk_all: but in reverse.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

YES if the block returns NO for all indexes in the array, NO otherwise.

Declared In

NSIndexSet+BlocksKit.h

bk_reject:

- (NSIndexSet *)bk_reject:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set and returns an all indexes but the ones matching the block.

This selector performs literally the exact same function as bk_select: but in reverse.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

Returns an index set of all indexes but those matched.

Declared In

NSIndexSet+BlocksKit.h

bk_select:

- (NSIndexSet *)bk_select:(BOOL ( ^ ) ( NSUInteger index ))block
Discussion

Loops through an index set and returns an all indexes matching the block.

Parameters

block

A single-argument, BOOL-returning code block.

Return Value

Returns an index set of matching indexes found.

See Also

Declared In

NSIndexSet+BlocksKit.h