tag:blogger.com,1999:blog-20975090.post3154746188112694240..comments2024-03-11T10:18:55.852-05:00Comments on Headius: Converting Groovy to RubyCharles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-20975090.post-63815117052319747512008-05-04T08:04:00.000-05:002008-05-04T08:04:00.000-05:00And a python version here:def subn(n, lst): if ...And a python version here:<BR/><BR/>def subn(n, lst):<BR/> if n > len(lst) or n < 0:<BR/> return []<BR/> elif n == 0:<BR/> return [[]]<BR/> else:<BR/> return ( subn(n, lst[1:]) +<BR/> [ (lst[:1] + rest) for rest in subn(n-1, lst[1:]) ] )Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-20975090.post-39105215617788534772008-04-19T21:08:00.000-05:002008-04-19T21:08:00.000-05:00Groovy:def subn(n, list){!n? [[]]: !list? []:(list...Groovy:<BR/>def subn(n, list){<BR/>!n? [[]]: !list? []:<BR/>(list.size() == 1? []: list[1..-1]).with{<BR/>subn(n-1, it).collect{ [list[0]] + it } + subn(n, it)<BR/>}<BR/>}<BR/><BR/>assert subn(2, ['a','b','c']) ==<BR/>[["a", "b"], ["a", "c"], ["b", "c"]]Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-20975090.post-47130199470293875322008-04-18T00:47:00.000-05:002008-04-18T00:47:00.000-05:00In Mark's improved example, you can also even remo...In Mark's improved example, you can also even remove the last return keyword, to make it more idiomatic Groovy, and to save some more characters that the Ruby version.<BR/><BR/>But yes, both Groovy and Ruby are quite similar for that kind of algorithms: closures, colllect method taking a closure, list subscript operator, etc.<BR/><BR/>That's an interesting parallel to make here, and I wonder why you thought this article would be flamed? (hence why the useless sentences like "I'll be flamed by you-know-who"?)Guillaume Laforgehttps://www.blogger.com/profile/03993980328127368671noreply@blogger.comtag:blogger.com,1999:blog-20975090.post-22435068404813849662008-04-17T20:53:00.000-05:002008-04-17T20:53:00.000-05:00Not golfing, just a different style - getting rid ...Not golfing, just a different style - getting rid of returns altogether and using splat instead of indexing:<BR/><BR/>def subn(n, list)<BR/> if n == 0<BR/> [[]]<BR/> elsif list.empty?<BR/> []<BR/> else<BR/> head, *tail = list<BR/> subn(n-1, tail).map {|it| [head, *it] } + subn(n, tail)<BR/> end<BR/>end<BR/><BR/><BR/>Regards,<BR/>Sean<BR/><BR/>(Shame about the formatting)seanohttps://www.blogger.com/profile/10589056607741161718noreply@blogger.comtag:blogger.com,1999:blog-20975090.post-56264252034124323232008-04-17T19:35:00.000-05:002008-04-17T19:35:00.000-05:00Just a quick followup on my site:http://www.glenst...Just a quick followup on my site:<BR/><BR/>http://www.glenstampoultzis.net/blog/?p=62chewyhttps://www.blogger.com/profile/00587518900311780550noreply@blogger.comtag:blogger.com,1999:blog-20975090.post-90024825971478155242008-04-17T19:26:00.000-05:002008-04-17T19:26:00.000-05:00The biggest difference is that from beginning to e...The biggest difference is that from beginning to end as he is changing it from Java to Groovy the code continues to execute correctly so it much easier to do those incremental changes.Samhttps://www.blogger.com/profile/14121705654622595662noreply@blogger.comtag:blogger.com,1999:blog-20975090.post-31776779323937102052008-04-17T19:22:00.000-05:002008-04-17T19:22:00.000-05:00Hmm, that makes them exactly the same number of ch...Hmm, that makes them exactly the same number of characters, Groovy's two extra "()" pairs on the if statements traded for Ruby's definition of "|it|".Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-20975090.post-85962008239714683842008-04-17T19:18:00.000-05:002008-04-17T19:18:00.000-05:00Seems to me that the Groovy version can be reduced...Seems to me that the Groovy version can be reduced further:<BR/><BR/>def subn(n, list) {<BR/> if (!n) return [[]]<BR/> if (!list) return []<BR/><BR/> def remainder = list[1..-1]<BR/> return subn(n-1, remainder).collect() { [list[0] + it } + subn(n, remainder);<BR/>}Anonymousnoreply@blogger.com