Samstag, 27. Februar 2010

Syntax for currying in Scala


val numbers = List.range(1,10); println(numbers.foldLeft(0)(_ +_ ))


or


val numbers = List.range(1,10); println((numbers foldLeft 0)(_ +_ ))

Iterating through command line arguments in Scala


for {
i <- args.indices.drop(1)
if args(i) startsWith prefix
} printf("args(%d): %s\n", i, prefix)


or


args.zipWithIndex
.drop(1)
.filter {case (s, i) => s startsWith prefix}
.foreach(arg => println("args("+arg._2+"): "+arg._1))


or


for {
(s, i) <- args.zipWithIndex.drop(1)
if s startsWith prefix
} println("args("+i+"): "+s)

Using case classes to filter matching cases in Scala


abstract class A0

case class A() extends A0

case class B() extends A0

case class C() extends A0

val list = List( A(), A(), B(), C(), C(), C() )

val len1 = list filter ( _ match {case A() => true; case _ => false} ) length

val len2 = list filter ( _ match {case _:A => true; case _ => false} ) length

val len3 = list filter ( x => x match {case A() => true; case _ => false} ) length

printf("=> len1: %d, len2: %d, len3: %d \n",len1,len2,len3)

Montag, 22. Februar 2010

How to read from a BufferedInputStream in Scala


def bufferedInput(fileName: String) = new BufferedInputStream(new FileInputStream(fileName));

def bytesAsInts(fileName:String): Iterator[Int] = {
val in = bufferedInput(fileName)
Iterator continually in.read() takeWhile (_ != -1)
}

val file = bytesAsInts("test.txt")

file foreach(_=>printf("%c",_:Int))

// AND

file foreach(_=>printf(x=>"%c",x))


// def isNull[A] = (_: A) == null

Samstag, 20. Februar 2010

How to randomly shuffle a Sequence or List in Scala



val names = List("1","2","3","4","5")

List.fill(names.size)(scala.util.Random.nextInt).zipWithIndex.sortBy(identity).map(_._2).map(names(_))




or


val names = List("1","2","3","4","5","6","7")

Sorting.stableSort(names, (a:String, b:String) => Random.nextInt(2) == 1)
res11: Array[java.lang.String] = Array(2, 7, 4, 5, 1, 6, 3)

How to transform a List into a java.util.List



implicit def toJavaList[A](i: scala.Iterable[A]): java.util.List[A] = {
val result = new java.util.ArrayList[A]
i.foreach(result.add)
java.util.Collections.unmodifiableList[A](result)
}


Or, if you don't need a immutable List:


implicit def toJavaMutableList[A](i: scala.Iterable[A]): java.util.List[A] = {
val result = new java.util.ArrayList[A]
i.foreach(result.add)
result
}



Example using javaMutableList :



val names = List("1","2","3","4","5","6","7")

val jlist: java.util.List[String] = names
jlist: java.util.List[String] = [1, 2, 3, 4, 5, 6, 7]

val shuffled = java.util.Collections.shuffle(jlist)
shuffled: Unit = ()

println(jlist)
[4, 5, 3, 7, 2, 6, 1]

Mittwoch, 17. Februar 2010

Arabic-Roman numerals converter written in Scala

A simple arabic to roman numeral converter written in Scala


import scala.collection.SortedMap

def roman(x:Int): String = {

val romanLiteral = SortedMap(1000 -> "M", 900 -> "CM", 500 -> "D", 400 -> "CD",
100 -> "C", 90 -> "XC", 50 -> "L", 40 -> "XL", 10 -> "X", 9 -> "IX", 5 ->
"V", 4 -> "IV", 1 -> "I")

val arab = romanLiteral.keySet.toList.reverse

def r(n:Int,idx:Int,romanNumber:StringBuilder): String = {

val a = arab(idx)

if (n==0)
romanNumber.toString
else if ( n >= a )
r( n-a, idx, romanNumber append romanLiteral(a) )
else
r( n, idx+1, romanNumber )
}

r(x,0,new StringBuilder())

}

(1 to 100).foreach( x => printf( "%s=%s\n", x, roman(x) ))