Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

SamirTalwar/Streams

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Streams for Java

Functional programming in Java shouldn't be so hard. Streams is an attempt to make it a little easier on us all by providing Haskell-style functionality in an object-oriented format. A Stream is a simple object that wraps an iterator (or an iterable) and allows you to (mis)treat it as you please. Everything is lazily evaluated, so nothing happens until you ask for the results.

Personally, I find this sort of thing is much better explained with an example:

Iterator<Integer> numbers = theIteratorOfAMassivelyLongList();
Stream<Integer> numberStream = Stream.wrap(numbers);
Stream<String> htmlStream = numberStream.map(add(3))
                                        .filter(isEven())
                                        .concat(defaults())
                                        .zipWith(definitions(), formatAsHtml());

for (String item : htmlStream.take(5)) {
    // This will loop through only the first five values of that horrendously complicated endeavour.
    // In fact, it will only evaluate the first five values.
    // No wasted effort.
}

All you need to do is define those functions. Streams backs onto Google's Guava when it can to allow you to reuse code wherever possible.

private Function<Integer, Integer> add(final int n) {
    return new Function<Integer, Integer>() {
        public Integer apply(Integer input) {
            return input + n;
        }
    };
}

Using Streams

All you have to do is download the JAR, grab Guava 11.0.1 and add them to your classpath.

Building Streams

Streams is built through [Maven][]. Just fire up a command line and type:

mvn package

This will run all the tests and create a JAR in the target directory. Please run this command and ensure it succeeds before pushing up to your public repository as it will make sure anyone downloading it can build it.

About

Streams are a way of creating useful iterables in Java, similar to C#'s Linq to Objects, Python's generators or Haskell's lists.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages