{"id":20264005,"url":"https://github.com/robrix/Either","last_synced_at":"2025-05-07T15:30:52.504Z","repository":{"id":23976001,"uuid":"27358798","full_name":"robrix/Either","owner":"robrix","description":"Swift µframework of Either, which represents two alternatives.","archived":false,"fork":false,"pushed_at":"2018-09-03T23:22:19.000Z","size":129,"stargazers_count":150,"open_issues_count":0,"forks_count":15,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-08T01:51:24.661Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robrix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-01T02:20:44.000Z","updated_at":"2023-07-09T14:52:16.000Z","dependencies_parsed_at":"2022-08-22T06:51:05.625Z","dependency_job_id":null,"html_url":"https://github.com/robrix/Either","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robrix%2FEither","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robrix%2FEither/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robrix%2FEither/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robrix%2FEither/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robrix","download_url":"https://codeload.github.com/robrix/Either/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252905635,"owners_count":21822835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T11:37:13.457Z","updated_at":"2025-05-07T15:30:51.758Z","avatar_url":"https://github.com/robrix.png","language":"Swift","funding_links":[],"categories":["Data Structure"],"sub_categories":[],"readme":"# Either\n\nThis is a Swift microframework providing `Either\u003cLeft, Right\u003e` and `EitherProtocol`, with generic implementations of `==`/`!=` where `Left` \u0026 `Right`: `Equatable`.\n\n`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\u003cInt, NSError\u003e` might be the result of a computation which could fail, while `Either\u003cString, String\u003e` might mean that you want to handle a string in one of two different ways.\n\n`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.\n\n\n## Use\n\nConstructing an `Either`:\n\n```swift\n// Wrap:\nlet left = Either\u003cInt, String\u003e.left(4)\nlet right = Either\u003cInt, String\u003e.right(\"four\")\n```\n\nExtracting the value:\n\n```swift\n// Unwrap:\nlet value = left.either(ifLeft: { $0 }, ifRight: { $0.characters.count })\n```\n\nRepresenting success/failure:\n\n```swift\nlet result = someComputation() // result has type `Either\u003cError, T\u003e`\nlet success = result.right // success has type `T?`\nlet error = result.left    // error has type `Error?`\n```\n\nHowever, you might instead prefer to use a [more tailored `Result`](https://github.com/antitypical/Result) type. Even if it doesn’t conform to `EitherProtocol` already, you can implement conformance in your application:\n\n```swift\nextension Result: EitherProtocol { // Result\u003cT, Error\u003e\n\tstatic func toLeft(_ value: Error) -\u003e Result {\n\t\treturn Result(error: value)\n\t}\n\n\tstatic func toRight(value: T) -\u003e Result {\n\t\treturn Result(value: value)\n\t}\n\n\tfunc either\u003cResult\u003e(ifLeft: (Error) -\u003e Result, ifRight: (T) -\u003e Result) -\u003e Result {\n\t\tswitch self {\n\t\tcase let .success(x):\n\t\t\treturn g(x)\n\t\tcase let .failure(error):\n\t\t\treturn f(error)\n\t\t}\n\t}\n}\n```\n\nNow you can use generic functions like `==`, `!=`, and any you might write with both `Either` and `Result`.\n\nAPI documentation is in the source.\n\n\n## Integration\n\n1. 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.\n2. 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.\n3. Link your target against `Either.framework` and each of the dependency frameworks.\n4. 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.)\n\nOr use the Swift package manager and add this to you `Package.swift` file:  \n```\n  ...\n  dependencies: [\n    ...  \n    .package(url: \"https://github.com/robrix/Either\", \"2.0.1\" ..\u003c \"3.0.0\")\n  ],\n  targets: [\n    ...\n    .target(\n        name: \"\u003cYourTargetName\u003e\",\n        dependencies: [\"Either\"]),\n  ]\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobrix%2FEither","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobrix%2FEither","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobrix%2FEither/lists"}