Semordnilap: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
(Updated for compatibility with Scala 3)
Line 3,112: Line 3,112:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>val wordsAll =
<lang scala>val wordsAll = scala.io.Source.fromURL("http://www.puzzlers.org/pub/wordlists/unixdict.txt").getLines.map(_.toLowerCase).to[IndexedSeq]
scala.io.Source.
fromURL("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt").
getLines().map(_.toLowerCase).toIndexedSeq



/**
/**
Line 3,119: Line 3,123:
* words are different.
* words are different.
*/
*/
def semordnilap( words:Seq[String] ) : Seq[(String,String)] = {
def semordnilap( words:IndexedSeq[String] ) : IndexedSeq[(String,String)] = {


( words.
words.
zipWithIndex. // index will be needed to eliminate duplicate
zipWithIndex. // index will be needed to eliminate duplicate
filter {
filter {
case (w,i) =>
case (w,i) =>
val j = words.indexOf(w.reverse) // eg. (able,62) and (elba,7519)
val j = words.indexOf(w.reverse) // eg. (able,62) and (elba,7519)
i < j && w != w.reverse // save the matches which are not palindromes
i < j && w != w.reverse // save the matches which are not palindromes
}.
map {
case (w,_) => (w,w.reverse) // drop the index
}
}
).
map {
case (w,i) => (w,w.reverse) // drop the index
}
}
}


Line 3,137: Line 3,140:


{
{
println( ss.size + " matches, including: \n" )
println( s"${ss.size} matches, including: \n" )
println( ss.take(5).mkString( "\n" ) )
println( ss.take(5).mkString( "\n" ) )
}</lang>
}</lang>