try-monad

A Java Try Monad

License

License

GroupId

GroupId

com.jecklgamis
ArtifactId

ArtifactId

try-monad
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

try-monad
A Java Try Monad
Project URL

Project URL

http://github.com/jecklgamis/try-monad
Source Code Management

Source Code Management

http://github.com/jecklgamis/try-monad/tree/master

Download try-monad

How to add to project

<!-- https://jarcasting.com/artifacts/com.jecklgamis/try-monad/ -->
<dependency>
    <groupId>com.jecklgamis</groupId>
    <artifactId>try-monad</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.jecklgamis/try-monad/
implementation 'com.jecklgamis:try-monad:1.0'
// https://jarcasting.com/artifacts/com.jecklgamis/try-monad/
implementation ("com.jecklgamis:try-monad:1.0")
'com.jecklgamis:try-monad:jar:1.0'
<dependency org="com.jecklgamis" name="try-monad" rev="1.0">
  <artifact name="try-monad" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jecklgamis', module='try-monad', version='1.0')
)
libraryDependencies += "com.jecklgamis" % "try-monad" % "1.0"
[com.jecklgamis/try-monad "1.0"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

try-monad Build Status

About

Inspired by scala.util.Try, this is small library that provides an abstraction of 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'
Apache Buildr
'com.jecklgamis:try-monad:jar:1.0'
Groovy Grapes
@Grapes(
@Grab(group='com.jecklgamis', module='try-monad', version='1.0')
)
Leiningen
[com.jecklgamis/try-monad "1.0"]

A Primer On Using Try

Below is an example util function to check socket connection to a specified host.

import java.io.IOException;
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((s) -> s.isConnected()).getOrElse(false);
    }

    public static void main(String[] args) {
        System.out.println(canConnect("localhost", 80));
    }
}

The canConnect function uses TryFactory.attempt (similar to try) to invoke a function that returns a socket instance (() → new Socket). It will then return an instance of Try , specifically a Success if there is no exception thrown, or Failure otherwise.

If the call is successful, the function passed to map (i.e. (s) → s.isConnected()) will be invoked. The s parameter in the expression is the result of function in attempt (a socket instance). If attempt fails (attempt returned a Failure), map will not be invoked and simply returns the Failure returned by attempt.

The last call in the chain is getOrElse(false), which will return a boolean depending on the output of map. If the map returns a Success (no exception thrown in (s)→ s.isConnected), `getOrElse will simply return the value of the function passed to map. Otherwise, getOrElse will return the given value, which in this case false.

Versions

Version
1.0