Code Monkey home page Code Monkey logo

either.java's Introduction

Either.java

Java CI with Maven

A right-biased implementation of Haskell's Either a b for Java, using Java 8 for mapping/folding and type inference.

An Either<A,B> is used to "condense" two distinct options down to a single choice.

"Right-biased" means that, like Haskell's Either a b, when an Either<A, B> has both options available, it prefers the one on the "right" (that is, B). The mnemonic often used in Haskell to remember/explain this "bias" is that an Either (usually used for error-checking), gives you either the right answer, or else whatever's left.

Wait, what?

Yes, polymorphism is great in Java. Animal.speak(), Dog.speak(), Cat.speak(), and so on.

But sometimes you want to be REALLY EXPLICIT about the possible types of objects you're dealing with, either (ha ha) because they don't logically make sense in the same inheritance structure or because you want to limit your possible cases to exactly two classes.

For example, let's say I'm dealing with my code and some third-party library that still uses java.util.Date (a class that has been largely deprecated since JDK 1.1, and is almost always a strictly-worse choice than using either the newer java.time apis, or Joda Time) .

Terrible, I know, but there's always horrible legacy code out there in the world.

I don't want to further propagate this old usage of a terrible deprecated class, so I, a Modern Java Developer, prefer to use java.time.LocalDateTime in my code.

Except great, now I've gotta deal with grabbing a List<LocalDateTime> from these and a List<Date> from those and then do a bunch of gnarly conversion everywhere.

Using Either.java, I can instead easily build a List<Either<Date,LocalDatetime>>. This tells me (and the compiler) that I'm dealing with a bunch of things that are either a Date (yuck) or a LocalDateTime.

I can even nicely convert them all into LocalDateTimes:

