Giter Site home page Giter Site logo

result-flow's Introduction

result-flow

Java library for functional style "Railway Oriented Programming"

This library was inspired by Railway Oriented Programming and the Result type of the Rust programming language. It contains utilities that enable functional style programming in Java with under-the-hood error handling without having to define try-catch-blocks or conditionals.

Example

$ cat > Example.java
import de.perschon.resultflow.Result;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

// To build and run:
// 	javac -cp "target/*" Example.java
//	java -cp ".:target/*" Example
public class Example {

  public static void main(String[] args) {
    Result<String> y = Result
      .ok(args)
      .map(x -> x[0])
      .mapWrap(URL::new)
      .mapWrap(URL::openConnection)
      .mapWrap(URLConnection::getInputStream)
      .mapWrap(InputStream::readAllBytes)
      .map(String::new)
      .map(x -> x.substring(0, Integer.min(100, x.length())));
    if (y.isOk()) {
      System.out.println(args[0] + "\n------------------\n" + y.getValue());
    } else {
      System.err.println("FAIL");
      y.getError().printStackTrace();
      System.exit(1);
    }
  }
}
^D

$ javac -cp "target/*" Example.java
$ java -cp ".:target/*" Example
FAIL
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
	at Example.lambda$main$0(Example.java:15)
	at de.perschon.resultflow.Result.map(Result.java:136)
	at Example.main(Example.java:15)
$ 

$ java -cp ".:target/*" Example abc
FAIL
java.lang.RuntimeException: java.net.MalformedURLException: no protocol: abc
	at de.perschon.resultflow.Result.mapWrap(Result.java:156)
	at Example.main(Example.java:16)
Caused by: java.net.MalformedURLException: no protocol: abc
	at java.base/java.net.URL.<init>(URL.java:674)
	at java.base/java.net.URL.<init>(URL.java:569)
	at java.base/java.net.URL.<init>(URL.java:516)
	at de.perschon.resultflow.Result.mapWrap(Result.java:151)
	... 1 more

$ java -cp ".:target/*" Example http://example.com 
http://example.com
------------------
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <m
$ 

Other related sites

  1. Railway Oriented Programming. Scott Wlaschin's 2013 blog post that explains a result monad in pictures of rail road tracks.

  2. Against Railway-Oriented Programming When a Result monad is not a good fit.

  3. kotlin-result active project with a much richer implementation.

  4. Macsoftex/result-flow Another result-flow fork that has added apply, fold, flatMap, flatMapFailure, and flatMapOrElse methods.

result-flow's People

Contributors

leopard2a5 avatar mbucc avatar

Watchers

 avatar  avatar

result-flow's Issues

map(unchecked(...)) doesn't always work right

I think i need a new method called mapchecked.

if the lambda passed to unchecked throws an exception, the uncheck code catches it and rethrows it. however, the try/catch in map does not catch this, and the exception breaks out of the monad.

what wanted was to always get a Result out of the map(unchecked()) call.

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.