Coder Social home page Coder Social logo

utils's Introduction

utils

Some utilities to make Java 9 even more expressive. This will be updated with code examples in the near future

Try

Try is a java version of the Scala type.

ForComp

ForComp attempts to give Java a little of the power of for comprehension similar to that of Scala.

Obviously, without the language having for comprehension built in there must be some compromises, but the result of the for comprehension here is still a powerful tool for Java.

The test cases give some good examples of usage

An example of swapping a nest loop for for comp ( full example )

    private int nestedLoops() {
        int result = 0;

        for (int i = 0; i < 100; i ++) {
            for(int j = 0; j < 50; j++) {
                for(int k = 0; k < 20; k++) {
                    result += (i + j + k);
                }
            }
        }

        return result;
    }

    private int nestedForComp() {
         return forComp(stream(0, 99)).
                with(stream(0, 49)).
                with(stream(0, 19)).
                yieldFlat().mapToInt(i -> i).sum();
    }

Match

Match gives a similar pattern matching function to that of Scala's match. Match.match takes a subject to match on and then a number of Case types for the patterns.

Like Scala's match if no pattern matches then a MatchException will be thrown

The Case class takes a predicate and a function that will be called if matched.

There are three helper methods in the Match class to help create Case types: matchCase creates a Case based on the predicate and function supplied typeCase takes a Class and matched based on type comparison defaultCase only takes a function and is used as a fall through match

Also like Scala's match, order is important and the match function will complete on the first pattern matched

Some contrived examples ( full example )

        final MatchExample example = new MatchExample();
        final Try<String> tryOne = example.successString();
        final Try<String> tryTwo = example.failureString();

        boolean worked = match(tryOne,
                typeCase(Success.class, o -> true),
                typeCase(Failure.class, o -> false));

        if (!worked) throw new RuntimeException("Should have worked");

        worked = match(tryTwo,
                typeCase(Success.class, o -> true),
                typeCase(Failure.class, o -> false));

        if (worked) throw new RuntimeException("Shouldn't have worked");


        final int i = 28;

        final int result = match(i,
                matchCase(e -> e + 1 == 30, e -> 30),
                matchCase(e -> e / 4 == 7, e -> 7));

        if (result != 7) throw new RuntimeException("Should've been 7");

        final Double pi = 3.1415927;

        final String answer = match(pi,
                typeCase(Integer.class, j -> "Not today"),
                matchCase(j -> j == 3.1415927, j-> "Eureka!"));

        if (!answer.equals("Eureka!")) throw new RuntimeException("Sunk");

utils's People

Contributors

kbreidenbach avatar

Watchers

 avatar  avatar

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.