NSSet(FunkyUtilities)

@interface NSSet <__covariant ObjectType>
(FunkyUtilities) @end

This extension provides simple and easy to use functional and general utilities for NSSet. If you need to prefix the extension methods in this category, you should import NSSet+FunkyPrefixedUtilities.h, where every utility method is prefixed with the funky_ keyword for compatiblitiy reasons.

See

Prefixed counterpart NSSet(FunkyPrefixedUtilities)

See

Mutable counterpart NSMutableSet(FunkyUtilities)
  • Returns whether the condition matches all elements in the set

    Declaration

    Objective-C

    - (BOOL)all:(BOOL (^)(ObjectType))block;

    Swift

    func all(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    YES if condition matches all elements, NO otherwise

  • Returns whether the condition matches no elements in the set

    Declaration

    Objective-C

    - (BOOL)none:(BOOL (^)(ObjectType))block;

    Swift

    func none(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    NO if condition matches any of the elements, YES otherwise

  • Returns whether the condition matches at least one element in the set

    Declaration

    Objective-C

    - (BOOL)contains:(BOOL (^)(ObjectType))block;

    Swift

    func contains(_ block: ((Any?) -> Bool)!) -> Bool

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    YES if condition matches any of the elements, NO otherwise

  • Returns the number of elements the given condition matches in the set, like if you would filter the set.

    Declaration

    Objective-C

    - (NSUInteger)count:(BOOL (^)(ObjectType))block;

    Swift

    func count(_ block: ((Any?) -> Bool)!) -> UInt

    Parameters

    block

    The condition given as a BOOL expression

    Return Value

    Number of occasions where the condition matches in the set

  • Returns a new NSSet instance with the same amount of elements, where each element is transformed to another by returning a new object in the block parameter.

    Declaration

    Objective-C

    - (NSSet *)map:(id (^)(ObjectType))block;

    Swift

    func map(_ block: ((Any?) -> Any?)!) -> Set

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSSet instance where each element is formed by the result of the block calls

  • Returns a new NSSet instance where each element is transformed to another by returning a new object in the block parameter. It ignores the nil parameters returned from the block.

    Declaration

    Objective-C

    - (NSSet *)nilTolerantMap:(id (^)(ObjectType))block;

    Swift

    func nilTolerantMap(_ block: ((Any?) -> Any?)!) -> Set

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSSet instance where each element is formed by the result of the block calls

  • Returns a new NSSet instance with the same amount of elements, where each element is transformed to another by returning a new object in the block parameter. Same as map, but the result is going to be flattened, so if you return an NSSet any iteration, it is going to be converted into a flat structure, not an set of sets.

    Declaration

    Objective-C

    - (NSSet *)flatMap:(id (^)(ObjectType))block;

    Swift

    func flatMap(_ block: ((Any?) -> Any?)!) -> Set

    Parameters

    block

    The transformator code which should return a new value based on the existing one.

    Return Value

    A new NSSet instance where each element is formed by the result of the flattened block calls

  • Returns a new NSSet instance with the element, that are passing the returned expression in the original set.

    See

    Mutable counterpart -[NSMutableSet(FunkyUtilities) filter:]

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)filtered:(BOOL (^)(ObjectType))block;

    Swift

    func filtered(_ block: ((Any?) -> Bool)!) -> Set

    Parameters

    block

    The filtering predicate given as a BOOL expression

    Return Value

    A new NSSet instance where each element is selected from the original one where the block returned YES

  • Flattens the set, meaning that if it consisted of Set items, they are going to be flattened into one flat structure of elements. An set of sets will transform to an set of elements from each of the previous sets. This computation is performed deeply, meaning that it contontued flattening elements, until they produce a flat structure.

    See

    Mutable counterpart -[NSMutableSet(FunkyUtilities) flattened]

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)flattened;

    Swift

    func flattened() -> Set

    Return Value

    A new NSSet instance where the elements don’t contain NSSets

  • Takes the union of the two sets.

    See

    Immutable counterpart -[NSSet(FunkyUtilities) takeUnion:]

    See

    Prefixed counterpart -[NSMutableSet(FunkyPrefixedUtilities) funky_takingUnion:]

    Declaration

    Objective-C

    - (NSSet *)takingUnion:(NSSet *)set;

    Swift

    func takingUnion(_ set: Set

    Parameters

    set

    The other collection to union with

    Return Value

    A new NSSet which is the union of the two sets

  • Takes a set which is the current minus the provided one.

    See

    Immutable counterpart -[NSSet(FunkyUtilities) takeMinus:]

    See

    Prefixed counterpart -[NSMutableSet(FunkyPrefixedUtilities) funky_takingMinus:]

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)takingMinus:(NSSet<ObjectType> *)set;

    Swift

    func takingMinus(_ set: Set

    Parameters

    set

    The other collection to minus with

    Return Value

    A new NSSet which is the minus of the two sets

  • Takes the intersection of the two sets.

    See

    Immutable counterpart -[NSSet(FunkyUtilities) takeIntersection:]

    See

    Prefixed counterpart -[NSMutableSet(FunkyPrefixedUtilities) funky_takingIntersection:]

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)takingIntersection:(NSSet<ObjectType> *)set;

    Swift

    func takingIntersection(_ set: Set

    Parameters

    set

    The other collection to intersect with

    Return Value

    A new NSSet which is the intersection of the two sets

  • Groups elements to an NSDictionary, where the returned element serves as a key, and the objects as the value. If multiple elements are returned with the same key, the result (of which is going to get included) is unpredicatble.

    See

    Prefixed counterpart -[NSSet(FunkyPrefixedUtilities) funky_groupByUsingFirst:]

    Declaration

    Objective-C

    - (NSDictionary<id, ObjectType> *)groupBy:(id (^)(ObjectType))block;

    Swift

    func group(by block: ((Any?) -> Any?)!) -> [AnyHashable : Any]!

    Parameters

    block

    A block which returns a key (based on the passed element) used as the key in the dictionary for that element.

    Return Value

    An NSDictionary where keys are produced by the result of the block call, and the values are the original elements

  • Groups elements to an NSDictionary, where the returned element serves as a key, and the objects as the value. The elements in the resulting Dictionary are sets, so if multiple elements return the same keys, all of them are going to be included in the value.

    Declaration

    Objective-C

    - (NSDictionary<id, NSSet<ObjectType> *> *)associateBy:
        (id (^)(ObjectType))block;

    Swift

    func associate(by block: ((Any?) -> Any?)!) -> [AnyHashable : Set

    Parameters

    block

    A block which returns a key (based on the passed element) used as the key in the dictionary.

    Return Value

    An NSDictionary where keys are produced by the result of the blocks, and the values are an set of original elements

  • Produces an aggregated value based on the elements of the set.

    Declaration

    Objective-C

    - (id)reduce:(id (^)(id, ObjectType))block withInitialValue:(id)start;

    Swift

    func reduce(_ block: ((Any?, Any?) -> Any?)!, withInitialValue start: Any!) -> Any!

    Parameters

    start

    The value to start with. At first this is going to be the rolling value.

    block

    The computation logic, which takes the rolling value and the current item and aggregates them using some custom logic.

    Return Value

    A custom value which is produced by computing all the elements with a custom logic, returned in block

  • A special use-case of reduce, which summarises the returned double values for each element in the set.

    Declaration

    Objective-C

    - (double)sum:(double (^)(ObjectType))block;

    Swift

    func sum(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The sum of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the average value of the returned double values for each element in the set.

    Declaration

    Objective-C

    - (double)average:(double (^)(ObjectType))block;

    Swift

    func average(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The average of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the minimum of the returned double values for each element in the set.

    Declaration

    Objective-C

    - (double)minValue:(double (^)(ObjectType))block;

    Swift

    func minValue(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The minimum of all elements, where the numbers are computed are transformed based on the original elements

  • A special use-case of reduce, which takes the maximum of the returned double values for each element in the set.

    Declaration

    Objective-C

    - (double)maxValue:(double (^)(ObjectType))block;

    Swift

    func maxValue(_ block: ((Any?) -> Double)!) -> Double

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The maximum of all elements, where the numbers are computed are transformed based on the original elements

  • Returns an set with all the minimal value elements in the set, where the minimum was computed by the returned double value for each element in the set.

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)minItems:(double (^)(ObjectType))block;

    Swift

    func minItems(_ block: ((Any?) -> Double)!) -> Set

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The set of elements with the minimal value, where the numbers are computed are transformed based on the original elements

  • Returns an set with all the maximal value elements in the set, where the maximum was computed by the returned double value for each element in the set.

    Declaration

    Objective-C

    - (NSSet<ObjectType> *)maxItems:(double (^)(ObjectType))block;

    Swift

    func maxItems(_ block: ((Any?) -> Double)!) -> Set

    Parameters

    block

    A block which returns double value, based on the current element.

    Return Value

    The set of elements with the maximal value, where the numbers are computed are transformed based on the original elements