GithubHelp home page GithubHelp logo

uptonking / jool-java-8 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 169 KB

add tuple, function, stream extensions to java 8 lambda. forked from jOOQ/jOOL

Home Page: https://github.com/jOOQ/jOOL

Java 100.00%
lambda java-streams

jool-java-8's Introduction

jOOλ

jOOλ is part of the jOOQ series (along with jOOQ, jOOX, jOOR, jOOU) providing some useful extensions to Java 8 lambdas. It contains these classes:

org.jooq.lambda.function

Why only Function and BiFunction? We have also included support for Function1 through Function16.

org.jooq.lambda.tuple

Tuple support is essential in functional programming. A variety of things can be modelled as tuples, e.g. function argument lists. This is why we support type safe Tuple1 through Tuple16 types.

org.jooq.lambda.Seq

The new Streams API was implemented to provide filter/map/reduce-like operations leveraging the new lambdas. Many of the useful methods that we know from other functional languages (e.g Scala) are missing. This is why jOOλ knows a Seq (short of Sequential) interface that extends Stream and adds a variety of additional methods to.

Please note that all Seq's are sequential and ordered streams, so don't bother to call parallel() on it, it will return the same Seq.

Seq adds a handful of useful methods, such as:

// (1, 2, 3, 4, 5, 6)
Seq.of(1, 2, 3).concat(Seq.of(4, 5, 6));

// true
Seq.of(1, 2, 3, 4).contains(2);

// true
Seq.of(1, 2, 3, 4).containsAll(2, 3);

// true
Seq.of(1, 2, 3, 4).containsAny(2, 5);

// (tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
Seq.of(1, 2).crossJoin(Seq.of("A", "B"));

// (tuple(1, 1), tuple(1, 2), tuple(2, 1), tuple(2, 2))
Seq.of(1, 2).crossSelfJoin()

// (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, ...)
Seq.of(1, 2, 3).cycle();

// tuple((1, 2, 3), (1, 2, 3))
Seq.of(1, 2, 3).duplicate();

// "!abc"
Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t);

// "abc!"
Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u);

// { 1 = (1, 3), 0 = (2, 4) }
Seq.of(1, 2, 3, 4).groupBy(i -> i % 2);

// (tuple(1, (1, 3)), tuple(0, (2, 4)))
Seq.of(1, 2, 3, 4).grouped(i -> i % 2);

// (tuple(1, 1), tuple(2, 2))
Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b);

// (tuple(1, 2), tuple(2, 1))
Seq.of(1, 2).innerSelfJoin((t, u) -> t != u)

// (1, 0, 2, 0, 3, 0, 4)
Seq.of(1, 2, 3, 4).intersperse(0);

// "123"
Seq.of(1, 2, 3).join();

// "1, 2, 3"
Seq.of(1, 2, 3).join(", ");

// "^1|2|3$"
Seq.of(1, 2, 3).join("|", "^", "$"); 

// (tuple(1, 1), tuple(2, 2), tuple(4, null))
Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b);

// (tuple(tuple(1, 0), NULL), tuple(tuple(2, 1), tuple(1, 0)))
Seq.of(tuple(1, 0), tuple(2, 1)).leftOuterSelfJoin((t, u) -> t.v2 == u.v1)

// (1, 2)
Seq.of(1, 2, 3, 4, 5).limitWhile(i -> i < 3);

// (1, 2)
Seq.of(1, 2, 3, 4, 5).limitUntil(i -> i == 3);

// (1, 2L)
Seq.of(new Object(), 1, "B", 2L).ofType(Number.class);

// (tuple(1, 1), tuple(2, 2), tuple(null, 3))
Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b);

// (tuple(NULL, tuple(1, 0)), tuple(tuple(1, 0), tuple(2, 1)))
Seq.of(tuple(1, 0), tuple(2, 1)).rightOuterSelfJoin((t, u) -> t.v1 == u.v2)

// tuple((1, 3), (2, 4))
Seq.of(1, 2, 3, 4).partition(i -> i % 2 != 0);

// (1, 3, 4)
Seq.of(1, 2, 3, 4).remove(2);

// (1, 4)
Seq.of(1, 2, 3, 4).removeAll(2, 3, 5);

// (2, 3)
Seq.of(1, 2, 3, 4).retainAll(2, 3, 5);

// (4, 3, 2, 1)
Seq.of(1, 2, 3, 4).reverse();

// (3, 1, 4, 5, 2) for example
Seq.of(1, 2, 3, 4, 5).shuffle();

// (3, 4, 5)
Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3);

// (3, 4, 5)
Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3);

// (2, 3)
Seq.of(1, 2, 3, 4, 5).slice(1, 3)

// tuple((1, 2), (3, 4, 5))
Seq.of(1, 2, 3, 4, 5).splitAt(2);

// tuple(1, (2, 3, 4, 5))
Seq.of(1, 2, 3, 4, 5).splitAtHead();

// tuple((1, 2, 3), (a, b, c))
Seq.unzip(Seq.of(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")));

// (tuple(1, "a"), tuple(2, "b"), tuple(3, "c"))
Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"));

// ("1:a", "2:b", "3:c")
Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y);

// (tuple("a", 0), tuple("b", 1), tuple("c", 2))
Seq.of("a", "b", "c").zipWithIndex();

org.jooq.lambda.Unchecked

Lambda expressions and checked exceptions are a major pain. Even before going live with Java 8, there are a lot of Stack Overflow questions related to the subject:

The Unchecked class can be used to wrap common @FunctionalInterfaces in equivalent ones that are allowed to throw checked exceptions. E.g. this painful beast:

Arrays.stream(dir.listFiles()).forEach(file -> {
    try {
        System.out.println(file.getCanonicalPath());
    }
    catch (IOException e) {
        throw new RuntimeException(e);
    }

    // Ouch, my fingers hurt! All this typing!
});

... will become this beauty:

Arrays.stream(dir.listFiles()).forEach(
    Unchecked.consumer(file -> { System.out.println(file.getCanonicalPath()); })
);

... or if you fancy method references:

Arrays.stream(dir.listFiles())
        .map(Unchecked.function(File::getCanonicalPath))
        .forEach(System.out::println);

Do note that Unchecked also allows you to throw checked exceptions explicitly without the compiler noticing:

// Compiler doesn't see that checked Exception is being thrown:
Unchecked.throwChecked(new Exception());

Download

For use with Java 9+

<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>jool</artifactId>
  <version>0.9.13</version>
</dependency>

For use with Java 8+

<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>jool-java-8</artifactId>
  <version>0.9.13</version>
</dependency>

jool-java-8's People

Contributors

uptonking avatar

Watchers

 avatar

Forkers

doytsujin

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.