Skip to content

Commit

Permalink
Merge pull request #29 from antitypical/hash-tag-no-dependencies
Browse files Browse the repository at this point in the history
#nodependencies
  • Loading branch information
robrix committed Apr 20, 2015
2 parents 8066811 + 1ff460a commit 1756b77
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 88 deletions.
9 changes: 0 additions & 9 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion Cartfile

This file was deleted.

3 changes: 0 additions & 3 deletions Cartfile.resolved

This file was deleted.

1 change: 0 additions & 1 deletion Carthage/Checkouts/Box
Submodule Box deleted from 18d05a
1 change: 0 additions & 1 deletion Carthage/Checkouts/Either
Submodule Either deleted from 026d34
1 change: 0 additions & 1 deletion Carthage/Checkouts/Prelude
Submodule Prelude deleted from 180e9a
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ API documentation is in the source.
## Integration

1. Add this repository as a submodule and check out its dependencies, and/or [add it to your Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile) if you’re using [carthage](https://github.com/Carthage/Carthage/) to manage your dependencies.
2. Drag `Result.xcodeproj` into your project or workspace, and do the same with its dependencies (i.e. the other `.xcodeproj` files included in `Result.xcworkspace`). NB: `Result.xcworkspace` is for standalone development of Result, while `Result.xcodeproj` is for targets using Result as a dependency.
3. Link your target against `Result.framework` and each of the dependency frameworks.
4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Result and its dependencies.)
2. Drag `Result.xcodeproj` into your project or workspace.
3. Link your target against `Result.framework`.
4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Result.)
34 changes: 4 additions & 30 deletions Result.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,7 @@
D45480981A957465009D7229 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = D45480961A957465009D7229 /* Result.swift */; };
D45480991A9574B8009D7229 /* ResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D454806E1A9572F5009D7229 /* ResultTests.swift */; };
D454809A1A9574BB009D7229 /* Result.h in Headers */ = {isa = PBXBuildFile; fileRef = D454805C1A9572F5009D7229 /* Result.h */; settings = {ATTRIBUTES = (Public, ); }; };
D47B107E1A9AAC3C006701A8 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107B1A9AAC3C006701A8 /* Box.framework */; };
D47B107F1A9AAC3C006701A8 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107C1A9AAC3C006701A8 /* Either.framework */; };
D47B10801A9AAC3C006701A8 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107D1A9AAC3C006701A8 /* Prelude.framework */; };
D47B10841A9AAC51006701A8 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107B1A9AAC3C006701A8 /* Box.framework */; };
D47B10851A9AAC51006701A8 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107C1A9AAC3C006701A8 /* Either.framework */; };
D47B10861A9AAC51006701A8 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107D1A9AAC3C006701A8 /* Prelude.framework */; };
D47B10871A9AAC54006701A8 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107B1A9AAC3C006701A8 /* Box.framework */; };
D47B10881A9AAC54006701A8 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107C1A9AAC3C006701A8 /* Either.framework */; };
D47B10891A9AAC54006701A8 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107D1A9AAC3C006701A8 /* Prelude.framework */; };
D47B108A1A9AAC54006701A8 /* Box.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107B1A9AAC3C006701A8 /* Box.framework */; };
D47B108B1A9AAC54006701A8 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107C1A9AAC3C006701A8 /* Either.framework */; };
D47B108C1A9AAC54006701A8 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D47B107D1A9AAC3C006701A8 /* Prelude.framework */; };
D48EA2DA1AE3D9FE00B23F6F /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = D48EA2D91AE3D9FE00B23F6F /* State.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -56,51 +45,37 @@
D454807D1A957361009D7229 /* Result.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D45480871A957362009D7229 /* Result-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Result-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D45480961A957465009D7229 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
D47B107B1A9AAC3C006701A8 /* Box.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Box.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D47B107C1A9AAC3C006701A8 /* Either.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Either.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D47B107D1A9AAC3C006701A8 /* Prelude.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Prelude.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D48EA2D91AE3D9FE00B23F6F /* State.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
D45480531A9572F5009D7229 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D47B107E1A9AAC3C006701A8 /* Box.framework in Frameworks */,
D47B107F1A9AAC3C006701A8 /* Either.framework in Frameworks */,
D47B10801A9AAC3C006701A8 /* Prelude.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D45480641A9572F5009D7229 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D47B10871A9AAC54006701A8 /* Box.framework in Frameworks */,
D45480681A9572F5009D7229 /* Result.framework in Frameworks */,
D47B10891A9AAC54006701A8 /* Prelude.framework in Frameworks */,
D47B10881A9AAC54006701A8 /* Either.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D45480791A957361009D7229 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D47B10841A9AAC51006701A8 /* Box.framework in Frameworks */,
D47B10861A9AAC51006701A8 /* Prelude.framework in Frameworks */,
D47B10851A9AAC51006701A8 /* Either.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D45480841A957362009D7229 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D47B108A1A9AAC54006701A8 /* Box.framework in Frameworks */,
D45480881A957362009D7229 /* Result.framework in Frameworks */,
D47B108C1A9AAC54006701A8 /* Prelude.framework in Frameworks */,
D47B108B1A9AAC54006701A8 /* Either.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -132,6 +107,7 @@
children = (
D454805C1A9572F5009D7229 /* Result.h */,
D45480961A957465009D7229 /* Result.swift */,
D48EA2D91AE3D9FE00B23F6F /* State.swift */,
D454805A1A9572F5009D7229 /* Supporting Files */,
);
path = Result;
Expand All @@ -140,9 +116,6 @@
D454805A1A9572F5009D7229 /* Supporting Files */ = {
isa = PBXGroup;
children = (
D47B107B1A9AAC3C006701A8 /* Box.framework */,
D47B107C1A9AAC3C006701A8 /* Either.framework */,
D47B107D1A9AAC3C006701A8 /* Prelude.framework */,
D454805B1A9572F5009D7229 /* Info.plist */,
);
name = "Supporting Files";
Expand Down Expand Up @@ -339,6 +312,7 @@
buildActionMask = 2147483647;
files = (
D45480971A957465009D7229 /* Result.swift in Sources */,
D48EA2DA1AE3D9FE00B23F6F /* State.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 0 additions & 9 deletions Result.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 19 additions & 30 deletions Result/Result.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright (c) 2015 Rob Rix. All rights reserved.

/// An enum representing either a failure with an explanatory error, or a success with a result value.
public struct Result<T, Error>: EitherType, Printable, DebugPrintable {
public struct Result<T, Error>: Printable, DebugPrintable {
// MARK: Constructors

/// Constructs a success wrapping a `value`.
public init(value: T) {
either = .right(value)
state = .Success(Box(value))
}

/// Constructs a failure wrapping an `error`.
public init(error: Error) {
either = .left(error)
state = .Failure(Box(error))
}

/// Constructs a result from an Optional, failing with `Error` if `nil`
Expand All @@ -34,19 +34,19 @@ public struct Result<T, Error>: EitherType, Printable, DebugPrintable {

/// Returns the value from `Success` Results, `nil` otherwise.
public var value: T? {
return either.right
return analysis(ifSuccess: { $0 }, ifFailure: { _ in nil })
}

/// Returns the error from `Failure` Results, `nil` otherwise.
public var error: Error? {
return either.left
return analysis(ifSuccess: { _ in nil }, ifFailure: { $0 })
}

/// Case analysis for Result.
///
/// Returns the value produced by applying `ifFailure` to `Failure` Results, or `ifSuccess` to `Success` Results.
public func analysis<Result>(@noescape #ifSuccess: T -> Result, @noescape ifFailure: Error -> Result) -> Result {
return either.either(ifLeft: ifFailure, ifRight: ifSuccess)
return state.analysis(ifSuccess: ifSuccess, ifFailure: ifFailure)
}


Expand Down Expand Up @@ -89,21 +89,6 @@ public struct Result<T, Error>: EitherType, Printable, DebugPrintable {
}


// MARK: EitherType

public static func left(error: Error) -> Result {
return failure(error)
}

public static func right(value: T) -> Result {
return success(value)
}

public func either<Result>(@noescape #ifLeft: Error -> Result, @noescape ifRight: T -> Result) -> Result {
return either.either(ifLeft: ifLeft, ifRight: ifRight)
}


// MARK: Printable

public var description: String {
Expand All @@ -120,18 +105,24 @@ public struct Result<T, Error>: EitherType, Printable, DebugPrintable {
}


// MARK: Private
// MARK: State

private var either: Either<Error, T>
private init(state: State<T, Error>) {
self.state = state
}

private let state: State<T, Error>
}


/// Returns `true` if `left` and `right` are both `Success`es and their values are equal, or if `left` and `right` are both `Failure`s and their errors are equal.
public func == <T: Equatable, Error: Equatable> (left: Result<T, Error>, right: Result<T, Error>) -> Bool {
return
(left.value &&& right.value).map { $0 == $1 }
?? (left.error &&& right.error).map { $0 == $1 }
?? false
if let left = left.value, right = right.value {
return left == right
} else if let left = left.error, right = right.error {
return left == right
}
return false
}

/// Returns `true` if `left` and `right` represent different cases, or if they represent the same case but different values.
Expand All @@ -148,7 +139,7 @@ public func ?? <T, Error> (left: Result<T, Error>, @autoclosure right: () -> T)
/// Returns `left` if it is a `Success`es, or `right` otherwise. Short-circuits.
public func ?? <T, Error> (left: Result<T, Error>, @autoclosure right: () -> Result<T, Error>) -> Result<T, Error> {
return left.analysis(
ifSuccess: const(left),
ifSuccess: { _ in left },
ifFailure: { _ in right() })
}

Expand Down Expand Up @@ -199,6 +190,4 @@ public func >>- <T, U, Error> (result: Result<T, Error>, @noescape transform: T

// MARK: - Imports

import Either
import Prelude
import Foundation
24 changes: 24 additions & 0 deletions Result/State.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2015 Rob Rix. All rights reserved.

/// Implementation details of `Result`.
internal enum State<T, Error> {
func analysis<Result>(@noescape #ifSuccess: T -> Result, @noescape ifFailure: Error -> Result) -> Result {
switch self {
case let .Success(value):
return ifSuccess(value.value)
case let .Failure(value):
return ifFailure(value.value)
}
}

case Success(Box<T>)
case Failure(Box<Error>)
}

internal final class Box<T> {
init(_ value: T) {
self.value = value
}

let value: T
}

0 comments on commit 1756b77

Please sign in to comment.