List<Either<Date, LocalDateTime>> birthdays = allThePeople.stream()
                                                          .map(person -> Either.<Date,LocalDateTime>either(this::getDeprecatedDOBFromPerson, this::getNiceNewDOBFromPersonIfAvailable)
                                                          .collect(Collectors.toList());

List<LocalDateTime> convertedBirthdays = birthdays.stream()
                             .map(eitherDateOrLocalDateTime.fold(
                                (Date deprecatedDOB)   -> myFunctionToConvertDateToLocalDateTime(deprecatedDOB),
                                (LocalDatetime newDOB) -> newDOB
                             )).collect(Collectors.toList());

Boom. Now you have a list of LocalDateTimes, and it's explicit from the code that some of the people had deprecated Dates, while others had nice, shiny new LocalDateTimes. Even the compiler can tell!

Other common use-cases for an Either include capturing and handling errors gracefully using Either<SomeKindOfException, SuccessfulResultClass>. As mentioned earlier, the convention in the Haskell world (from which I totally "borrowed" the Either) is that an Either gives you "either the Right answer or whatever's Left" -- that is, errors on the left, expected output on the right.

For this reason, this Either is right-biased; if you give it Either.either(()->42, ()->"Hello, World!");, you'll get a Right containing "Hello, World!", not a Left containing 42. I swear, it's not a political thing; there just needs to be a predictable rule-of-thumb for how to handle it when the Either gets both a left value and a right value (after all, it's called an Either, not a Both).

Result

Because this errors-to-the-left, results-to-the-right idiom is so common, this library also includes a Result<T> class, which instead of being Left<L,R> or Right<L,R> is Err<T> or Ok<T>. Instead of Either.either(() -> "left", () -> 42), the constructor method you'll want is Result.attempt(() -> someMethodThatMightThrowAnException()). You can even chain a series of possibly-failing functions using map:

Result<C> = Result.attempt(() -> someOperationThatMightFailOrReturnA())
                  .map(a -> someOtherOperationThatMightFailOReturnB(a))
                  .map(b -> someThirdOperationThatMightFailOrReturnC(b));

EitherCollectors

If you're working with collections of Eithers (for example, you're performing a series of validations, all of which can either succeed or return a validation error message), you'll probably be interested in EitherCollectors, with its .toLeftBiased() and .toRightBiased() methods for use with Java Streams:

// Let's say you have this variable already defined from your validations:
// List<Either<ValidationError, ValidationSuccess>> validationResults 

// And you want to fail the user's request if there's *any* error, and return *all* the errors if so. EitherCollectors can help!
Either<List<ValidationError>, List<ValidationSuccess>> errorsOrSuccesses = validationResults.stream()
																				.collect(EitherCollectors.toLeftBiased());

// If there's even *one* left-hand ValidationError in our list, then we'll get a Left containing *all* the ValiidationErrors.
// If not, we'll get a Right containing any ValidationSuccess results.

// So we can do this:
return errorsOrSuccesses.fold(
	(List<ValidationError> errors) -> new UserFacingErrorsResponse(errors),
	(List<ValidationSuccess successes) -> new SuccessResponse(successes)
);

So what else can it do?

Wanna see more? Check out the unit tests for a run-down of how Eithers behave. If those tests aren't descriptive enough, or you think they should behave differently, open a Github issue! I built this because it didn't look like anyone else had built it for Java yet, and I may have lost something in the translation. I'm totally open for feedback.

Cool! How do I get it for my project?

Simple! It's in Maven Central, so just add this to your pomfile (or the equivalent for gradle/sbt/lein/whatever):

<dependency>
    <groupId>com.spencerwi</groupId>
    <artifactId>Either.java</artifactId>
    <version>2.9.0</version>
</dependency>

either.java's People

Contributors

hamishlawson avatar jartysiewicz avatar michaldo avatar peterunold avatar sfesenko avatar spencerwi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

either.java's Issues

Result.map doesn't handle exceptions, as described in the readme

Next scenario from the readme fails, however it's expected that it'll wrap exceptions from chained methods

@Test
public void map() {
    Result<?> result = Result.attempt(() -> someOperationThatMightFailOrReturnA())
            .map(a -> someOtherOperationThatMightFailOReturnB(a))
            .map(b -> someThirdOperationThatMightFailOrReturnC(b));
    Assert.assertTrue(result.isErr());
}

private Object someThirdOperationThatMightFailOrReturnC(Object b) {
    return Objects.requireNonNull(b);
}

private Object someOtherOperationThatMightFailOReturnB(Object a) {
    return Objects.requireNonNull(a);
}

private Object someOperationThatMightFailOrReturnA() {
    return null;
}
java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at com.spencerwi.either.ResultMapTest.someOtherOperationThatMightFailOReturnB(ResultMapTest.java:27)
	at com.spencerwi.either.ResultMapTest.lambda$map$1(ResultMapTest.java:16)
	at com.spencerwi.either.Result$Ok.map(Result.java:108)

Result.flatMaps type signature is surprising

map and FlatMap have basically the same Type Signature apart from Exception Handling, which is wrong.

public abstract <T> Result<T> map(Function<R,T> transformValue);
public abstract <T> Result<T> flatMap(ExceptionThrowingFunction<R,T> transformValue);

Compared to flatMap on Scalas Try (or pretty much any other Class with flatMap in any functional Language):

 flatMap[U](f: (T) โ‡’ Try[U]): Try[U] 

I'd like to have a "real" flatMap Function. Catching Exceptions and translating to Result should be done by both, map and flatMap.

License?

Hi @spencerwi,

I was wondering if you can think about and then add a license for this library (which is very nice and simple that's why I like it!).

Thank you very much!

Either contains 2 value pointers instead of 1

What's the reason for using 2 value pointers per element instead of one:

public abstract class Either<L, R> {
    protected L leftValue;
    protected R rightValue;

It seems like every indirection via Either will double the required space for the pointers. Shy of a union type, you could have used java.lang.Object and cast it dynamically to L or R.

Run method for Result

Hello,
I was very happy you made the effort to create this library.
Also I like how you added the Result-approach.

I am missing a run method for the Result class.
There is one in the Either so I will change my current usage of the library to the Either-Approach.

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.