Coder Social home page Coder Social logo

either's Introduction

Either

This is a Swift microframework providing Either<Left, Right> and EitherProtocol, with generic implementations of ==/!= where Left & Right: Equatable.

Either allows you to specify that a value should be one of two types, or that that a value of a single type should have one of two semantics. For example, Either<Int, NSError> might be the result of a computation which could fail, while Either<String, String> might mean that you want to handle a string in one of two different ways.

EitherProtocol is an easy-to-adopt protocol (it requires one method and two constructor functions) which allows clients to generically use Either or conforming Result, etc. types.

Use

Constructing an Either:

// Wrap:
let left = Either<Int, String>.left(4)
let right = Either<Int, String>.right("four")

Extracting the value:

// Unwrap:
let value = left.either(ifLeft: { $0 }, ifRight: { $0.characters.count })

Representing success/failure:

let result = someComputation() // result has type `Either<Error, T>`
let success = result.right // success has type `T?`
let error = result.left    // error has type `Error?`

However, you might instead prefer to use a more tailored Result type. Even if it doesn’t conform to EitherProtocol already, you can implement conformance in your application:

extension Result: EitherProtocol { // Result<T, Error>
	static func toLeft(_ value: Error) -> Result {
		return Result(error: value)
	}

	static func toRight(value: T) -> Result {
		return Result(value: value)
	}

	func either<Result>(ifLeft: (Error) -> Result, ifRight: (T) -> Result) -> Result {
		switch self {
		case let .success(x):
			return g(x)
		case let .failure(error):
			return f(error)
		}
	}
}

Now you can use generic functions like ==, !=, and any you might write with both Either and Result.

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 if you’re using carthage to manage your dependencies.
  2. Drag Either.xcodeproj into your project or workspace, and do the same with its dependencies (i.e. the other .xcodeproj files included in Either.xcworkspace). NB: Either.xcworkspace is for standalone development of Either, while Either.xcodeproj is for targets using Either as a dependency.
  3. Link your target against Either.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 Either and its dependencies.)

either's People

Contributors

335g avatar abizern avatar alexrozanski avatar angerman avatar bencochran avatar gfontenot avatar neilpa avatar pocket7878 avatar robrix avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.