RosettaCodeData/Task/Ordered-words/Scala/ordered-words.scala
Ingy döt Net 6f050a029e update
2013-06-05 21:47:54 +00:00

20 lines
617 B
Scala

val wordsAll = scala.io.Source.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.toSeq
/**
* Given a sequence of words return a sub-sequence of the
* words that have characters in sorted order.
*/
def orderedWords( words:Seq[String] ) : Seq[(String)] = {
def isOrdered( s:String ) : Boolean =
(s.foldLeft( (true,'@') ){
case ((false,_),_) => return false
case ((true,prev),c) => ((prev <= c),c)
})._1
wordsAll.filter( isOrdered(_) ).toSeq
}
val ww = orderedWords( wordsAll ).sortBy( -_.length )
println( ww.takeWhile( _.length == ww.head.length ).mkString("\n") )