├── .gitignore ├── LICENSE ├── LlamaKit.podspec ├── LlamaKit.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ ├── LlamaKit-Mac.xcscheme │ └── LlamaKit-iOS.xcscheme ├── LlamaKit ├── Info.plist ├── LlamaKit.h ├── README.md └── Result.swift ├── LlamaKitTests ├── Info.plist └── ResultTests.swift └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 LlamaKit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LlamaKit.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "LlamaKit" 3 | s.version = "0.6.0" 4 | s.summary = "Collection of must-have functional Swift tools." 5 | s.description = "Collection of must-have functional tools. Trying to be as lightweight as possible, hopefully providing a simple foundation that more advanced systems can build on. LlamaKit is very Cocoa-focused. It is designed to work with common Cocoa paradigms, use names that are understandable to Cocoa devs, integrate with Cocoa tools like GCD, and in general strive for a low-to-modest learning curve for devs familiar with ObjC and Swift rather than Haskell and ML." 6 | s.homepage = "https://github.com/LlamaKit/LlamaKit" 7 | s.license = "MIT" 8 | s.author = { "Rob Napier" => "robnapier@gmail.com" } 9 | s.social_media_url = "https://twitter.com/cocoaphony" 10 | s.ios.deployment_target = "8.3" 11 | s.osx.deployment_target = "10.10" 12 | s.source = { :git => "https://github.com/LlamaKit/LlamaKit.git", :tag => "v#{s.version}" } 13 | s.source_files = "LlamaKit/*.swift" 14 | s.requires_arc = true 15 | end 16 | -------------------------------------------------------------------------------- /LlamaKit.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | D0297D2919D679B3009986A9 /* LlamaKit.h in Headers */ = {isa = PBXBuildFile; fileRef = FB2F8D8619BF637B00E4C49E /* LlamaKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | D0297D2A19D679B8009986A9 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F8D9A19BF661000E4C49E /* Result.swift */; }; 12 | D0297D2B19D679C0009986A9 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F8D9019BF637B00E4C49E /* ResultTests.swift */; }; 13 | D0297D2D19D67A56009986A9 /* LlamaKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0297D1019D67999009986A9 /* LlamaKit.framework */; }; 14 | D0297D2E19D67AAE009986A9 /* LlamaKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB2F8D8119BF637B00E4C49E /* LlamaKit.framework */; }; 15 | F529ED881A9C95C5003A9C65 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F529ED871A9C95C5003A9C65 /* Foundation.framework */; }; 16 | FB2F8D8719BF637B00E4C49E /* LlamaKit.h in Headers */ = {isa = PBXBuildFile; fileRef = FB2F8D8619BF637B00E4C49E /* LlamaKit.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17 | FB2F8D9119BF637B00E4C49E /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F8D9019BF637B00E4C49E /* ResultTests.swift */; }; 18 | FB2F8D9B19BF661000E4C49E /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F8D9A19BF661000E4C49E /* Result.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | D0297D1C19D67999009986A9 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = FB2F8D7819BF637B00E4C49E /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = D0297D0F19D67999009986A9; 27 | remoteInfo = LlamaKit; 28 | }; 29 | D0297D2F19D67ABF009986A9 /* PBXContainerItemProxy */ = { 30 | isa = PBXContainerItemProxy; 31 | containerPortal = FB2F8D7819BF637B00E4C49E /* Project object */; 32 | proxyType = 1; 33 | remoteGlobalIDString = FB2F8D8019BF637B00E4C49E; 34 | remoteInfo = "LlamaKit-iOS"; 35 | }; 36 | /* End PBXContainerItemProxy section */ 37 | 38 | /* Begin PBXFileReference section */ 39 | D0297D1019D67999009986A9 /* LlamaKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LlamaKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | D0297D1A19D67999009986A9 /* LlamaKit-MacTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "LlamaKit-MacTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 41 | F529ED871A9C95C5003A9C65 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 42 | FB2F8D8119BF637B00E4C49E /* LlamaKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = LlamaKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 43 | FB2F8D8519BF637B00E4C49E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 44 | FB2F8D8619BF637B00E4C49E /* LlamaKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LlamaKit.h; sourceTree = ""; }; 45 | FB2F8D8C19BF637B00E4C49E /* LlamaKit-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "LlamaKit-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 46 | FB2F8D8F19BF637B00E4C49E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 47 | FB2F8D9019BF637B00E4C49E /* ResultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResultTests.swift; sourceTree = ""; }; 48 | FB2F8D9A19BF661000E4C49E /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = ""; }; 49 | FB7C6F3D19D61C230087DA98 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 50 | /* End PBXFileReference section */ 51 | 52 | /* Begin PBXFrameworksBuildPhase section */ 53 | D0297D0C19D67999009986A9 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | D0297D1719D67999009986A9 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | D0297D2D19D67A56009986A9 /* LlamaKit.framework in Frameworks */, 65 | ); 66 | runOnlyForDeploymentPostprocessing = 0; 67 | }; 68 | FB2F8D7D19BF637B00E4C49E /* Frameworks */ = { 69 | isa = PBXFrameworksBuildPhase; 70 | buildActionMask = 2147483647; 71 | files = ( 72 | F529ED881A9C95C5003A9C65 /* Foundation.framework in Frameworks */, 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | FB2F8D8919BF637B00E4C49E /* Frameworks */ = { 77 | isa = PBXFrameworksBuildPhase; 78 | buildActionMask = 2147483647; 79 | files = ( 80 | D0297D2E19D67AAE009986A9 /* LlamaKit.framework in Frameworks */, 81 | ); 82 | runOnlyForDeploymentPostprocessing = 0; 83 | }; 84 | /* End PBXFrameworksBuildPhase section */ 85 | 86 | /* Begin PBXGroup section */ 87 | FB2F8D7719BF637B00E4C49E = { 88 | isa = PBXGroup; 89 | children = ( 90 | F529ED871A9C95C5003A9C65 /* Foundation.framework */, 91 | FB2F8D8319BF637B00E4C49E /* LlamaKit */, 92 | FB2F8D8D19BF637B00E4C49E /* LlamaKitTests */, 93 | FB2F8D8219BF637B00E4C49E /* Products */, 94 | ); 95 | indentWidth = 2; 96 | sourceTree = ""; 97 | tabWidth = 2; 98 | usesTabs = 0; 99 | }; 100 | FB2F8D8219BF637B00E4C49E /* Products */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | FB2F8D8119BF637B00E4C49E /* LlamaKit.framework */, 104 | FB2F8D8C19BF637B00E4C49E /* LlamaKit-iOSTests.xctest */, 105 | D0297D1019D67999009986A9 /* LlamaKit.framework */, 106 | D0297D1A19D67999009986A9 /* LlamaKit-MacTests.xctest */, 107 | ); 108 | name = Products; 109 | sourceTree = ""; 110 | }; 111 | FB2F8D8319BF637B00E4C49E /* LlamaKit */ = { 112 | isa = PBXGroup; 113 | children = ( 114 | FB2F8D8619BF637B00E4C49E /* LlamaKit.h */, 115 | FB2F8D9A19BF661000E4C49E /* Result.swift */, 116 | FB2F8D8419BF637B00E4C49E /* Supporting Files */, 117 | ); 118 | path = LlamaKit; 119 | sourceTree = ""; 120 | }; 121 | FB2F8D8419BF637B00E4C49E /* Supporting Files */ = { 122 | isa = PBXGroup; 123 | children = ( 124 | FB7C6F3D19D61C230087DA98 /* README.md */, 125 | FB2F8D8519BF637B00E4C49E /* Info.plist */, 126 | ); 127 | name = "Supporting Files"; 128 | sourceTree = ""; 129 | }; 130 | FB2F8D8D19BF637B00E4C49E /* LlamaKitTests */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | FB2F8D9019BF637B00E4C49E /* ResultTests.swift */, 134 | FB2F8D8E19BF637B00E4C49E /* Supporting Files */, 135 | ); 136 | path = LlamaKitTests; 137 | sourceTree = ""; 138 | }; 139 | FB2F8D8E19BF637B00E4C49E /* Supporting Files */ = { 140 | isa = PBXGroup; 141 | children = ( 142 | FB2F8D8F19BF637B00E4C49E /* Info.plist */, 143 | ); 144 | name = "Supporting Files"; 145 | sourceTree = ""; 146 | }; 147 | /* End PBXGroup section */ 148 | 149 | /* Begin PBXHeadersBuildPhase section */ 150 | D0297D0D19D67999009986A9 /* Headers */ = { 151 | isa = PBXHeadersBuildPhase; 152 | buildActionMask = 2147483647; 153 | files = ( 154 | D0297D2919D679B3009986A9 /* LlamaKit.h in Headers */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | FB2F8D7E19BF637B00E4C49E /* Headers */ = { 159 | isa = PBXHeadersBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | FB2F8D8719BF637B00E4C49E /* LlamaKit.h in Headers */, 163 | ); 164 | runOnlyForDeploymentPostprocessing = 0; 165 | }; 166 | /* End PBXHeadersBuildPhase section */ 167 | 168 | /* Begin PBXNativeTarget section */ 169 | D0297D0F19D67999009986A9 /* LlamaKit-Mac */ = { 170 | isa = PBXNativeTarget; 171 | buildConfigurationList = D0297D2719D67999009986A9 /* Build configuration list for PBXNativeTarget "LlamaKit-Mac" */; 172 | buildPhases = ( 173 | D0297D0B19D67999009986A9 /* Sources */, 174 | D0297D0C19D67999009986A9 /* Frameworks */, 175 | D0297D0D19D67999009986A9 /* Headers */, 176 | D0297D0E19D67999009986A9 /* Resources */, 177 | ); 178 | buildRules = ( 179 | ); 180 | dependencies = ( 181 | ); 182 | name = "LlamaKit-Mac"; 183 | productName = LlamaKit; 184 | productReference = D0297D1019D67999009986A9 /* LlamaKit.framework */; 185 | productType = "com.apple.product-type.framework"; 186 | }; 187 | D0297D1919D67999009986A9 /* LlamaKit-MacTests */ = { 188 | isa = PBXNativeTarget; 189 | buildConfigurationList = D0297D2819D67999009986A9 /* Build configuration list for PBXNativeTarget "LlamaKit-MacTests" */; 190 | buildPhases = ( 191 | D0297D1619D67999009986A9 /* Sources */, 192 | D0297D1719D67999009986A9 /* Frameworks */, 193 | D0297D1819D67999009986A9 /* Resources */, 194 | ); 195 | buildRules = ( 196 | ); 197 | dependencies = ( 198 | D0297D1D19D67999009986A9 /* PBXTargetDependency */, 199 | ); 200 | name = "LlamaKit-MacTests"; 201 | productName = LlamaKitTests; 202 | productReference = D0297D1A19D67999009986A9 /* LlamaKit-MacTests.xctest */; 203 | productType = "com.apple.product-type.bundle.unit-test"; 204 | }; 205 | FB2F8D8019BF637B00E4C49E /* LlamaKit-iOS */ = { 206 | isa = PBXNativeTarget; 207 | buildConfigurationList = FB2F8D9419BF637B00E4C49E /* Build configuration list for PBXNativeTarget "LlamaKit-iOS" */; 208 | buildPhases = ( 209 | FB2F8D7C19BF637B00E4C49E /* Sources */, 210 | FB2F8D7D19BF637B00E4C49E /* Frameworks */, 211 | FB2F8D7E19BF637B00E4C49E /* Headers */, 212 | FB2F8D7F19BF637B00E4C49E /* Resources */, 213 | ); 214 | buildRules = ( 215 | ); 216 | dependencies = ( 217 | ); 218 | name = "LlamaKit-iOS"; 219 | productName = LlamaKit; 220 | productReference = FB2F8D8119BF637B00E4C49E /* LlamaKit.framework */; 221 | productType = "com.apple.product-type.framework"; 222 | }; 223 | FB2F8D8B19BF637B00E4C49E /* LlamaKit-iOSTests */ = { 224 | isa = PBXNativeTarget; 225 | buildConfigurationList = FB2F8D9719BF637B00E4C49E /* Build configuration list for PBXNativeTarget "LlamaKit-iOSTests" */; 226 | buildPhases = ( 227 | FB2F8D8819BF637B00E4C49E /* Sources */, 228 | FB2F8D8919BF637B00E4C49E /* Frameworks */, 229 | FB2F8D8A19BF637B00E4C49E /* Resources */, 230 | ); 231 | buildRules = ( 232 | ); 233 | dependencies = ( 234 | D0297D3019D67ABF009986A9 /* PBXTargetDependency */, 235 | ); 236 | name = "LlamaKit-iOSTests"; 237 | productName = LlamaKitTests; 238 | productReference = FB2F8D8C19BF637B00E4C49E /* LlamaKit-iOSTests.xctest */; 239 | productType = "com.apple.product-type.bundle.unit-test"; 240 | }; 241 | /* End PBXNativeTarget section */ 242 | 243 | /* Begin PBXProject section */ 244 | FB2F8D7819BF637B00E4C49E /* Project object */ = { 245 | isa = PBXProject; 246 | attributes = { 247 | LastUpgradeCheck = 0600; 248 | ORGANIZATIONNAME = "Rob Napier"; 249 | TargetAttributes = { 250 | D0297D0F19D67999009986A9 = { 251 | CreatedOnToolsVersion = 6.1; 252 | }; 253 | D0297D1919D67999009986A9 = { 254 | CreatedOnToolsVersion = 6.1; 255 | }; 256 | FB2F8D8019BF637B00E4C49E = { 257 | CreatedOnToolsVersion = 6.0; 258 | }; 259 | FB2F8D8B19BF637B00E4C49E = { 260 | CreatedOnToolsVersion = 6.0; 261 | }; 262 | }; 263 | }; 264 | buildConfigurationList = FB2F8D7B19BF637B00E4C49E /* Build configuration list for PBXProject "LlamaKit" */; 265 | compatibilityVersion = "Xcode 3.2"; 266 | developmentRegion = English; 267 | hasScannedForEncodings = 0; 268 | knownRegions = ( 269 | en, 270 | ); 271 | mainGroup = FB2F8D7719BF637B00E4C49E; 272 | productRefGroup = FB2F8D8219BF637B00E4C49E /* Products */; 273 | projectDirPath = ""; 274 | projectRoot = ""; 275 | targets = ( 276 | FB2F8D8019BF637B00E4C49E /* LlamaKit-iOS */, 277 | FB2F8D8B19BF637B00E4C49E /* LlamaKit-iOSTests */, 278 | D0297D0F19D67999009986A9 /* LlamaKit-Mac */, 279 | D0297D1919D67999009986A9 /* LlamaKit-MacTests */, 280 | ); 281 | }; 282 | /* End PBXProject section */ 283 | 284 | /* Begin PBXResourcesBuildPhase section */ 285 | D0297D0E19D67999009986A9 /* Resources */ = { 286 | isa = PBXResourcesBuildPhase; 287 | buildActionMask = 2147483647; 288 | files = ( 289 | ); 290 | runOnlyForDeploymentPostprocessing = 0; 291 | }; 292 | D0297D1819D67999009986A9 /* Resources */ = { 293 | isa = PBXResourcesBuildPhase; 294 | buildActionMask = 2147483647; 295 | files = ( 296 | ); 297 | runOnlyForDeploymentPostprocessing = 0; 298 | }; 299 | FB2F8D7F19BF637B00E4C49E /* Resources */ = { 300 | isa = PBXResourcesBuildPhase; 301 | buildActionMask = 2147483647; 302 | files = ( 303 | ); 304 | runOnlyForDeploymentPostprocessing = 0; 305 | }; 306 | FB2F8D8A19BF637B00E4C49E /* Resources */ = { 307 | isa = PBXResourcesBuildPhase; 308 | buildActionMask = 2147483647; 309 | files = ( 310 | ); 311 | runOnlyForDeploymentPostprocessing = 0; 312 | }; 313 | /* End PBXResourcesBuildPhase section */ 314 | 315 | /* Begin PBXSourcesBuildPhase section */ 316 | D0297D0B19D67999009986A9 /* Sources */ = { 317 | isa = PBXSourcesBuildPhase; 318 | buildActionMask = 2147483647; 319 | files = ( 320 | D0297D2A19D679B8009986A9 /* Result.swift in Sources */, 321 | ); 322 | runOnlyForDeploymentPostprocessing = 0; 323 | }; 324 | D0297D1619D67999009986A9 /* Sources */ = { 325 | isa = PBXSourcesBuildPhase; 326 | buildActionMask = 2147483647; 327 | files = ( 328 | D0297D2B19D679C0009986A9 /* ResultTests.swift in Sources */, 329 | ); 330 | runOnlyForDeploymentPostprocessing = 0; 331 | }; 332 | FB2F8D7C19BF637B00E4C49E /* Sources */ = { 333 | isa = PBXSourcesBuildPhase; 334 | buildActionMask = 2147483647; 335 | files = ( 336 | FB2F8D9B19BF661000E4C49E /* Result.swift in Sources */, 337 | ); 338 | runOnlyForDeploymentPostprocessing = 0; 339 | }; 340 | FB2F8D8819BF637B00E4C49E /* Sources */ = { 341 | isa = PBXSourcesBuildPhase; 342 | buildActionMask = 2147483647; 343 | files = ( 344 | FB2F8D9119BF637B00E4C49E /* ResultTests.swift in Sources */, 345 | ); 346 | runOnlyForDeploymentPostprocessing = 0; 347 | }; 348 | /* End PBXSourcesBuildPhase section */ 349 | 350 | /* Begin PBXTargetDependency section */ 351 | D0297D1D19D67999009986A9 /* PBXTargetDependency */ = { 352 | isa = PBXTargetDependency; 353 | target = D0297D0F19D67999009986A9 /* LlamaKit-Mac */; 354 | targetProxy = D0297D1C19D67999009986A9 /* PBXContainerItemProxy */; 355 | }; 356 | D0297D3019D67ABF009986A9 /* PBXTargetDependency */ = { 357 | isa = PBXTargetDependency; 358 | target = FB2F8D8019BF637B00E4C49E /* LlamaKit-iOS */; 359 | targetProxy = D0297D2F19D67ABF009986A9 /* PBXContainerItemProxy */; 360 | }; 361 | /* End PBXTargetDependency section */ 362 | 363 | /* Begin XCBuildConfiguration section */ 364 | D0297D2319D67999009986A9 /* Debug */ = { 365 | isa = XCBuildConfiguration; 366 | buildSettings = { 367 | COMBINE_HIDPI_IMAGES = YES; 368 | DEFINES_MODULE = YES; 369 | DYLIB_COMPATIBILITY_VERSION = 1; 370 | DYLIB_CURRENT_VERSION = 1; 371 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 372 | FRAMEWORK_VERSION = A; 373 | GCC_PREPROCESSOR_DEFINITIONS = ( 374 | "DEBUG=1", 375 | "$(inherited)", 376 | ); 377 | INFOPLIST_FILE = LlamaKit/Info.plist; 378 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 379 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; 380 | PRODUCT_NAME = "$(PROJECT_NAME)"; 381 | SDKROOT = macosx; 382 | SKIP_INSTALL = YES; 383 | }; 384 | name = Debug; 385 | }; 386 | D0297D2419D67999009986A9 /* Release */ = { 387 | isa = XCBuildConfiguration; 388 | buildSettings = { 389 | COMBINE_HIDPI_IMAGES = YES; 390 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 391 | DEFINES_MODULE = YES; 392 | DYLIB_COMPATIBILITY_VERSION = 1; 393 | DYLIB_CURRENT_VERSION = 1; 394 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 395 | FRAMEWORK_VERSION = A; 396 | INFOPLIST_FILE = LlamaKit/Info.plist; 397 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 398 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; 399 | PRODUCT_NAME = "$(PROJECT_NAME)"; 400 | SDKROOT = macosx; 401 | SKIP_INSTALL = YES; 402 | }; 403 | name = Release; 404 | }; 405 | D0297D2519D67999009986A9 /* Debug */ = { 406 | isa = XCBuildConfiguration; 407 | buildSettings = { 408 | COMBINE_HIDPI_IMAGES = YES; 409 | FRAMEWORK_SEARCH_PATHS = ( 410 | "$(DEVELOPER_FRAMEWORKS_DIR)", 411 | "$(inherited)", 412 | ); 413 | GCC_PREPROCESSOR_DEFINITIONS = ( 414 | "DEBUG=1", 415 | "$(inherited)", 416 | ); 417 | INFOPLIST_FILE = LlamaKitTests/Info.plist; 418 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 419 | MACOSX_DEPLOYMENT_TARGET = 10.9; 420 | PRODUCT_NAME = "$(TARGET_NAME)"; 421 | SDKROOT = macosx; 422 | }; 423 | name = Debug; 424 | }; 425 | D0297D2619D67999009986A9 /* Release */ = { 426 | isa = XCBuildConfiguration; 427 | buildSettings = { 428 | COMBINE_HIDPI_IMAGES = YES; 429 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 430 | FRAMEWORK_SEARCH_PATHS = ( 431 | "$(DEVELOPER_FRAMEWORKS_DIR)", 432 | "$(inherited)", 433 | ); 434 | INFOPLIST_FILE = LlamaKitTests/Info.plist; 435 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; 436 | MACOSX_DEPLOYMENT_TARGET = 10.9; 437 | PRODUCT_NAME = "$(TARGET_NAME)"; 438 | SDKROOT = macosx; 439 | }; 440 | name = Release; 441 | }; 442 | FB2F8D9219BF637B00E4C49E /* Debug */ = { 443 | isa = XCBuildConfiguration; 444 | buildSettings = { 445 | ALWAYS_SEARCH_USER_PATHS = NO; 446 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 447 | CLANG_CXX_LIBRARY = "libc++"; 448 | CLANG_ENABLE_MODULES = YES; 449 | CLANG_ENABLE_OBJC_ARC = YES; 450 | CLANG_WARN_BOOL_CONVERSION = YES; 451 | CLANG_WARN_CONSTANT_CONVERSION = YES; 452 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 453 | CLANG_WARN_EMPTY_BODY = YES; 454 | CLANG_WARN_ENUM_CONVERSION = YES; 455 | CLANG_WARN_INT_CONVERSION = YES; 456 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 457 | CLANG_WARN_UNREACHABLE_CODE = YES; 458 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 459 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 460 | COPY_PHASE_STRIP = NO; 461 | CURRENT_PROJECT_VERSION = 1; 462 | ENABLE_STRICT_OBJC_MSGSEND = YES; 463 | GCC_C_LANGUAGE_STANDARD = gnu99; 464 | GCC_DYNAMIC_NO_PIC = NO; 465 | GCC_OPTIMIZATION_LEVEL = 0; 466 | GCC_PREPROCESSOR_DEFINITIONS = ( 467 | "DEBUG=1", 468 | "$(inherited)", 469 | ); 470 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 471 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 472 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 473 | GCC_WARN_UNDECLARED_SELECTOR = YES; 474 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 475 | GCC_WARN_UNUSED_FUNCTION = YES; 476 | GCC_WARN_UNUSED_VARIABLE = YES; 477 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 478 | MACOSX_DEPLOYMENT_TARGET = 10.9; 479 | MTL_ENABLE_DEBUG_INFO = YES; 480 | ONLY_ACTIVE_ARCH = YES; 481 | SDKROOT = iphoneos; 482 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 483 | TARGETED_DEVICE_FAMILY = "1,2"; 484 | VERSIONING_SYSTEM = "apple-generic"; 485 | VERSION_INFO_PREFIX = ""; 486 | }; 487 | name = Debug; 488 | }; 489 | FB2F8D9319BF637B00E4C49E /* Release */ = { 490 | isa = XCBuildConfiguration; 491 | buildSettings = { 492 | ALWAYS_SEARCH_USER_PATHS = NO; 493 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 494 | CLANG_CXX_LIBRARY = "libc++"; 495 | CLANG_ENABLE_MODULES = YES; 496 | CLANG_ENABLE_OBJC_ARC = YES; 497 | CLANG_WARN_BOOL_CONVERSION = YES; 498 | CLANG_WARN_CONSTANT_CONVERSION = YES; 499 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 500 | CLANG_WARN_EMPTY_BODY = YES; 501 | CLANG_WARN_ENUM_CONVERSION = YES; 502 | CLANG_WARN_INT_CONVERSION = YES; 503 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 504 | CLANG_WARN_UNREACHABLE_CODE = YES; 505 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 506 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 507 | COPY_PHASE_STRIP = YES; 508 | CURRENT_PROJECT_VERSION = 1; 509 | ENABLE_NS_ASSERTIONS = NO; 510 | ENABLE_STRICT_OBJC_MSGSEND = YES; 511 | GCC_C_LANGUAGE_STANDARD = gnu99; 512 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 513 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 514 | GCC_WARN_UNDECLARED_SELECTOR = YES; 515 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 516 | GCC_WARN_UNUSED_FUNCTION = YES; 517 | GCC_WARN_UNUSED_VARIABLE = YES; 518 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 519 | MACOSX_DEPLOYMENT_TARGET = 10.9; 520 | MTL_ENABLE_DEBUG_INFO = NO; 521 | SDKROOT = iphoneos; 522 | TARGETED_DEVICE_FAMILY = "1,2"; 523 | VALIDATE_PRODUCT = YES; 524 | VERSIONING_SYSTEM = "apple-generic"; 525 | VERSION_INFO_PREFIX = ""; 526 | }; 527 | name = Release; 528 | }; 529 | FB2F8D9519BF637B00E4C49E /* Debug */ = { 530 | isa = XCBuildConfiguration; 531 | buildSettings = { 532 | CLANG_ENABLE_MODULES = YES; 533 | CODE_SIGN_IDENTITY = "iPhone Developer"; 534 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 535 | DEFINES_MODULE = YES; 536 | DYLIB_COMPATIBILITY_VERSION = 1; 537 | DYLIB_CURRENT_VERSION = 1; 538 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 539 | INFOPLIST_FILE = LlamaKit/Info.plist; 540 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 541 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 542 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 543 | PRODUCT_NAME = "$(PROJECT_NAME)"; 544 | PROVISIONING_PROFILE = ""; 545 | SDKROOT = iphoneos; 546 | SKIP_INSTALL = YES; 547 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 548 | }; 549 | name = Debug; 550 | }; 551 | FB2F8D9619BF637B00E4C49E /* Release */ = { 552 | isa = XCBuildConfiguration; 553 | buildSettings = { 554 | CLANG_ENABLE_MODULES = YES; 555 | CODE_SIGN_IDENTITY = "iPhone Developer"; 556 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 557 | DEFINES_MODULE = YES; 558 | DYLIB_COMPATIBILITY_VERSION = 1; 559 | DYLIB_CURRENT_VERSION = 1; 560 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 561 | INFOPLIST_FILE = LlamaKit/Info.plist; 562 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 563 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 564 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 565 | PRODUCT_NAME = "$(PROJECT_NAME)"; 566 | PROVISIONING_PROFILE = ""; 567 | SDKROOT = iphoneos; 568 | SKIP_INSTALL = YES; 569 | }; 570 | name = Release; 571 | }; 572 | FB2F8D9819BF637B00E4C49E /* Debug */ = { 573 | isa = XCBuildConfiguration; 574 | buildSettings = { 575 | FRAMEWORK_SEARCH_PATHS = ( 576 | "$(SDKROOT)/Developer/Library/Frameworks", 577 | "$(inherited)", 578 | ); 579 | GCC_PREPROCESSOR_DEFINITIONS = ( 580 | "DEBUG=1", 581 | "$(inherited)", 582 | ); 583 | INFOPLIST_FILE = LlamaKitTests/Info.plist; 584 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 585 | PRODUCT_NAME = "$(TARGET_NAME)"; 586 | }; 587 | name = Debug; 588 | }; 589 | FB2F8D9919BF637B00E4C49E /* Release */ = { 590 | isa = XCBuildConfiguration; 591 | buildSettings = { 592 | FRAMEWORK_SEARCH_PATHS = ( 593 | "$(SDKROOT)/Developer/Library/Frameworks", 594 | "$(inherited)", 595 | ); 596 | INFOPLIST_FILE = LlamaKitTests/Info.plist; 597 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 598 | PRODUCT_NAME = "$(TARGET_NAME)"; 599 | }; 600 | name = Release; 601 | }; 602 | /* End XCBuildConfiguration section */ 603 | 604 | /* Begin XCConfigurationList section */ 605 | D0297D2719D67999009986A9 /* Build configuration list for PBXNativeTarget "LlamaKit-Mac" */ = { 606 | isa = XCConfigurationList; 607 | buildConfigurations = ( 608 | D0297D2319D67999009986A9 /* Debug */, 609 | D0297D2419D67999009986A9 /* Release */, 610 | ); 611 | defaultConfigurationIsVisible = 0; 612 | defaultConfigurationName = Release; 613 | }; 614 | D0297D2819D67999009986A9 /* Build configuration list for PBXNativeTarget "LlamaKit-MacTests" */ = { 615 | isa = XCConfigurationList; 616 | buildConfigurations = ( 617 | D0297D2519D67999009986A9 /* Debug */, 618 | D0297D2619D67999009986A9 /* Release */, 619 | ); 620 | defaultConfigurationIsVisible = 0; 621 | defaultConfigurationName = Release; 622 | }; 623 | FB2F8D7B19BF637B00E4C49E /* Build configuration list for PBXProject "LlamaKit" */ = { 624 | isa = XCConfigurationList; 625 | buildConfigurations = ( 626 | FB2F8D9219BF637B00E4C49E /* Debug */, 627 | FB2F8D9319BF637B00E4C49E /* Release */, 628 | ); 629 | defaultConfigurationIsVisible = 0; 630 | defaultConfigurationName = Release; 631 | }; 632 | FB2F8D9419BF637B00E4C49E /* Build configuration list for PBXNativeTarget "LlamaKit-iOS" */ = { 633 | isa = XCConfigurationList; 634 | buildConfigurations = ( 635 | FB2F8D9519BF637B00E4C49E /* Debug */, 636 | FB2F8D9619BF637B00E4C49E /* Release */, 637 | ); 638 | defaultConfigurationIsVisible = 0; 639 | defaultConfigurationName = Release; 640 | }; 641 | FB2F8D9719BF637B00E4C49E /* Build configuration list for PBXNativeTarget "LlamaKit-iOSTests" */ = { 642 | isa = XCConfigurationList; 643 | buildConfigurations = ( 644 | FB2F8D9819BF637B00E4C49E /* Debug */, 645 | FB2F8D9919BF637B00E4C49E /* Release */, 646 | ); 647 | defaultConfigurationIsVisible = 0; 648 | defaultConfigurationName = Release; 649 | }; 650 | /* End XCConfigurationList section */ 651 | }; 652 | rootObject = FB2F8D7819BF637B00E4C49E /* Project object */; 653 | } 654 | -------------------------------------------------------------------------------- /LlamaKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LlamaKit.xcodeproj/xcshareddata/xcschemes/LlamaKit-Mac.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /LlamaKit.xcodeproj/xcshareddata/xcschemes/LlamaKit-iOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /LlamaKit/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | net.robnapier.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSHumanReadableCopyright 24 | Copyright © 2014 Rob Napier. All rights reserved. 25 | NSPrincipalClass 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LlamaKit/LlamaKit.h: -------------------------------------------------------------------------------- 1 | // 2 | // LlamaKit.h 3 | // LlamaKit 4 | // 5 | // Created by Rob Napier on 9/9/14. 6 | // Copyright (c) 2014 Rob Napier. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for LlamaKit. 12 | FOUNDATION_EXPORT double LlamaKitVersionNumber; 13 | 14 | //! Project version string for LlamaKit. 15 | FOUNDATION_EXPORT const unsigned char LlamaKitVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /LlamaKit/README.md: -------------------------------------------------------------------------------- 1 | LlamaKit 2 | ======== 3 | 4 | Collection of must-have functional tools. Trying to be as lightweight as possible, hopefully providing a simple foundation that 5 | more advanced systems can build on. LlamaKit is very Cocoa-focused. It is designed to work with common Cocoa paradigms, use names 6 | that are understandable to Cocoa devs, integrate with Cocoa tools like GCD, and in general strive for a low-to-modest learning 7 | curve for devs familiar with ObjC and Swift rather than Haskell and ML. There are more functionally beautiful toolkits out there 8 | (see [Swiftz](https://github.com/maxpow4h/swiftz) and [Swift-Extras](https://github.com/CodaFi/Swift-Extras) for some nice 9 | examples). LlamaKit intentionally is much less full-featured, and is focused only on things that come up commonly in Cocoa 10 | development. (Within those restrictions, it hopes to be based as much as possible on the lessons of other FP languages, and I 11 | welcome input from folks with deeper FP experience.) 12 | 13 | Currently has a `Result` object, which is the most critical. (And in the end, it may be the *only* thing in the main module.) 14 | 15 | LlamaKit should be considered highly experimental, pre-alpha, in development, I promise I will break you. 16 | 17 | But the `Result` object is kind of nice already if you want to go ahead and use it. :D 18 | 19 | (Note that I've moved the async objects, Future, Promise, Task, out of this repo. I'm working on them further, but they'll 20 | go into some other repo like LlamaKit-async. This repo is meant to be very, very core stuff that almost everyone will want. 21 | Async is too big for that.) 22 | -------------------------------------------------------------------------------- /LlamaKit/Result.swift: -------------------------------------------------------------------------------- 1 | /// Result 2 | /// 3 | /// Container for a successful value (T) or a failure with an NSError 4 | /// 5 | 6 | import Foundation 7 | 8 | /// A success `Result` returning `value` 9 | /// This form is preferred to `Result.Success(Box(value))` because it 10 | // does not require dealing with `Box()` 11 | public func success(value: T) -> Result { 12 | return .Success(Box(value)) 13 | } 14 | 15 | /// A failure `Result` returning `error` 16 | /// The default error is an empty one so that `failure()` is legal 17 | /// To assign this to a variable, you must explicitly give a type. 18 | /// Otherwise the compiler has no idea what `T` is. This form is preferred 19 | /// to Result.Failure(error) because it provides a useful default. 20 | /// For example: 21 | /// let fail: Result = failure() 22 | /// 23 | 24 | /// Dictionary keys for default errors 25 | public let ErrorFileKey = "LMErrorFile" 26 | public let ErrorLineKey = "LMErrorLine" 27 | 28 | private func defaultError(userInfo: [NSObject: AnyObject]) -> NSError { 29 | return NSError(domain: "", code: 0, userInfo: userInfo) 30 | } 31 | 32 | private func defaultError(message: String, file: String = __FILE__, line: Int = __LINE__) -> NSError { 33 | return defaultError([NSLocalizedDescriptionKey: message, ErrorFileKey: file, ErrorLineKey: line]) 34 | } 35 | 36 | private func defaultError(file: String = __FILE__, line: Int = __LINE__) -> NSError { 37 | return defaultError([ErrorFileKey: file, ErrorLineKey: line]) 38 | } 39 | 40 | public func failure(message: String, file: String = __FILE__, line: Int = __LINE__) -> Result { 41 | let userInfo: [NSObject : AnyObject] = [NSLocalizedDescriptionKey: message, ErrorFileKey: file, ErrorLineKey: line] 42 | return failure(defaultError(userInfo)) 43 | } 44 | 45 | public func failure(file: String = __FILE__, line: Int = __LINE__) -> Result { 46 | let userInfo: [NSObject : AnyObject] = [ErrorFileKey: file, ErrorLineKey: line] 47 | return failure(defaultError(userInfo)) 48 | } 49 | 50 | public func failure(error: E) -> Result { 51 | return .Failure(Box(error)) 52 | } 53 | 54 | /// Construct a `Result` using a block which receives an error parameter. 55 | /// Expected to return non-nil for success. 56 | 57 | public func try(f: NSErrorPointer -> T?, file: String = __FILE__, line: Int = __LINE__) -> Result { 58 | var error: NSError? 59 | return f(&error).map(success) ?? failure(error ?? defaultError(file: file, line: line)) 60 | } 61 | 62 | public func try(f: NSErrorPointer -> Bool, file: String = __FILE__, line: Int = __LINE__) -> Result<(),NSError> { 63 | var error: NSError? 64 | return f(&error) ? success(()) : failure(error ?? defaultError(file: file, line: line)) 65 | } 66 | 67 | /// Container for a successful value (T) or a failure with an E 68 | public enum Result { 69 | case Success(Box) 70 | case Failure(Box) 71 | 72 | /// The successful value as an Optional 73 | public var value: T? { 74 | switch self { 75 | case .Success(let box): return box.unbox 76 | case .Failure: return nil 77 | } 78 | } 79 | 80 | /// The failing error as an Optional 81 | public var error: E? { 82 | switch self { 83 | case .Success: return nil 84 | case .Failure(let err): return err.unbox 85 | } 86 | } 87 | 88 | public var isSuccess: Bool { 89 | switch self { 90 | case .Success: return true 91 | case .Failure: return false 92 | } 93 | } 94 | 95 | /// Return a new result after applying a transformation to a successful value. 96 | /// Mapping a failure returns a new failure without evaluating the transform 97 | public func map(transform: T -> U) -> Result { 98 | switch self { 99 | case Success(let box): 100 | return .Success(Box(transform(box.unbox))) 101 | case Failure(let err): 102 | return .Failure(err) 103 | } 104 | } 105 | 106 | /// Return a new result after applying a transformation (that itself 107 | /// returns a result) to a successful value. 108 | /// Calling with a failure returns a new failure without evaluating the transform 109 | public func flatMap(transform:T -> Result) -> Result { 110 | switch self { 111 | case Success(let value): return transform(value.unbox) 112 | case Failure(let error): return .Failure(error) 113 | } 114 | } 115 | } 116 | 117 | extension Result: Printable { 118 | public var description: String { 119 | switch self { 120 | case .Success(let box): 121 | return "Success: \(box.unbox)" 122 | case .Failure(let error): 123 | return "Failure: \(error.unbox)" 124 | } 125 | } 126 | } 127 | 128 | /// Failure coalescing 129 | /// .Success(Box(42)) ?? 0 ==> 42 130 | /// .Failure(NSError()) ?? 0 ==> 0 131 | public func ??(result: Result, @autoclosure defaultValue: () -> T) -> T { 132 | switch result { 133 | case .Success(let value): 134 | return value.unbox 135 | case .Failure(let error): 136 | return defaultValue() 137 | } 138 | } 139 | 140 | /// Equatable 141 | /// Equality for Result is defined by the equality of the contained types 142 | public func ==(lhs: Result, rhs: Result) -> Bool { 143 | switch (lhs, rhs) { 144 | case let (.Success(l), .Success(r)): return l.unbox == r.unbox 145 | case let (.Failure(l), .Failure(r)): return l.unbox == r.unbox 146 | default: return false 147 | } 148 | } 149 | 150 | public func !=(lhs: Result, rhs: Result) -> Bool { 151 | return !(lhs == rhs) 152 | } 153 | 154 | /// Due to current swift limitations, we have to include this Box in Result. 155 | /// Swift cannot handle an enum with multiple associated data (A, NSError) where one is of unknown size (A) 156 | final public class Box { 157 | public let unbox: T 158 | public init(_ value: T) { self.unbox = value } 159 | } 160 | -------------------------------------------------------------------------------- /LlamaKitTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | net.robnapier.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /LlamaKitTests/ResultTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // LlamaKitTests.swift 3 | // LlamaKitTests 4 | // 5 | // Created by Rob Napier on 9/9/14. 6 | // Copyright (c) 2014 Rob Napier. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import LlamaKit 11 | import XCTest 12 | 13 | class ResultTests: XCTestCase { 14 | let err = NSError(domain: "", code: 11, userInfo: nil) 15 | let err2 = NSError(domain: "", code: 12, userInfo: nil) 16 | 17 | func testSuccessIsSuccess() { 18 | let s: Result = success(42) 19 | XCTAssertTrue(s.isSuccess) 20 | } 21 | 22 | func testFailureIsNotSuccess() { 23 | let f: Result = failure() 24 | XCTAssertFalse(f.isSuccess) 25 | } 26 | 27 | func testSuccessReturnsValue() { 28 | let s: Result = success(42) 29 | XCTAssertEqual(s.value!, 42) 30 | } 31 | 32 | func testSuccessReturnsNoError() { 33 | let s: Result = success(42) 34 | XCTAssertNil(s.error) 35 | } 36 | 37 | func testFailureReturnsError() { 38 | let f: Result = failure(self.err) 39 | XCTAssertEqual(f.error!, self.err) 40 | } 41 | 42 | func testFailureReturnsNoValue() { 43 | let f: Result = failure(self.err) 44 | XCTAssertNil(f.value) 45 | } 46 | 47 | func testMapSuccessUnaryOperator() { 48 | let x: Result = success(42) 49 | let y = x.map(-) 50 | XCTAssertEqual(y.value!, -42) 51 | } 52 | 53 | func testMapFailureUnaryOperator() { 54 | let x: Result = failure(self.err) 55 | let y = x.map(-) 56 | XCTAssertNil(y.value) 57 | XCTAssertEqual(y.error!, self.err) 58 | } 59 | 60 | func testMapSuccessNewType() { 61 | let x: Result = success("abcd") 62 | let y = x.map { count($0) } 63 | XCTAssertEqual(y.value!, 4) 64 | } 65 | 66 | func testMapFailureNewType() { 67 | let x: Result = failure(self.err) 68 | let y = x.map { count($0) } 69 | XCTAssertEqual(y.error!, self.err) 70 | } 71 | 72 | func doubleSuccess(x: Int) -> Result { 73 | return success(x * 2) 74 | } 75 | 76 | func doubleFailure(x: Int) -> Result { 77 | return failure(self.err) 78 | } 79 | 80 | func testFlatMapSuccessSuccess() { 81 | let x: Result = success(42) 82 | let y = x.flatMap(doubleSuccess) 83 | XCTAssertEqual(y.value!, 84) 84 | } 85 | 86 | func testFlatMapSuccessFailure() { 87 | let x: Result = success(42) 88 | let y = x.flatMap(doubleFailure) 89 | XCTAssertEqual(y.error!, self.err) 90 | } 91 | 92 | func testFlatMapFailureSuccess() { 93 | let x: Result = failure(self.err2) 94 | let y = x.flatMap(doubleSuccess) 95 | XCTAssertEqual(y.error!, self.err2) 96 | } 97 | 98 | func testFlatMapFailureFailure() { 99 | let x: Result = failure(self.err2) 100 | let y = x.flatMap(doubleFailure) 101 | XCTAssertEqual(y.error!, self.err2) 102 | } 103 | 104 | func testDescriptionSuccess() { 105 | let x: Result = success(42) 106 | XCTAssertEqual(x.description, "Success: 42") 107 | } 108 | 109 | func testDescriptionFailure() { 110 | let x: Result = failure() 111 | XCTAssert(x.description.hasPrefix("Failure: Error Domain= Code=0 ")) 112 | } 113 | 114 | func testCoalesceSuccess() { 115 | let r: Result = success(42) 116 | let x = r ?? 43 117 | XCTAssertEqual(x, 42) 118 | } 119 | 120 | func testCoalesceFailure() { 121 | let x = failure() ?? 43 122 | XCTAssertEqual(x, 43) 123 | } 124 | 125 | private func makeTryFunction(x: T, _ succeed: Bool = true)(error: NSErrorPointer) -> T { 126 | if !succeed { 127 | error.memory = NSError(domain: "domain", code: 1, userInfo: [:]) 128 | } 129 | return x 130 | } 131 | 132 | func testTryTSuccess() { 133 | XCTAssertEqual(try(makeTryFunction(42 as Int?)) ?? 43, 42) 134 | } 135 | 136 | func testTryTFailure() { 137 | let result = try(makeTryFunction(nil as String?, false)) 138 | XCTAssertEqual(result ?? "abc", "abc") 139 | XCTAssert(result.description.hasPrefix("Failure: Error Domain=domain Code=1 ")) 140 | } 141 | 142 | func testTryBoolSuccess() { 143 | XCTAssert(try(makeTryFunction(true)).isSuccess) 144 | } 145 | 146 | func testTryBoolFailure() { 147 | let result = try(makeTryFunction(false, false)) 148 | XCTAssertFalse(result.isSuccess) 149 | XCTAssert(result.description.hasPrefix("Failure: Error Domain=domain Code=1 ")) 150 | } 151 | 152 | func testSuccessEquality() { 153 | let result: Result = success("result") 154 | let otherResult: Result = success("result") 155 | 156 | XCTAssert(result == otherResult) 157 | } 158 | 159 | func testFailureEquality() { 160 | let result: Result = failure(err) 161 | let otherResult: Result = failure(err) 162 | 163 | XCTAssert(result == otherResult) 164 | } 165 | 166 | func testSuccessInequality() { 167 | let result: Result = success("result") 168 | let otherResult: Result = success("different result") 169 | 170 | XCTAssert(result != otherResult) 171 | } 172 | 173 | func testFailureInequality() { 174 | let result: Result = failure(err) 175 | let otherResult: Result = failure(err2) 176 | 177 | XCTAssert(result != otherResult) 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | NOTE: This project has been merged with and superceded by Rob Rix's [Result](https://github.com/antitypical/Result) µframework. 2 | 3 | LlamaKit 4 | ======== 5 | 6 | Collection of must-have functional tools. Trying to be as lightweight as possible, hopefully providing a simple foundation that 7 | more advanced systems can build on. LlamaKit is very Cocoa-focused. It is designed to work with common Cocoa paradigms, use names 8 | that are understandable to Cocoa devs, integrate with Cocoa tools like GCD, and in general strive for a low-to-modest learning 9 | curve for devs familiar with ObjC and Swift rather than Haskell and ML. There are more functionally beautiful toolkits out there 10 | (see [Swiftz](https://github.com/maxpow4h/swiftz) and [Swift-Extras](https://github.com/CodaFi/Swift-Extras) for some nice 11 | examples). LlamaKit intentionally is much less full-featured, and is focused only on things that come up commonly in Cocoa 12 | development. (Within those restrictions, it hopes to be based as much as possible on the lessons of other FP languages, and I 13 | welcome input from folks with deeper FP experience.) 14 | 15 | Currently has a `Result` object, which is the most critical. (And in the end, it may be the *only* thing in the main module.) 16 | `Result` is mostly done except for documentation (in progress). Tests are built. 17 | 18 | `Future` is in progress. It's heavily inspired by [Scala's approach](http://docs.scala-lang.org/overviews/core/futures.html), 19 | though there are some small differences. I haven't decided if a `Promise` ISA `Future` or HASA `Future`. The Scala approach 20 | is a weird hybrid. It technically HASA `Future`, but in the main implementation, the `Promise` is its own `Future`, so it's 21 | kind of ISA, too. Still a work in progress there. I'm considering pulling `Future` out; it already makes this module too 22 | complicated (did I mention that LlamaKit wants to be really, really simple?) 23 | 24 | LlamaKit should be considered highly experimental, pre-alpha, in development, I promise I will break you. 25 | 26 | But the `Result` object is kind of nice already if you want to go ahead and use it. :D 27 | 28 | Current Thinkings on Structure 29 | ============================== 30 | 31 | (This is highly in progress and subject to change, like everything. Comments welcome.) 32 | 33 | I want LlamaKit to provide several important tools to simplify functional composition. But LlamaKit doesn't intend to be a programming approach (vs. [ReactiveCocoa](https://github.com/ReactiveCocoa) or [TypeLift](https://github.com/typelift) which provide powerful ways to think about problems). LlamaKit is just a bag of tools. If you want to borrow my hammer, you don't have to take my circular saw, too. So LlamaKit is split up into several small frameworks so you can pick-and-choose. 34 | 35 | 36 | 37 | 38 | 39 |
LlamaKit (umbrella)
LlamaFutureLlama... (?)LlamaOps
LlamaCore
40 | 41 | LlamaCore 42 | : The absolute basics. If it's in LlamaCore, I believe that the majority of Swift developers should be using it. I think other libraries should bulid on top of it. I think Apple should put it into stdlib. This is the stuff that I worry *many* developers will reinvent, and that will cause collisions between code bases. LlamaCore strives to be incredibly non-impacting. It avoids creating top-level functions that might conflict with consuming code. It declares no new opeartors. The bar is very high to be in LlamaCore. It currently contains just two types: `Result` (which I think Apple should put into stdlib) and `Box` (which only exists because of Swift compiler limitations). In the ideal LlamaKit, LlamaCore would be empty. 43 | 44 | LlamaFuture 45 | : Concurrency primitives, most notably `Future`. In my current design, `Future` is actually stand-alone and doesn't require LlamaCore, but I think that most developers will want a failable `Future>` (which I am tentatively calling `Task`). I also expect this to hold `Promise`, which is a future that the caller manually completes. (This is still under very heavy consideration; I'm not sure exactly what I want yet.) LlamaFuture is tightly coupled to GCD, and is intended as a nicer interface to common Cocoa concurrency primitives, not a replacement for them. 46 | 47 | LlamaOps 48 | : Functional composition with operators like `>>=` and `|>` is a beautiful thing. But it carries with it a lot of overhead. Not only are there cognative loads (the code is not obvious at all to the uninitiated), there are non-trivial compiler and language impacts. Operators are declared globally (specifically precedence and associativity). The Swift compiler has some serious performance problems building code with complex operator usage. And in the case of operator conflict, the resulting errors are very confusing. Widely used libraries should strongly avoid bringing in new operators implicitly. My intention is that you would always have to import LlamaOps explicitly, even if you import the umbrella LlamaKit. 49 | 50 | LlamaKit 51 | : I do expect most of the things in LlamaKit to be useful to many, if not most, Cocoa devs. I don't want to force you to take everything, but I do want to make it easy to take everything (except operators). So hopefully I can provide an umbrella framework. I don't know if that actually works in Xcode, but we'll see. 52 | 53 | Llama... 54 | : At this point I'm not expecting a ton more stuff, but this is where it would go. While I'm evangelizing functional programming, I want most people to use Swift to achieve that, not lots of layers on top of Swift. So for instance, I'm not particularly sold on an `Either` right now. In most cases I'd rather you use an enum directly. And I don't want to create a full functor-applicative-monad hierarchy (TypeLift is covering that for us). I probabaly do want somewhere to put `sequence()`, `lift()`, `pure()`, and `flip()` and maybe that could become LlamaLambda (LlamaLamb? LlamaFunc?) But I want to go slow there and see what needs arrise in real projects. 55 | --------------------------------------------------------------------------------