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
FluentIterable #11
Comments
Original comment posted by kevinb9n on 2007-10-22 at 05:47 PM Thanks! Also see these blog posts (and my comments on them) http://blogs.warwick.ac.uk/chrismay/entry/writing_functional_java In short, the "FP" features of the library could definitely be made a lot better, Owner: kevinb9n |
Original comment posted by jonhnnyweslley on 2007-10-23 at 06:50 PM Hi Kevin, I read your comments. But I dont liked a little of the one in url: I think that Function and Predicate must to continue as interfaces. |
Original comment posted by kevinb9n on 2007-10-23 at 06:58 PM Of course there must be interfaces. In my experience it's very rare that people wouldn't extend the abstract class. But The interesting question is: if usage of the abstract class proves to be 10, 20, 50 Heresy, I know, but I get paid to consider heresy :) What I really fear is making the line Predicate<Set<? extends Number>> containsFive = new Predicate<Set<? extends yet another eight characters longer, due to the need to instantiate AbstractPredicate |
Original comment posted by ymeymann on 2007-11-04 at 04:14 PM Hi Kevin, While using google-collections in my project, I created FunctionChain class that may Usage example: I tried to address 2 things: (a) keep Function as interface (b) get rid of NPE's by What do you think? -- Yardena. |
Original comment posted by kevinb9n on 2009-03-17 at 04:49 PM We have been experimenting internally with "fluent" versions of common types. The notion here is to follow the lead of the pair: java.util.Comparator (interface) The latter implements the former, has a from() method taking the former, and is in This seems like a pattern that, while not totally 100% awesome, is worth following So what we're playing with: Comparator --> Ordering For example, FluentIterable looks like this: abstract class FluentIterable<T> implements Iterable<T> { int size() ImmutableList<T> toList() T getOnlyElement() int frequency(@Nullable Object) T firstMatching(Predicate<? super T>) Any input? |
Original comment posted by jim.andreou on 2009-03-17 at 06:14 PM Years ago I had experimented with returning "ExtendedIterable" (and other such types)
In the case of Ordering, I see comparator as a SPI and Ordering as its respective So, I would hope you continue to prefer simplicity over slight syntactical gains, as I also want to draw some attention to the fact that this conversation wouldn't need |
Original comment posted by kevinb9n on 2009-03-17 at 09:40 PM You certainly are right that I'd hate to see this proliferate to Set and Map and all If Ordering/Comparator is a little different from the rest (API/SPI), then Function So it's really FluentIterable that is most controversial here. Well -- I love |
Original comment posted by earwin on 2009-03-17 at 10:22 PM I second the vote for having interfaces that define all these new fluent methods. So it goes like: Then, if you're introducing upgraded interface for Iterables/Comparators and From that point I'm no longer sure which names should be shorter. What I'm not What about naming upgraded Iterable a Sequence? Predicate is a cool name, it goes better with Function and it doesn't clash with Hopefully, I was able to convey my thoughts :) |
Original comment posted by kevinb9n on 2009-03-17 at 11:00 PM I still don't see the advantage of an interface for Ordering. Can you try again to Whatever the "better" Iterable is, I'm convinced it must include "Iterable". Picking By this rationale the name Ordering was a mistake, and maybe it was, but it's one we |
Original comment posted by earwin on 2009-03-19 at 07:18 PM Similar to your case - an ability to use Ordering enums. It's just a joke, but what about taking your words literally and using BetterIterable? |
Original comment posted by kevinb9n on 2009-03-19 at 07:40 PM But an Ordering enum would be such an incredible burden to implement! Don't bother; As well, remember than any interface we release is something we can never, ever add a "BetterIterable"? Believe me, we considered it. We tried everything. |
Original comment posted by jim.andreou on 2009-03-19 at 08:48 PM Maybe you should put less weight to API discoverability, and trust your users to know (A question, in order to understand the precise dilemma: if you introduce |
Original comment posted by kevinb9n on 2009-03-19 at 08:51 PM It's too late to have any other choice but to keep them redundantly. |
Original comment posted by kevinb9n on 2009-09-17 at 06:02 PM (No comment entered for this change.) Labels: |
Original comment posted by jonhnnyweslley on 2009-11-16 at 12:59 AM Take a look at ParallelArray: "The ParallelArray library builds on top of fork-join http://gee.cs.oswego.edu/dl/jsr166/dist/extra166ydocs/extra166y/ParallelArray.html |
Original comment posted by kevinb@google.com on 2010-07-30 at 03:53 AM (No comment entered for this change.) Labels: - |
Original comment posted by kevinb@google.com on 2010-07-30 at 03:56 AM (No comment entered for this change.) Labels: - |
Original comment posted by kevinb@google.com on 2011-01-12 at 08:33 PM Issue #520 has been merged into this issue. |
Original comment posted by nvollmar on 2011-01-13 at 08:15 AM Do you have any plans to release your internally used FluentIterable in the near future? |
Original comment posted by kevinb@google.com on 2011-01-13 at 02:57 PM It is not a high priority, no. Status: |
Original comment posted by nvollmar on 2011-01-19 at 01:04 PM Just for those interested, I've extended my Queryable with fold and orderBy/thenBy. It's as lazy as possible. So I think Queryable isn't a bad name for it, since its more than just fluent. |
Original comment posted by kevinb@google.com on 2011-07-13 at 06:18 PM (No comment entered for this change.) Status: |
Original comment posted by tomas.zalusky on 2011-07-19 at 07:26 AM Hi, at first thanks for Guava, it helps to increase productivity a lot! My question: please could you describe current state of adding fluent versions of collection-related classes to Guava? I carefully watch these issues and am interested of current situation. I rate this feature very important. If the only problem is naming, wouldn't be better to ultimately decide somehow? (Personally I prefer concise one-word names like Sequence, Filter/Matcher or Transformer but I absolutely accept anything else - rather unfavorite name than nothing.) Thanks! Historical comments: #11 #334 #11 |
Original comment posted by kevinb@google.com on 2011-07-19 at 02:23 PM Ah, it is about time for an update. I mentioned that we've been "experimenting" with fluent Iterables, Functions and Predicates inside Google. Most of us feel that the experiment has been a success, and we will endeavor to get these into Guava release 11 or 12 for you. We will call them simply FluentIterable, FluentFunction and FluentPredicate. Status: |
Original comment posted by wasserman.louis on 2011-12-09 at 07:08 PM (No comment entered for this change.) |
Original comment posted by wasserman.louis on 2011-12-09 at 07:08 PM Issue #818 has been merged into this issue. |
Original comment posted by fry@google.com on 2011-12-10 at 03:12 PM (No comment entered for this change.) Labels: |
Original comment posted by wasserman.louis on 2012-01-03 at 07:38 PM This is super exciting and yay. |
Original comment posted by wasserman.louis on 2012-01-03 at 08:15 PM Recommendation: having transform() or map() methods on FluentFunction would allow it to be overloaded for list, collection, iterable, optional, etc., as opposed to adding those methods to FluentIterable alone. |
Original comment posted by wasserman.louis on 2012-02-16 at 06:50 PM We're splitting this up into separate issues; FluentPredicate is now issue 334. |
Original comment posted by em...@soldal.org on 2012-02-17 at 12:08 AM Would it be possible to weave functions into a monadic structure using optional? |
Original comment posted by wasserman.louis on 2012-02-17 at 01:00 AM ...Speaking as a Haskell aficionado who thinks monads are awesome... No. Java does not have the syntax to make it even remotely pleasant...and I'm not sure that even Java 8 will make monads pleasant in Java. |
Original comment posted by kevinb@google.com on 2012-03-02 at 08:29 PM Trying for release 12 with this, but not certain. Here is our API for your consideration: First, you get a FluentIterable using FluentIterable.from(anyIterable) or FluentIterable.of(T...). There are simple queries:
There are the "element extraction" methods:
Then the fun stuff: chaining-style methods which all return FluentIterable:
And lastly when you're done with all the chaining stuff, you might want to dump the contents into something else:
Oh, one other thing: it will not have special equals or hashCode behavior, and we are not sure what to do about toString(). Generating AbstractCollection-style output seems useful, but we probably want to cap that at a certain ceiling, against the risk of FluentIterable.from(Ranges.all().asSet(integers())), etc. What do you think so far, users? Labels: |
Original comment posted by cgdecker on 2012-03-02 at 09:26 PM That looks good and is more or less what I'd expect. A few thoughts:
This is slightly confusing to me in this context. Does it return absent if the iterable is empty, a value if it has one element, and throw an exception if it has more?
Maybe it's just me, but I generally think of concat as a standalone operation that takes its inputs and concatenates them (as with Iterables.concat), not something that one object does to add another to it. Append makes sense for that to me; StringBuilder etc. already use it and I think it accurately describes what you're doing. "Append the input iterable to this one to produce a new iterable." Replace "append" with "concatenate" there and it doesn't sound quite right to me.
Would it make sense to add a method like this?
That would allow users to easily dump the contents into a mutable collection if desired. On the other hand, maybe it'd be better not to encourage that? |
Original comment posted by kevinb@google.com on 2012-03-02 at 10:02 PM We considered copyInto(C) as the direct analog of Iterables.addAll(). It's not ruled out, but we noticed that within Google, SetView.immutableCopy() has 6x as many usages as SetView.copyInto(C). And our internal FluentIterable has become pretty popular and no one's ever asked for it yet. |
Original comment posted by em...@soldal.org on 2012-03-02 at 11:07 PM Just a quick question, it produces new object for each method call right |
Original comment posted by brice.dutheil on 2012-03-03 at 12:51 AM Hi Kevin, What do you mean by that ?
It should at least have the be the same behavior as a list or set of the current FluentIterable. Also, what do you think of using the 'map()' name intead of 'transform()'. I think naming this properly is better in the long run, just as we like to indicate patern names in the code. Having these known higher order function naming might actually help users see / understand what functional programming means. Thoughts ? |
Original comment posted by wasserman.louis on 2012-03-03 at 02:22 AM
The hashCode and equals behavior of lists and sets is different. FluentIterable really shouldn't have any special equals or hashcode. I have mixed feelings about map vs. transform. I like that the name is the same for both Iterables and FluentIterable. Fold or reduce doesn't make sense unless we either introduce a Pair type (which is in the Idea Graveyard and should never happen), or we introduce a binary function type, which I don't think we're ready to do. |
Original comment posted by wasserman.louis on 2012-03-03 at 02:22 AM (See issue 218 on fold/reduce.) |
Original comment posted by brice.dutheil on 2012-03-03 at 01:17 PM
I'm sorry I didn't express what I wanted to say correctly, I wanted to say the current FluentIterable could use the same behavior as the underlying iterable being a List or a Set. Anyway I'd like to rollback my after-midnight thought, because Iterable can have many different implementations that are not set nor list. I agree with you : no special behavior for equals and hashcode.
Actually without the fold operator. I agree this doesn't make sense to use the map name instead of transform. Though it could be aliases. I don't think Pair was a good option anyway to implement folding, it feels unnatural. However I would rather see a binary function type, and one of them being an Accumulator type. |
Original comment posted by kevinb@google.com on 2012-03-03 at 02:51 PM Emily: yes. |
Original comment posted by j...@nwsnet.de on 2012-03-05 at 09:04 AM
Why is that a bad way? And what's the alternative? Both returning |
Original comment posted by kurt.kluever on 2012-03-05 at 07:42 PM (No comment entered for this change.) Owner: kak@google.com |
Original comment posted by MrChrisPSimmons on 2012-03-12 at 09:57 AM We've implemented something similar to this internally and I've got a use case I'd like to see considered that isn't mentioned above. A means to composite Iterables, similar to Iterables: <T> Iterable<T> concat(Iterable<? extends Iterable<? extends T>> inputs) Note that you've got an iterable of iterables here, which naturally maps onto something like this:- FluentIterable<Out> concat(Function<? super T, Iterable<Out>> function) If T extends Iterable then the function can just be the identity. This is much more powerful than append(Iterable) - roughly 3:1 ratio of usage in our code base of a concat-like operation versus append. |
Original comment posted by kak@google.com on 2012-03-13 at 08:50 PM Just hit head! ec452d2 @MrChrisPSimmons + others: Can you please file a separate issue for your requests? Thanks :-) Status: |
Original comment posted by cgdecker on 2012-03-13 at 10:13 PM Awesome! I added a review for the commit with a couple questions. |
Original issue created by jonhnnyweslley on 2007-10-22 at 05:12 PM
Hi,
Last weekend I thought:
"Why not to use functional programming with Java? More precisely using
Google Collections!"
...
Minutes later... Voila!!!
I can to write pieces of code as:
names.select(new Regexp(".*e$")).transform(new GetLength()).select(new
OddNumber())
I wanted to move this code to a fluent interface. Now, I dont need the
verbose syntax of the static methods, and, mainly, this code is more easy
to read.
The attached jar file contains the full source code with:
About Fluente Interface:
http://martinfowler.com/bliki/FluentInterface.html
http://en.wikipedia.org/wiki/Fluent_interface
The text was updated successfully, but these errors were encountered: