Skip to content

Commit

Permalink
Add ResultProtocol.fanout(_:) as an alternative to &&& operator
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Dec 27, 2016
1 parent 17dfc26 commit 88932f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
10 changes: 9 additions & 1 deletion Result/ResultProtocol.swift
Expand Up @@ -51,6 +51,14 @@ public extension ResultProtocol {
ifFailure: Result<U, Error>.failure)
}

/// Returns a Result with a tuple of the receiver and `other` values if both
/// are `Success`es, or re-wrapping the error of the earlier `Failure`.
public func fanout<R: ResultProtocol>(_ other: @autoclosure () -> R) -> Result<(Value, R.Value), Error>
where Error == R.Error
{
return self.flatMap { left in other().map { right in (left, right) } }
}

/// Returns a new Result by mapping `Failure`'s values using `transform`, or re-wrapping `Success`es’ values.
public func mapError<Error2>(_ transform: (Error) -> Error2) -> Result<Value, Error2> {
return flatMapError { .failure(transform($0)) }
Expand Down Expand Up @@ -119,7 +127,7 @@ infix operator &&& : LogicalConjunctionPrecedence
public func &&& <L: ResultProtocol, R: ResultProtocol> (left: L, right: @autoclosure () -> R) -> Result<(L.Value, R.Value), L.Error>
where L.Error == R.Error
{
return left.flatMap { left in right().map { right in (left, right) } }
return left.fanout(right)
}

precedencegroup ChainingPrecedence {
Expand Down
40 changes: 19 additions & 21 deletions Tests/ResultTests/ResultTests.swift
Expand Up @@ -17,6 +17,24 @@ final class ResultTests: XCTestCase {
XCTAssert(Result(nil, failWith: error) == failure)
}

func testFanout() {
let resultSuccess = success.fanout(success)
if let (x, y) = resultSuccess.value {
XCTAssertTrue(x == "success" && y == "success")
} else {
XCTFail()
}

let resultFailureBoth = failure.fanout(failure2)
XCTAssert(resultFailureBoth.error == error)

let resultFailureLeft = failure.fanout(success)
XCTAssert(resultFailureLeft.error == error)

let resultFailureRight = success.fanout(failure2)
XCTAssert(resultFailureRight.error == error2)
}

func testBimapTransformsSuccesses() {
XCTAssertEqual(success.bimap(
success: { $0.characters.count },
Expand Down Expand Up @@ -168,26 +186,6 @@ final class ResultTests: XCTestCase {
let result = Result<String, AnyError>.success("fail").tryMap(tryIsSuccess)
XCTAssert(result == failure)
}

// MARK: Operators

func testConjunctionOperator() {
let resultSuccess = success &&& success
if let (x, y) = resultSuccess.value {
XCTAssertTrue(x == "success" && y == "success")
} else {
XCTFail()
}

let resultFailureBoth = failure &&& failure2
XCTAssert(resultFailureBoth.error == error)

let resultFailureLeft = failure &&& success
XCTAssert(resultFailureLeft.error == error)

let resultFailureRight = success &&& failure2
XCTAssert(resultFailureRight.error == error2)
}
}

final class NoErrorTests: XCTestCase {
Expand Down Expand Up @@ -269,6 +267,7 @@ extension ResultTests {
("testMapRewrapsFailures", testMapRewrapsFailures),
("testInitOptionalSuccess", testInitOptionalSuccess),
("testInitOptionalFailure", testInitOptionalFailure),
("testFanout", testFanout),
("testErrorsIncludeTheSourceFile", testErrorsIncludeTheSourceFile),
("testErrorsIncludeTheSourceLine", testErrorsIncludeTheSourceLine),
("testErrorsIncludeTheCallingFunction", testErrorsIncludeTheCallingFunction),
Expand All @@ -289,7 +288,6 @@ extension ResultTests {
// ("testTryProducesSuccessesForOptionalAPI", testTryProducesSuccessesForOptionalAPI),
("testTryMapProducesSuccess", testTryMapProducesSuccess),
("testTryMapProducesFailure", testTryMapProducesFailure),
("testConjunctionOperator", testConjunctionOperator),
]
}
}
Expand Down

0 comments on commit 88932f7

Please sign in to comment.