This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString * NSStringFromIndexSet(NSIndexSet *indexSet) { | |
NSMutableString *string = nil; | |
NSRange range = NSMakeRange([indexSet firstIndex], 1); | |
while (range.location != NSNotFound) { | |
NSUInteger nextIndex = [indexSet indexGreaterThanIndex:range.location]; | |
while (nextIndex == range.location + range.length) { | |
range.length++; | |
nextIndex = [indexSet indexGreaterThanIndex:nextIndex]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/NSArray.h> | |
@interface NSArray (Predicates) | |
- (NSArray *)arrayOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate; | |
- (NSArray *)arrayOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClassAttribute(object): | |
"""Class attributes can only be accessed via class type objects. | |
Attempting to access the attribute via an instance of the class will | |
result in an AttributeError. | |
""" | |
__slots__ = ('attr',) | |
def __init__(self, attr): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_dicts(d1, d2): | |
"""Safely merges the contents of the dictionary d2 into dictionary d1. | |
Unlike dict.update(), this is a non-destructive operation: a KeyError is | |
raised if one of d2's keys already exists in d1. | |
""" | |
# Based on timing tests, it's faster to scan for conflicts upfront and | |
# then perform a bulk update. This presumes that, generally speaking, | |
# the destination dictionary d1 will be larger (in item count) than d2. | |
for key in d2.iterkeys(): |
NewerOlder