GithubHelp home page GithubHelp logo

java-generator-functions's Introduction

java-generator-functions

An implementation of Python-like generator functions in Java. This repository contains a single class, Generator with a method yield(...) which can be used to mimic the behaviour of the yield keyword in Python.

Examples

The following is a simple generator that yields 1 and then 2:

Generator<Integer> simpleGenerator = new Generator<Integer>() {
    public void run() throws InterruptedException {
        yield(1);
        // Some logic here...
        yield(2);
    }
};
for (Integer element : simpleGenerator)
    System.out.println(element);
// Prints "1", then "2".

Infinite generators are also possible:

Generator<Integer> infiniteGenerator = new Generator<Integer>() {
    public void run() throws InterruptedException {
        while (true)
            yield(1);
    }
};

The Generator class lies in package io.herrmann.generator. So you need to import io.herrmann.generator.Generator; in order for the above examples to work.

Usage

This package is hosted as a Maven repository with the following url:

http://dl.bintray.com/filipmalczak/maven

To use it from Maven, add the following to your pom.xml:

<project>
    ...
    <repositories>
        ...
        <repository>
            <id>java-generator-functions</id>
            <url>http://dl.bintray.com/filipmalczak/maven</url>
        </repository>
    </repositories>
    ...
    <dependencies>
        <dependency>
            <groupId>io.herrmann</groupId>
            <artifactId>java-generator-functions</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

For Gradle:

compile(group: 'io.herrmann', name: 'java-generator-functions', version: '1.0')

Caveats and Performance

The Generator class internally works with a Thread to produce the items. It does ensure that no Threads stay around if the corresponding Generator is no longer used. However:

If too many Generators are created before the JVM gets a chance to garbage collect the old ones, you may encounter OutOfMemoryErrors. This problem most strongly presents itself on OS X where the maximum number of Threads is significantly lower than on other OSs (around 2000).

The performance is obviously not great but not too shabby either. On my machine with a dual core i5 CPU @ 2.67 GHz, 1000 items can be produced in < 0.03s.

Contributing

Contributions and pull requests are welcome. Please ensure that mvn test still passes and add any unit tests as you see fit. Please also follow the same coding conventions, in particular the line limit of 80 characters and the use of tabs instead of spaces.

java-generator-functions's People

Contributors

filipmalczak avatar leebickmtu avatar mherrmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-generator-functions's Issues

Does the generator only run once?

I'm building a variable tree structure that could have some branches thousands long, so instead of relying on recursion and hitting stack overflow, I've written a depth first search generator:

`private Generator<TreeNode[]> treeCrawler = new Generator<TreeNode[]>() {
public void run() throws InterruptedException {
ArrayList currentBranch = new ArrayList();
TreeNode currentNode;
TreeNode[] branch;

	currentBranch.add(_root.iter());
	while (currentBranch.size() > 0) {
		currentNode = currentBranch.get(currentBranch.size() - 1);
		if (currentNode.hasNext()) {
			currentNode = currentNode.next();
			currentBranch.add(currentNode.iter());
		} else {
			if (currentNode.isLeafNode()) {
				branch = currentBranch.toArray(new TreeNode[currentBranch.size()]);
				yield(branch);
			}
			currentBranch.remove(currentBranch.size() - 1);
		}
	}
}

};`

TreeNode is my internal node handler, iter() hasNext() and next() are internally managed to return each child node in succession.

It's designed to return the entire branch, from root to leaf, as an array. However it runs once, then never runs correct again.

Am I not structuring it right? It's stored as an instance variable of the public tree handler.

proposal

I really like your implementation of the generators.

Do you think it would be ok to wrap the element computed inside the generator in some kind of LazyElement so that this element could also encapsulate eventual exceptions that happened in the run function. And just like when call future.get in java you might return the execution exception when you would do LazyElement.get()

It would happen to be nice when piping generators together.

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.