grails - How to convert Java code to Groovy code automatically -


I have written some code in Java and I need to change code to give Groovy the advantage, whether Java code Is there any way to automate or change some existing plugins that can start me in this direction?

I would like to present it with an example program:

  Personal Fixed list & lt; List & gt; Subn (int n, list li) {list & lt; List & gt; Rate = New Arrestist & lt; List & gt; (); If (n == 0) {ret.add (new arrelist ()); Return writ; } If (li.isEmpty ()) {Return Return; } T x = Lee.Gate (0); List xs = li.subList (1, li. Size ()); (List sub: subnot (N-1, xs)) for {sub.add (0, x); Ret.add (sub); } Ret.addAll (subn (n, xs)); Return writ; }  

For the first step I would like to delete static typing, although I do not execute it, but for such a small code, I will go for it.

This letter count will change :) But there is no structure now we change the array:

  Def subn (n, li) {def ret = []; If (n == 0) {ret.add ([]); Return writ; } If (li.isEmpty ()) {Return Return; } Def x = li.get (0); Def xs = li.subList (1, li size ()); (For subs in subn (n-1, xs)) {sub.add (0, x); Ret.add (sub); } Ret.addAll (subn (n, xs)); Return writ; }  

We see more characters sorting, now if the statement changes.

  Diff Upon (n, li) {if (n == 0) returns [[]]; If (li.isEmpty ()) back []; Def ret = []; Def x = li.get (0); Def xs = li.subList (1, li size ()); (For subs in subn (n-1, xs)) {sub.add (0, x); Ret.add (sub); } Ret.addAll (subn (n, xs)); Return writ; }  

I'm not really a fan of short variable names, so in the next step some variables have to be renamed. Apart from this, I'll change the loop slightly to use every function. 'This' magic variable changes the sub-variable.

  def subn (n, list) {if (n == 0) back [[]]; If (list.isEmpty ()) back []; Def ret = []; Def head = list.get (0); Def balance = list. SubList (1, list.size ()); Subn (n-1, remaining) .each {it.add (0, head); Ret.add (this); } Ret.addAll (subn (n, left)); Return writ; }  

We can make the loop a bit more by adding an archive together:

  def subn (n , List) {if (n == 0) return [[]]; If (list.isEmpty ()) back []; Def ret = []; Def head = list.get (0); Def balance = list. SubList (1, list.size ()); Subn (n-1, the rest) .each {ret.add ([head] + this); } Ret.addAll (subn (n, left)); Return writ; }  

Seeing the code makes it clear that the 'ret' variable is just gathering results. Instead we can use the collected method for this purpose.

  def subn (n, list) {if (n == 0) return []; If (list.isEmpty ()) back []; Def head = list.get (0); Def balance = list. SubList (1, list.size ()); Def ret = subn (n-1, left) .collect {[head] + it} ret.addAll (subn (n, the rest)); Return writ; } Actually we do not really need 'rate' if we instead add two collections together:  
  def subn (n, list) {if (n == 0) return [[]]; If (list.isEmpty ()) back []; Def head = list.get (0); Def balance = list. SubList (1, list.size ()); Return subn-nn (n-1, remaining) .collect {[head] + this} + upon (n, left); }  

Finally returns the head variable inline. It is used only in one place, so it is not really worth it. Apart from this, we will use Groovy List Indexing to remove get () calls.

  def subn (n, list) {if (n == 0) returns [[]]; If (list.isEmpty ()) back []; Def balance = list. SubList (1, list.size ()); Return subn (n-1, remaining) .collect {[list [0]] + this} + Upon (n, left); }  

Comments