-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take advantage of Java 8 language features #5
base: master
Are you sure you want to change the base?
Conversation
Also use a lambda for a Runnable instead of an anonymous inner class.
This allows it to be used by functions that expect suppliers, for example, with Stream.generate(). If the Supplier reaches the end of the underlying Iterator, a NoSuchElementException is thrown.
Everything works yet, except... You can't use a generator multiple times, which would be fine, except that Java caches "stateless" lambdas (even if the functional interface is using a hack to give it state, but Java can't know that).
This is the best solution I see to the problem with Java caching lambdas it thinks are stateless.
It follows naturally from the last commit, and really only took a few lines of code.
It had become redundant, as all it really did was hold an iterator. Now GeneratorIterator takes its place.
The class no longer implements Supplier; that was a silly way of doing things. It turns out that you can make a stream from an iterator easily. Generator is now really stateless.
Thank you very much for the PR. It looks very interesting. Your commits are also very well encapsulated and easy to follow. I would like to incorporate your changes, but there are a couple of points I would like to discuss first:
I haven't had time to look at all your changes in detail but these are the most important points I could see. Cheers! |
|
Generator is now split into the backwards-compatible, stateful, abstract class Generator and the newer functional interface GeneratorIterator.
Syntax highlighting is nice...
This became useful again because Generator is now stateful (which was required for the yield() method to work in a backward-compatible way).
I think I've done pretty much everything you suggested (unless you want more than one README example using the old syntax). In the course of fixing your third point, I realized I basically did the same thing as your proposed solution for the fourth point, except that |
Actually, I noticed after my last comment that you mention that the Actually, I just thought of an even better way of handling that class. |
I just added another method for syntactic sugar. Are you still interested in merging this PR? I think the only question is whether or not you like the fact that all Generators are reusable (unlike in Python). There's no public |
Hi Brian, sorry I've been so out of touch. The main reason is that I am busy working on other projects, and Your changes look fine, but they are basically a complete rewrite of the library. By merging your changes, I would take on responsibility of code that I mostly didn't write. Given that I'm already out of touch with the project, that's something I would rather not do. Having said that, I do think there is a very viable way forward: You could publish your code as a separate project, saying that it is "generator functions for Java 8+". And I could put a note at the top of "my" README saying that users who want Java 8 language features should look at your project. I think this would also be a very good solution to my point 1. above. Sorry if I caused you unnecessary work. I hope it won't be in vain if you continue it as a separate project. M |
I just took the time to enhance the project with Java 8 language features. The Generator syntax is more concise now, and it can be used to create Java 8 (parallel) streams. Also, Generators are now stateless by default, so you can iterate over a generator multiple times (unless it performs some mutation). The following is copied and pasted from my fork of your README.
Examples
The following is a simple generator that yields
1
and then2
:Infinite generators are also possible:
You can even use a generator to create a (parallel)
Stream
: