Code Monkey home page Code Monkey logo

algebra's Introduction

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");
  }

algebra's People

Contributors

szabowexler avatar stevie400 avatar hs-jenkins-bot avatar jhartz avatar jhaber avatar zklapow avatar gmessinese avatar mjball 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.