Giter VIP home page Giter VIP logo

gen2it's Introduction

Generator to Iterator Converter

This tool compiles generator methods to Java-style iterators.

Generator methods are a very convenient way to write code that produces streams of values: they are normal functions that periodically yield values to the caller. In many cases it is easier to express a stream of values as a generator than to manually write an implementation of Iterator.

For example, a generator method to produce Fibonacci numbers might look like this:

int a = 1;
int b = 1;
while (true) {
    yield(a);     // produce a value
    int prev_a = a;
    a = b;
    b += prev_a;
}

Using the tool

I expect you to be running Python 3. Install dependencies with pip:

$ pip3 install -r requirements.txt

Invoke the tool by running:

$ python3 -m gen2it INPUT_FILE -o OUTPUT_FILE

The input should be a Java source file with a single class having a TypeName generate(args...) method. That method may call yield(TypeName value) to yield values. The output is a Java source file with boolean hasNext() and TypeName next() methods.

Sample inputs for the tool can be found in the examples folder.

Usage Notes

  • The generated iterators are not threadsafe. You will need to manually synchronize calls to any method on the generated iterator classes.

  • This style of programming can be faked in pure Java using threads, however, this tool will avoid the overhead of the extra threads and the risks of leaking memory through them.

  • Many features are not yet implemented. Feel free to file issues or open pull requests to fix missing functionality. A few things that may not work properly yet:

    • try-catch and try-with-resources
    • for-loops
    • using a for-each loop over an array

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.