{"id":31970527,"url":"https://github.com/hubspot/algebra","last_synced_at":"2026-01-07T17:22:20.112Z","repository":{"id":49562443,"uuid":"125133410","full_name":"HubSpot/algebra","owner":"HubSpot","description":"Simple abstract data types (wrapping derive4j) in Java","archived":false,"fork":false,"pushed_at":"2025-08-11T17:57:35.000Z","size":167,"stargazers_count":18,"open_issues_count":4,"forks_count":7,"subscribers_count":154,"default_branch":"master","last_synced_at":"2025-08-11T19:26:24.237Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HubSpot.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-03-14T00:45:00.000Z","updated_at":"2025-08-11T17:57:12.000Z","dependencies_parsed_at":"2024-03-12T19:40:36.921Z","dependency_job_id":"ec557361-77eb-4659-88d6-46584c297941","html_url":"https://github.com/HubSpot/algebra","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/HubSpot/algebra","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HubSpot%2Falgebra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HubSpot%2Falgebra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HubSpot%2Falgebra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HubSpot%2Falgebra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HubSpot","download_url":"https://codeload.github.com/HubSpot/algebra/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HubSpot%2Falgebra/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279020649,"owners_count":26086895,"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","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-10-14T19:16:00.538Z","updated_at":"2025-10-14T19:16:08.925Z","avatar_url":"https://github.com/HubSpot.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# algebra\nGeneric ADTs in Java.\n\n## Using\n\nTo use `algebra` in your project simply include it in your POM:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.hubspot\u003c/groupId\u003e\n  \u003cartifactId\u003ealgebra\u003c/artifactId\u003e\n  \u003cversion\u003e1.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Provided Types\n\nThe main type provided by `algebra` is the `Result\u003cT, E\u003e`. `T` and `E` can be any classes you want, but it is generally advisable that `E` be an enum or another ADT so you can correctly match on error conditions. Lets take a look at an example.\n\n```java\nenum Error {\n  BROKEN,\n  REALLY_BROKEN\n}\n\npublic Result\u003cString, Error\u003e doWork() {\n  if (success) {\n    return Result.ok(result);\n  } else if (thing1Broke) {\n    return Result.err(Error.BROKEN);\n  }\n  \n  return Result.err(Error.REALLY_BROKEN);\n}\n```\n\nIf we then want to use `doWork` in a library, but our library only has one error type, we can simply map the error and return a new result:\n\n```java\n\nenum LibError {\n  ERROR\n}\n\npublic Result\u003cString, LibError\u003e doMoreWork() {\n  return doWork().mapErr(err -\u003e LibError.ERROR);\n}\n```\n\nOf course when it gets to our client and they actually want to handle the error they can do so using `match`:\n\n```java\nclient.doMoreWork().match(\n  err -\u003e LOGGER.error(\"Got error: {}\", err),\n  ok -\u003e LOGGER.info(\"Got successful result: {}\", ok)\n);\n```\n\n## Testing\n\nTo test code with ADTs, we provide a fluent AssertJ API in `algebra-testing`.\n\nTo use `algebra-testing` in your project simply include it in your POM:\n\n```xml\n    \u003cdependency\u003e\n      \u003cgroupId\u003ecom.hubspot\u003c/groupId\u003e\n      \u003cartifactId\u003ealgebra-testing\u003c/artifactId\u003e\n      \u003cversion\u003e1.2\u003c/version\u003e\n      \u003cscope\u003etest\u003c/scope\u003e\n    \u003c/dependency\u003e\n```\n\nAdd a static import and use the assertions:\n\n```java\nimport static com.hubspot.assertj.algebra.api.Assertions.assertThat;\nimport static org.assertj.core.api.Assertions.assertThat;\n```\n\n```java\n  @Test\n  public void itWorks() {\n    assertThat(Result.ok(\"Ok\"))\n        .isOk()\n        .containsOk(\"Ok\");\n  }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhubspot%2Falgebra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhubspot%2Falgebra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhubspot%2Falgebra/lists"}