Giter Site home page Giter Site logo

streamx's Introduction

streamx

More or less useful extensions to java.util.stream API mainly for easing consumption of streams backed by IO resources that must be closed.

Example:

// using auto-closing Stream wrapper...

AC.stream(Files.walk(Paths.get("/usr/share/doc")))     // walk paths in directory - close after terminal op.
    .filter(p -> p.toFile().isFile())                  // just take normal files
    .filter(p -> p.toString().endsWith(".txt"))        // and text files among them
    .flatMap(IO.function(                              // wrap IOException with UncheckedIOException
        p -> concat(Stream.of("#", "# " + p, "#"),     // concatenate path name header with
                    Files.lines(p))                    // expanded lines
    ))
    .forEach(System.out::println);                     // print them out (will also close the stream)

// using Streamable functional interface as a factory for streams

Streamable.IO<Path> docs = () -> Files.walk(Paths.get("/usr/share/doc")); // factory of path walk streams

Streamable<String> lines = docs                        // factory of lines streams
    .filter(p -> p.toFile().isFile())                  // just take normal files
    .filter(p -> p.toString().endsWith(".txt"))        // and text files among them
    .flatMap(IO.function(                              // wrap IOException with UncheckedIOException
        p -> concat(Stream.of("#", "# " + p, "#"),     // concatenate path name header with
                    Files.lines(p))                    // expanded lines
    ));

// ... invoke the factory and consume the stream...

lines
    .autoClosingStream()                               // request an auto-closing stream
    .forEach(System.out::println);                     // print them out

// ...or...

try (Stream<String> linesStream = lines.stream()) {    // use try-with-resources on normal stream
    linesStream.forEach(System.out::println);          // print them out
}


// or almost entirely without this extra stuff...

Function<Stream<Path>, Stream<Path>> filterTextFiles = ps ->
    ps.filter(p -> p.toFile().isFile())                // just take normal files
      .filter(p -> p.toString().endsWith(".txt"));     // and text files among them

Function<Stream<Path>, Stream<String>> dumpLines = ps ->
    ps.flatMap(
        IO.function(                                   // wrap IOException with UncheckedIOException
            p -> concat(Stream.of("#", "# " + p, "#"), // concatenate path name header with
                        Files.lines(p))                // expanded lines
        )
    );

Consumer<Stream<String>> printLines = ls -> ls.forEach(System.out::println);

// ... apply this lazy transformations...

printLines.andThen(Stream::close)
    .accept(
        filterTextFiles.andThen(dumpLines)
            .apply(
                Files.walk(Paths.get("/usr/share/doc"))
            )
    );

streamx's People

Contributors

plevart avatar

Stargazers

袁世超 avatar 小知了 avatar Simeon Malchev avatar Dmytro avatar Igor Tavčar avatar Sam Pullara avatar Hiroshi Yamamoto avatar

Watchers

 avatar Igor Tavčar 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.