Skip to content

Commit

Permalink
Merge pull request #48 from 47deg/js-37-TryMonad
Browse files Browse the repository at this point in the history
Js 37 try monad
  • Loading branch information
Javier de Silóniz Sandino committed Oct 13, 2015
2 parents 0981672 + d9e3ffb commit fbb0903
Show file tree
Hide file tree
Showing 5 changed files with 463 additions and 5 deletions.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,73 @@ matchTest.apply(1) // "One"
matchTest.apply(1000) // "Moar!"
```
#### UTILS
**Try**
`Try` is a monad that encapsulates an operation that can fail and throw an exception. As you may be aware, Swift now supports `do-try-catch` blocks to handle operations that can fail. `Try` can wrap throwable functions (those marked with the `throws` keyword) to handle these failures for you. The result of the wrapped operation is stored in a `TryMatcher.Success(x)` if it's a valid result `x`, or `TryMatcher.Failure(ex)` if the operation has thrown an `ex` exception.
```swift
import SecondBridge
// Throwable function
func convertStringToInt(s: String) throws -> Int {
if let parsedInt = Int(s) {
return parsedInt
} else {
throw ParseError.InvalidString
}
}
let tryParseCorrectString = Try<Int>(try self.convertStringToInt("47"))
tryParseCorrectString.isFailure() // false
tryParseCorrectString.isSuccess() // true
// You can get the result of the operations through pattern matching...
switch tryParseCorrectString.matchResult {
case .Success(let value): print("Result is \(value)")
case .Failure(let ex): print("There has been an exception!")
}
// ...or using convenience functions like getOrElse
let value = tryParseCorrectString.getOrElse(0) // 47
let tryParseIncorrectString = Try<Int>(try self.convertStringToInt("47 Degrees"))
tryParseCorrectString.isFailure() // true
tryParseCorrectString.isSuccess() // false
let invalidValue = tryParseIncorrectString.getOrElse(666) // 666
// You can apply several Higher-Order Functions to Try instances
// to apply functions to the encapsulated values:
let f = { (n: Int) -> Int in n + 10 }
let mapCorrectResult = tryParseCorrectString.map(f).getOrElse(666) // 57
let filterCorrectResult =
tryParseCorrectString.filter({ $0 != 47 }) // .Failure(exception)
func tryHalf(n: Int) -> Try<Int> {
// Returns a Try containing a function that divides any Int by two
// ...
}
let flatmapCorrectResultAgainstOKFunction = tryParseCorrectString.flatMap(tryHalf)
flatmapCorrectResultAgainstOKFunction.isSuccess() // true
flatmapCorrectResultAgainstOKFunction.getOrElse(1) // 23
// You can also use `recover` and `recoverWith` to chain a set of
// Partial Functions that can handle failures in your `Try`s:
let recoverResult = tryParseIncorrectString.recover({
(e: ErrorType) -> Bool in
return true
} |-> {(e: ErrorType) -> (Int) in return 0})
recoverResult.isSuccess() // true
let recoverResultGet = recoverResult.getOrElse(1) // 0
```
System Requirements
==================
Second Bridge supports iOS 8.0+.
Second Bridge supports iOS 8.0+ and Swift 2.0.
Contribute
=========
Expand Down
30 changes: 26 additions & 4 deletions SecondBridge/SecondBridge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
590992581B9DD61D008380B0 /* SecondBridge.xcworkspace in Resources */ = {isa = PBXBuildFile; fileRef = 590992571B9DD61D008380B0 /* SecondBridge.xcworkspace */; };
5927AF651BC5066A00161247 /* TryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5927AF641BC5066A00161247 /* TryTests.swift */; settings = {ASSET_TAGS = (); }; };
59283E531B0A03B500EC3A9F /* SecondBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = A183A1271AA85FA500C535A6 /* SecondBridge.h */; settings = {ATTRIBUTES = (Public, ); }; };
594E50371BC40B85005866FE /* ArraySlice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 594E50361BC40B85005866FE /* ArraySlice.swift */; settings = {ASSET_TAGS = (); }; };
594E50381BC40B85005866FE /* ArraySlice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 594E50361BC40B85005866FE /* ArraySlice.swift */; settings = {ASSET_TAGS = (); }; };
Expand All @@ -29,6 +29,8 @@
5982D68F1AE7B6DC00BA53D0 /* Vector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5982D68E1AE7B6DC00BA53D0 /* Vector.swift */; };
5982D6901AE7B6DC00BA53D0 /* Vector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5982D68E1AE7B6DC00BA53D0 /* Vector.swift */; };
599207CD1BC40D7800BB786D /* VectorIntegrityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 599207CC1BC40D7800BB786D /* VectorIntegrityTests.swift */; settings = {ASSET_TAGS = (); }; };
599207D01BC4174400BB786D /* Try.swift in Sources */ = {isa = PBXBuildFile; fileRef = 599207CF1BC4174400BB786D /* Try.swift */; settings = {ASSET_TAGS = (); }; };
599207D11BC4174400BB786D /* Try.swift in Sources */ = {isa = PBXBuildFile; fileRef = 599207CF1BC4174400BB786D /* Try.swift */; settings = {ASSET_TAGS = (); }; };
59A911B11AEA2F6F00EE50CA /* VectorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 59A911B01AEA2F6F00EE50CA /* VectorTests.swift */; };
59A911B21AEA2F6F00EE50CA /* SecondBridge.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A183A1221AA85FA500C535A6 /* SecondBridge.framework */; };
59A911B91AEA2F8000EE50CA /* Vector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5982D68E1AE7B6DC00BA53D0 /* Vector.swift */; };
Expand Down Expand Up @@ -93,7 +95,7 @@
/* Begin PBXFileReference section */
374373D53C1E1730C0517D5F /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
484FE31132DFCAEC6C987ABF /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
590992571B9DD61D008380B0 /* SecondBridge.xcworkspace */ = {isa = PBXFileReference; lastKnownFileType = wrapper.workspace; path = SecondBridge.xcworkspace; sourceTree = "<group>"; };
5927AF641BC5066A00161247 /* TryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TryTests.swift; sourceTree = "<group>"; };
594E50361BC40B85005866FE /* ArraySlice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArraySlice.swift; sourceTree = "<group>"; };
596D703C1AADB79B00CE9962 /* Map.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Map.swift; sourceTree = "<group>"; };
596D70401AADB80400CE9962 /* MapTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapTests.swift; sourceTree = "<group>"; };
Expand All @@ -108,6 +110,7 @@
5982D6811AE647A100BA53D0 /* ArrayTTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayTTests.swift; sourceTree = "<group>"; };
5982D68E1AE7B6DC00BA53D0 /* Vector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vector.swift; sourceTree = "<group>"; };
599207CC1BC40D7800BB786D /* VectorIntegrityTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VectorIntegrityTests.swift; sourceTree = "<group>"; };
599207CF1BC4174400BB786D /* Try.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Try.swift; sourceTree = "<group>"; };
59A911AC1AEA2F6F00EE50CA /* VectorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = VectorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
59A911AF1AEA2F6F00EE50CA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
59A911B01AEA2F6F00EE50CA /* VectorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VectorTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +174,14 @@
name = Frameworks;
sourceTree = "<group>";
};
5927AF631BC5065800161247 /* ErrorHandling */ = {
isa = PBXGroup;
children = (
5927AF641BC5066A00161247 /* TryTests.swift */,
);
path = ErrorHandling;
sourceTree = "<group>";
};
597143101AC9368800C1217C /* Protocols */ = {
isa = PBXGroup;
children = (
Expand All @@ -197,6 +208,14 @@
path = Protocols;
sourceTree = "<group>";
};
599207CE1BC4173800BB786D /* ErrorHandling */ = {
isa = PBXGroup;
children = (
599207CF1BC4174400BB786D /* Try.swift */,
);
path = ErrorHandling;
sourceTree = "<group>";
};
59A911AD1AEA2F6F00EE50CA /* VectorTests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -286,7 +305,6 @@
A183A1181AA85FA500C535A6 = {
isa = PBXGroup;
children = (
590992571B9DD61D008380B0 /* SecondBridge.xcworkspace */,
A183A1241AA85FA500C535A6 /* SecondBridge */,
A183A1311AA85FA500C535A6 /* SecondBridgeTests */,
59A911AD1AEA2F6F00EE50CA /* VectorTests */,
Expand All @@ -309,6 +327,7 @@
A183A1241AA85FA500C535A6 /* SecondBridge */ = {
isa = PBXGroup;
children = (
599207CE1BC4173800BB786D /* ErrorHandling */,
59BBB24A1AC466BD00D2F7C1 /* Extension */,
59C9BCA71AC2DD440043C96E /* Functions */,
59D811B21AA87B3B005F7761 /* Data */,
Expand All @@ -330,6 +349,7 @@
A183A1311AA85FA500C535A6 /* SecondBridgeTests */ = {
isa = PBXGroup;
children = (
5927AF631BC5065800161247 /* ErrorHandling */,
597143101AC9368800C1217C /* Protocols */,
A12A11591AC31B2200FE8A80 /* Extension */,
59C9BCAC1AC2DD500043C96E /* Functions */,
Expand Down Expand Up @@ -473,7 +493,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
590992581B9DD61D008380B0 /* SecondBridge.xcworkspace in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -555,13 +574,15 @@
59BBB24E1AC466BD00D2F7C1 /* Range.swift in Sources */,
5981E3C91AB6DFE600F052A0 /* Traversable.swift in Sources */,
59C9BCA91AC2DD440043C96E /* PartialFunction.swift in Sources */,
599207D01BC4174400BB786D /* Try.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
A183A1291AA85FA500C535A6 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
599207D11BC4174400BB786D /* Try.swift in Sources */,
59BDBCEA1ADFB2C400FFA363 /* ListT.swift in Sources */,
59BDBCEB1ADFB2C400FFA363 /* Iterable.swift in Sources */,
59BDBCEC1ADFB2C400FFA363 /* IterableTests.swift in Sources */,
Expand All @@ -583,6 +604,7 @@
594E50381BC40B85005866FE /* ArraySlice.swift in Sources */,
A12A115B1AC31B2200FE8A80 /* RangeTests.swift in Sources */,
597143131AC9369700C1217C /* TraversableTests.swift in Sources */,
5927AF651BC5066A00161247 /* TryTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit fbb0903

Please sign in to comment.