Re:I don't like Python. Does that make me a bad person?
by wycats on Saturday June 26, 2010 @09:21PM
(#1464658)
An easier Ruby solution would be:
("letters in this sentence" + "and this one").chars.sort.reverse
Re:I don't like Python. Does that make me a bad person?
by sreque on Saturday June 26, 2010 @10:10PM
(#1464703)
Except you aren't using sets, so you get duplicate characters.
I can just as easily write ("letters in this sentence" + "and this one").toSeq.sorted.reverse in scala. You could tag the end of your ruby statement with .uniq and you would be fine. Alternatively, you could wrap the above scala statement in TreeSet(). These solutions process things in a significantly different way than was done by the OP, though.
(Set("letters in this sentence":_* ) & Set("and this one":_* )).toSeq.sorted.reverse
Or you, can take advantage of Scala's rich choice of collections.
import scala.collection.immutable.TreeSet
import scala.math.Ordering._
TreeSet("letters in this sentence":_* )(Char.reverse) & Set("and this one":_* ) toSeq
The machinery Scala has in place to make the second example work is quite impressive, and I was pleased to see that it worked. Even though the intersection method returns a brand new set, using implicit arguments it correctly creates a set of the correct type (TreeSet) with the correct ordering function (Char.reverse), without any duplication of code in the standard library (like overriding '&' in every sub-class).