better-java-monads

by jasongoodwin

jasongoodwin / better-java-monads
211 Stars 35 Forks Last release: Not found Other 61 Commits 2 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

better-java8-monads

I can't help but feel there were some important omissions in the Java8 library. This library attempts to fill in some of those short-comings.

Feature Overview

Try - Optional exists to express nulls in types, but there is no way to express success/failure in types. Try to the rescue! The Try type is very similar to the Try in Scala's standard lib.

CompletableFuture.sequence - If you have a list of futures, there is no obvious way to get a future of a list. This will come in handy!

Usage

Import into your project:

SBT

"com.jason-goodwin" % "better-monads" % "0.4.0"

Maven

    com.jason-goodwin
    better-monads
    0.4.0

Try

The try monad was attributed to Twitter and placed into the Scala standard library. While both Scala and Haskell have a monad Either which has a left and a right type, a Try is specifically of a type T on success or an exception on failure.

Usage

The Try api is meant to be similar to the Optional type so has the same functions. - get() returns the held value or throws the thrown exception - getUnchecked() returns the held value or throws the thrown exception wrapped in a RuntimeException instance - map(x) maps the success value x to a new value and type or otherwise passes the Failure forward. - flatMap((x) -> f(x)) maps the success value x to a new Try of f(x). - recover((t) -> x) will return the success value of the Try in the success case or the value x in the failure case. Exposes the exception. - recoverWith((t) -> f(x)) will return the success value of the Try in the success case or a new try of f(x) in the failure case. Exposes the exception. - filter((x) -> isTrue(x)) - If Success, returns the same Success if the predicate succeeds, otherwise, returns a Failure with type NoSuchElementException. - onSuccess((x) -> f(x)) execute some code on success - takes Consumer (eg requires no return value). - onFailure((x) -> f(x)) execute some code on failure - takes Consumer (eg requires no return value). - orElse(x) will return the success value of the Try in success case or the value x in failure case. - orElseTry(f) will return the success value of the Try in success case or a new Try(f) in the failure case. - orElseThrow(() -> throw new T) gets result or on failure will throw checked exception of type T - toOptional() will return Optional of success value of Try (if not null), otherwise it will return an empty Optional

Futures

There is no sequence method in the Java8 library so I've provided one. You'll find the function Futures.sequence which will convert a

List>
into a
CompletableFuture>
. This is useful in the common use case of processing all of the elements of a list concurrently.

Usage

Simply call Futures.sequence on a

List>
to get back a single future with the list of your items.
List list = IntStream.range(0, 100).boxed().collect(Collectors.toList());
    int size = list.size();
    List> futures = list
            .stream()
            .map(x -> CompletableFuture.supplyAsync(() -> x))
            .collect(Collectors.toList());

CompletableFuture> futureList = Futures.sequence(futures);

Tests

See the tests for examples of all functionality.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.