cheptsov / AdvancedExpressionFolding

https://plugins.jetbrains.com/plugin/9320?pr=idea
141 stars 29 forks source link

Provide folding for basic collectors #99

Open lpld opened 6 years ago

lpld commented 6 years ago

.collect(Collectors.toList()) -> .toList .collect(Collectors.toSet()) -> .toSet .collect(Collectors.joining(sep)) -> .mkString(sep) .collect(Collectors.groupingBy(a -> b)) -> .groupBy(a -> b) .collect(Collectors.toMap(t -> key, t -> value)) -> .toMap(t -> key, t -> value)

What do you think?

jfcabral commented 6 years ago

I think its a fantastic idea, but it seems that a couple of them are already implemented 😄

:white_check_mark: Already Implemented (tested on v0.9.9.4)

.collect(Collectors.toList()) -> .toList .collect(Collectors.toSet()) -> .toSet .collect(Collectors.toMap(t -> key, t -> value)) -> .toMap(t -> key, t -> value)

As for the remaining 2

.collect(Collectors.joining(sep)) -> .mkString(sep) .collect(Collectors.groupingBy(a -> b)) -> .groupBy(a -> b)

I think it would be interesting to add support for these. But in my opinion, I think the folded syntax should not be changed, keeping its consistency with the real collector operands.

So what do you think of this instead:

.collect(Collectors.joining(sep)) -> .joining(sep) .collect(Collectors.groupingBy(a -> b)) -> .groupingBy(a -> b)

lpld commented 6 years ago

Indeed. I think I understand why I didn't realize that it's already there. It seems that folding works for .collect(Collectors.toList()), but not for .collect(toList()) when toList is statically imported. Is it an expected behavior?

As for the syntax for joining and groupingBy, I agree, no need to introduce new names. BTW, there are few more collector methods that can be folded: reducing, counting, summarizing*, averaging*. Probably some of them are already implemented? (Sorry, can't check right now).