Skip to content

Latest commit

 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

try-monad

Build

Inspired by scala.util.Try, this is a small library that provides an abstraction of a result of a function application.

Getting Started

Maven

<dependency>
    <groupId>com.jecklgamis</groupId>
    <artifactId>try-monad</artifactId>
    <version>1.0</version>
</dependency>

Gradle

compile 'com.jecklgamis:try-monad:1.0'

Example Usage

import java.net.Socket;
import static com.jecklgamis.util.TryFactory.attempt;

public class ExampleUsage {
    public static boolean canConnect(String host, int port) {
        return attempt(() -> new Socket(host, port))
                .map(Socket::isConnected)
                .getOrElse(false);
    }
}

TryFactory.attempt wraps a computation that may throw, returning a Success or Failure. Subsequent calls like map and getOrElse chain naturally — map is skipped on Failure, and getOrElse provides a fallback value.

Building

mvn package

About

Try() monad in Java 8

Resources

Stars

10 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages