Sonntag, 4. April 2010

Freitag, 2. April 2010

Numbers to german words


#!/bin/sh


exec scala $0 $@


!#


object NumSpeller {


val digits = Array("","ein","zwei","drei","vier","fuenf","sechs","sieben","acht","neun")


val teens = Array("zehn", "elf", "zwoelf", "dreizehn", "vierzehn", "fuenfzehn", "sechzehn", "siebzehn", "achtzehn", "neunzehn")


val tens = Array("", "zehn", "zwanzig", "dreissig", "vierzig", "fuenfzig", "sechzig", "siebzig", "achtzig", "neunzig")


val thousands = Array("", "tausend", "million", "milliarde", "billion", "billiarde", "trillion", "trilliarde")


val thousands2 = Array("", "tausend", "millionen", "milliarden", "billionen", "billiarden", "trillionen", "trilliarden")



def spellGroup(num:Int, factor:Int) = {


val (v3, v2, v1) = ((num / 100) % 10, (num / 10) % 10, num % 10)


val di = v1 match {


case 1 => if (factor >= 2) { "eine" } else if (factor >= 1) { "ein" } else "eins"


case _ => digits(v1)


}



val ts = v2 match {


case 0 => di


case 1 => teens(v1)


case _ => if (v1>0) di + "und" + tens(v2) else tens(v2)


}



val hs = v3 match {


case 0 => ts


case _ => digits(v3) + "hundert" + ts


}



if (v1==1 && v3==0 && v2==0)


hs + thousands(factor)


else


hs + thousands2(factor)


}



def numberGroups(num:Long) = {


def _numberGroups(num:Long, factor:Int):List[(Double,Int)] = factor match {


case 0 => List((num % 1000,0))


case _ => ((num / Math.pow(1000, factor)) % 1000, factor) :: _numberGroups(num, factor - 1)


}


val ints = _numberGroups(num, 6) map (x => (x._1.asInstanceOf[Int],x._2))


//ints filterNot (x => x._1 == 0.0) foreach ( x => printf("(%d,%d)\n",x._1,x._2) )


ints filterNot (x => x._1 == 0.0)


}



def spell(num:Long) = num match {


case 0 => "Null"


case _ => (numberGroups(num) map { x => spellGroup(x._1,x._2) }).mkString.trim


}



}



object n2w {


def main(args:Array[String]) = {


val num = args(0).toLong


printf("%d=%s\n",num,NumSpeller.spell(num))


}


}



n2w.main(args)



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)