Algebra
Simple abstract data types (wrapping derive4j) in Java
Install / Use
/learn @HubSpot/AlgebraREADME
algebra
Generic ADTs in Java.
Using
To use algebra in your project simply include it in your POM:
<dependency>
<groupId>com.hubspot</groupId>
<artifactId>algebra</artifactId>
<version>1.2</version>
</dependency>
Provided Types
The main type provided by algebra is the Result<T, E>. 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.
enum Error {
BROKEN,
REALLY_BROKEN
}
public Result<String, Error> doWork() {
if (success) {
return Result.ok(result);
} else if (thing1Broke) {
return Result.err(Error.BROKEN);
}
return Result.err(Error.REALLY_BROKEN);
}
If 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:
enum LibError {
ERROR
}
public Result<String, LibError> doMoreWork() {
return doWork().mapErr(err -> LibError.ERROR);
}
Of course when it gets to our client and they actually want to handle the error they can do so using match:
client.doMoreWork().match(
err -> LOGGER.error("Got error: {}", err),
ok -> LOGGER.info("Got successful result: {}", ok)
);
Testing
To test code with ADTs, we provide a fluent AssertJ API in algebra-testing.
To use algebra-testing in your project simply include it in your POM:
<dependency>
<groupId>com.hubspot</groupId>
<artifactId>algebra-testing</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
Add a static import and use the assertions:
import static com.hubspot.assertj.algebra.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void itWorks() {
assertThat(Result.ok("Ok"))
.isOk()
.containsOk("Ok");
}
Related Skills
node-connect
339.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
