Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
374 additions
and
0 deletions.
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
assets/2014-05-24-tail-recursion-objc-and-arc/Tail.xcodeproj/project.pbxproj
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
// !$*UTF8*$! | ||
{ | ||
archiveVersion = 1; | ||
classes = { | ||
}; | ||
objectVersion = 46; | ||
objects = { | ||
|
||
/* Begin PBXBuildFile section */ | ||
0B990ECB19314C99005CCDCE /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0B990ECA19314C99005CCDCE /* Foundation.framework */; }; | ||
0B990ECE19314C99005CCDCE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B990ECD19314C99005CCDCE /* main.m */; }; | ||
/* End PBXBuildFile section */ | ||
|
||
/* Begin PBXCopyFilesBuildPhase section */ | ||
0B990EC519314C99005CCDCE /* CopyFiles */ = { | ||
isa = PBXCopyFilesBuildPhase; | ||
buildActionMask = 2147483647; | ||
dstPath = /usr/share/man/man1/; | ||
dstSubfolderSpec = 0; | ||
files = ( | ||
); | ||
runOnlyForDeploymentPostprocessing = 1; | ||
}; | ||
/* End PBXCopyFilesBuildPhase section */ | ||
|
||
/* Begin PBXFileReference section */ | ||
0B990EC719314C99005CCDCE /* Tail */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Tail; sourceTree = BUILT_PRODUCTS_DIR; }; | ||
0B990ECA19314C99005CCDCE /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; | ||
0B990ECD19314C99005CCDCE /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; usesTabs = 0; }; | ||
/* End PBXFileReference section */ | ||
|
||
/* Begin PBXFrameworksBuildPhase section */ | ||
0B990EC419314C99005CCDCE /* Frameworks */ = { | ||
isa = PBXFrameworksBuildPhase; | ||
buildActionMask = 2147483647; | ||
files = ( | ||
0B990ECB19314C99005CCDCE /* Foundation.framework in Frameworks */, | ||
); | ||
runOnlyForDeploymentPostprocessing = 0; | ||
}; | ||
/* End PBXFrameworksBuildPhase section */ | ||
|
||
/* Begin PBXGroup section */ | ||
0B990EBE19314C99005CCDCE = { | ||
isa = PBXGroup; | ||
children = ( | ||
0B990ECD19314C99005CCDCE /* main.m */, | ||
0B990EC919314C99005CCDCE /* Frameworks */, | ||
0B990EC819314C99005CCDCE /* Products */, | ||
); | ||
sourceTree = "<group>"; | ||
}; | ||
0B990EC819314C99005CCDCE /* Products */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
0B990EC719314C99005CCDCE /* Tail */, | ||
); | ||
name = Products; | ||
sourceTree = "<group>"; | ||
}; | ||
0B990EC919314C99005CCDCE /* Frameworks */ = { | ||
isa = PBXGroup; | ||
children = ( | ||
0B990ECA19314C99005CCDCE /* Foundation.framework */, | ||
); | ||
name = Frameworks; | ||
sourceTree = "<group>"; | ||
}; | ||
/* End PBXGroup section */ | ||
|
||
/* Begin PBXNativeTarget section */ | ||
0B990EC619314C99005CCDCE /* Tail */ = { | ||
isa = PBXNativeTarget; | ||
buildConfigurationList = 0B990ED519314C99005CCDCE /* Build configuration list for PBXNativeTarget "Tail" */; | ||
buildPhases = ( | ||
0B990EC319314C99005CCDCE /* Sources */, | ||
0B990EC419314C99005CCDCE /* Frameworks */, | ||
0B990EC519314C99005CCDCE /* CopyFiles */, | ||
); | ||
buildRules = ( | ||
); | ||
dependencies = ( | ||
); | ||
name = Tail; | ||
productName = Tail; | ||
productReference = 0B990EC719314C99005CCDCE /* Tail */; | ||
productType = "com.apple.product-type.tool"; | ||
}; | ||
/* End PBXNativeTarget section */ | ||
|
||
/* Begin PBXProject section */ | ||
0B990EBF19314C99005CCDCE /* Project object */ = { | ||
isa = PBXProject; | ||
attributes = { | ||
LastUpgradeCheck = 0510; | ||
ORGANIZATIONNAME = Fitbit; | ||
}; | ||
buildConfigurationList = 0B990EC219314C99005CCDCE /* Build configuration list for PBXProject "Tail" */; | ||
compatibilityVersion = "Xcode 3.2"; | ||
developmentRegion = English; | ||
hasScannedForEncodings = 0; | ||
knownRegions = ( | ||
en, | ||
); | ||
mainGroup = 0B990EBE19314C99005CCDCE; | ||
productRefGroup = 0B990EC819314C99005CCDCE /* Products */; | ||
projectDirPath = ""; | ||
projectRoot = ""; | ||
targets = ( | ||
0B990EC619314C99005CCDCE /* Tail */, | ||
); | ||
}; | ||
/* End PBXProject section */ | ||
|
||
/* Begin PBXSourcesBuildPhase section */ | ||
0B990EC319314C99005CCDCE /* Sources */ = { | ||
isa = PBXSourcesBuildPhase; | ||
buildActionMask = 2147483647; | ||
files = ( | ||
0B990ECE19314C99005CCDCE /* main.m in Sources */, | ||
); | ||
runOnlyForDeploymentPostprocessing = 0; | ||
}; | ||
/* End PBXSourcesBuildPhase section */ | ||
|
||
/* Begin XCBuildConfiguration section */ | ||
0B990ED319314C99005CCDCE /* Debug */ = { | ||
isa = XCBuildConfiguration; | ||
buildSettings = { | ||
ALWAYS_SEARCH_USER_PATHS = NO; | ||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; | ||
CLANG_CXX_LIBRARY = "libc++"; | ||
CLANG_ENABLE_MODULES = YES; | ||
CLANG_ENABLE_OBJC_ARC = YES; | ||
CLANG_WARN_BOOL_CONVERSION = YES; | ||
CLANG_WARN_CONSTANT_CONVERSION = YES; | ||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; | ||
CLANG_WARN_EMPTY_BODY = YES; | ||
CLANG_WARN_ENUM_CONVERSION = YES; | ||
CLANG_WARN_INT_CONVERSION = YES; | ||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; | ||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; | ||
COPY_PHASE_STRIP = NO; | ||
GCC_C_LANGUAGE_STANDARD = gnu99; | ||
GCC_DYNAMIC_NO_PIC = NO; | ||
GCC_ENABLE_OBJC_EXCEPTIONS = YES; | ||
GCC_OPTIMIZATION_LEVEL = 3; | ||
GCC_PREPROCESSOR_DEFINITIONS = ( | ||
"DEBUG=1", | ||
"$(inherited)", | ||
); | ||
GCC_SYMBOLS_PRIVATE_EXTERN = NO; | ||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; | ||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; | ||
GCC_WARN_UNDECLARED_SELECTOR = YES; | ||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; | ||
GCC_WARN_UNUSED_FUNCTION = YES; | ||
GCC_WARN_UNUSED_VARIABLE = YES; | ||
MACOSX_DEPLOYMENT_TARGET = 10.9; | ||
ONLY_ACTIVE_ARCH = YES; | ||
SDKROOT = macosx; | ||
}; | ||
name = Debug; | ||
}; | ||
0B990ED419314C99005CCDCE /* Release */ = { | ||
isa = XCBuildConfiguration; | ||
buildSettings = { | ||
ALWAYS_SEARCH_USER_PATHS = NO; | ||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; | ||
CLANG_CXX_LIBRARY = "libc++"; | ||
CLANG_ENABLE_MODULES = YES; | ||
CLANG_ENABLE_OBJC_ARC = YES; | ||
CLANG_WARN_BOOL_CONVERSION = YES; | ||
CLANG_WARN_CONSTANT_CONVERSION = YES; | ||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; | ||
CLANG_WARN_EMPTY_BODY = YES; | ||
CLANG_WARN_ENUM_CONVERSION = YES; | ||
CLANG_WARN_INT_CONVERSION = YES; | ||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; | ||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; | ||
COPY_PHASE_STRIP = YES; | ||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; | ||
ENABLE_NS_ASSERTIONS = NO; | ||
GCC_C_LANGUAGE_STANDARD = gnu99; | ||
GCC_ENABLE_OBJC_EXCEPTIONS = YES; | ||
GCC_OPTIMIZATION_LEVEL = 3; | ||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; | ||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; | ||
GCC_WARN_UNDECLARED_SELECTOR = YES; | ||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; | ||
GCC_WARN_UNUSED_FUNCTION = YES; | ||
GCC_WARN_UNUSED_VARIABLE = YES; | ||
MACOSX_DEPLOYMENT_TARGET = 10.9; | ||
SDKROOT = macosx; | ||
}; | ||
name = Release; | ||
}; | ||
0B990ED619314C99005CCDCE /* Debug */ = { | ||
isa = XCBuildConfiguration; | ||
buildSettings = { | ||
CLANG_ENABLE_OBJC_ARC = YES; | ||
GCC_OPTIMIZATION_LEVEL = 1; | ||
PRODUCT_NAME = "$(TARGET_NAME)"; | ||
}; | ||
name = Debug; | ||
}; | ||
0B990ED719314C99005CCDCE /* Release */ = { | ||
isa = XCBuildConfiguration; | ||
buildSettings = { | ||
CLANG_ENABLE_OBJC_ARC = YES; | ||
GCC_OPTIMIZATION_LEVEL = s; | ||
PRODUCT_NAME = "$(TARGET_NAME)"; | ||
}; | ||
name = Release; | ||
}; | ||
/* End XCBuildConfiguration section */ | ||
|
||
/* Begin XCConfigurationList section */ | ||
0B990EC219314C99005CCDCE /* Build configuration list for PBXProject "Tail" */ = { | ||
isa = XCConfigurationList; | ||
buildConfigurations = ( | ||
0B990ED319314C99005CCDCE /* Debug */, | ||
0B990ED419314C99005CCDCE /* Release */, | ||
); | ||
defaultConfigurationIsVisible = 0; | ||
defaultConfigurationName = Release; | ||
}; | ||
0B990ED519314C99005CCDCE /* Build configuration list for PBXNativeTarget "Tail" */ = { | ||
isa = XCConfigurationList; | ||
buildConfigurations = ( | ||
0B990ED619314C99005CCDCE /* Debug */, | ||
0B990ED719314C99005CCDCE /* Release */, | ||
); | ||
defaultConfigurationIsVisible = 0; | ||
defaultConfigurationName = Release; | ||
}; | ||
/* End XCConfigurationList section */ | ||
}; | ||
rootObject = 0B990EBF19314C99005CCDCE /* Project object */; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// | ||
// Created by Jonathon Mah on 2014-05-24. | ||
// Released into the public domain. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <objc/runtime.h> | ||
|
||
|
||
@interface ListNode : NSObject | ||
@property (nonatomic, copy) NSString *name; | ||
@property (nonatomic, strong) ListNode *next; | ||
- (NSUInteger)length_v1; | ||
- (NSUInteger)length_v2; | ||
- (NSUInteger)length_v3; | ||
- (NSUInteger)length_v4; | ||
- (NSUInteger)length_v5; | ||
- (NSUInteger)length_v6; | ||
- (NSUInteger)length_v7; | ||
- (NSUInteger)length_v8; | ||
@end | ||
|
||
|
||
int main(int argc, const char * argv[]) | ||
{ | ||
@autoreleasepool { | ||
NSArray *names = @[@"Jack", @"Jill", @"Alice", @"Bob", @"Eve"]; | ||
ListNode *head = nil; | ||
for (NSUInteger i = 0; i < 10; i++) { | ||
ListNode *ln = [ListNode new]; | ||
ln.name = names[arc4random_uniform((uint32_t)names.count)]; | ||
ln.next = head; | ||
head = ln; | ||
} | ||
|
||
NSLog(@"length: %lu", head.length_v1); | ||
NSLog(@"length: %lu", head.length_v2); | ||
NSLog(@"length: %lu", head.length_v3); | ||
NSLog(@"length: %lu", head.length_v4); | ||
NSLog(@"length: %lu", head.length_v5); | ||
NSLog(@"length: %lu", head.length_v6); | ||
NSLog(@"length: %lu", head.length_v7); | ||
NSLog(@"length: %lu", head.length_v8); | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
|
||
@implementation ListNode | ||
- (NSUInteger)length_v1 { | ||
return 1 + [self.next length_v1]; | ||
} | ||
|
||
- (NSUInteger)length_v2 { | ||
return [[self class] lengthOfListWithHead_v2:self]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v2:(ListNode *)node { | ||
if (!node) | ||
return 0; // base case | ||
else | ||
return 1 + [self lengthOfListWithHead_v2:node.next]; | ||
} | ||
|
||
- (NSUInteger)length_v3 { | ||
return [[self class] lengthOfListWithHead_v3:self]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v3:(ListNode *)node { | ||
NSUInteger count = 0; | ||
while (node) { | ||
count += 1; | ||
node = node.next; | ||
} | ||
return count; | ||
} | ||
|
||
- (NSUInteger)length_v4 { | ||
return [[self class] lengthOfListWithHead_v4:self count:0]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v4:(ListNode *)node count:(NSUInteger)count { | ||
while (node) { | ||
count += 1; | ||
node = node.next; | ||
} | ||
return count; | ||
} | ||
|
||
- (NSUInteger)length_v5 { | ||
return [[self class] lengthOfListWithHead_v5:self count:0]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v5:(ListNode *)node count:(NSUInteger)count { | ||
top: | ||
if (!node) { | ||
return count; | ||
} else { | ||
count += 1; | ||
node = node.next; | ||
goto top; | ||
} | ||
} | ||
|
||
- (NSUInteger)length_v6 { | ||
return [[self class] lengthOfListWithHead_v6:self count:0]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v6:(ListNode *)node count:(NSUInteger)count { | ||
if (!node) | ||
return count; | ||
else { | ||
return [self lengthOfListWithHead_v6:node.next count:(count + 1)]; | ||
} | ||
} | ||
|
||
- (NSUInteger)length_v7 { | ||
return [[self class] lengthOfListWithHead_v7:self count:0]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v7:(ListNode *)node count:(NSUInteger)count { | ||
if (!node) | ||
return count; | ||
else | ||
return [self lengthOfListWithHead_v7:node->_next count:(count + 1)]; | ||
} | ||
|
||
- (NSUInteger)length_v8 { | ||
return [[self class] lengthOfListWithHead_v8:self count:0]; | ||
} | ||
+ (NSUInteger)lengthOfListWithHead_v8:(ListNode *)node count:(NSUInteger)count { | ||
if (!node) | ||
return count; | ||
else if (object_getClass(node) == self) | ||
return [self lengthOfListWithHead_v8:node->_next count:(count + 1)]; | ||
else | ||
return [self lengthOfListWithHead_v8:node.next count:(count + 1)]; | ||
} | ||
@end |