Posts mit dem Label Scala werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Scala werden angezeigt. Alle Posts anzeigen

Dienstag, 13. Oktober 2009

Scala test

http://www.scalatest.org/getting_started_with_feature_spec

Freitag, 9. Oktober 2009

read file in Scala

read file in scala:

for (file <- new File("/var/log/").listFiles) {


list a directory

if (!file.isDirectory) {

ask is file not a directory

simple print out of a file

import scala.io.Source

for {
(line) <- Source.fromFile("bla.txt").getLines
} print(line)


read a file in an iterator

scala> val lines = scala.io.Source.fromFile("/var/log/syslog")
lines: scala.io.Source = non-empty iterator

scala> lines
res5: scala.io.Source = non-empty iterator


read a file in a string

val lines = scala.io.Source.fromFile("/var/log/syslog").mkString

// for utf-8 encoding
val lines = scala.io.Source.fromFile("file.txt", "utf-8").getLines.mkString



or you can use Java API

val os = new BufferedOutputStream(new FileOutputStream("gen/" + file.getName))

Donnerstag, 8. Oktober 2009

Challenge: pattern syslog

Challenge:

filtern some patern in file /var/log/syslog
and print it out


Shell:
fgrep usb /var/log/syslog


Java:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class JavaFilters {
public static void main(String[] args) {
String path="/var/log/syslog";
try {
BufferedReader input = new BufferedReader(new FileReader(path));

String line;
try {
Pattern p = Pattern.compile("dhclient");
while (( line = input.readLine()) != null){
Matcher m = p.matcher(line);
if(m.find())
System.out.println("found:: "+line);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}


Scala:

import scala.io.Source
import java.util.regex._

var p = Pattern.compile("usb")

for ( line <- Source.fromFile("/var/log/syslog").getLines ){
var m = p.matcher(line);
if( m.find )
print("found::" + line)
}


Perl:

#!/usr/bin/perl

open(IN, "/var/log/syslog") or die "canot open file: $!";

@lines = <IN>;
close IN;

foreach $line(@lines) {
if( $line =~ /dhclient/) {
print "found:: $line";
}
}


Ruby:

#!/usr/bin/ruby

file = File.new("/var/log/syslog", "r") or die "canot open file"

while (line = file.gets)
if ( line =~ /usb/ )
print "found:: "+line
end
end


Python:

#!/usr/bin/python

try:
f = open('/var/log/syslog', 'r')

for line in f.readlines():
if( "usb" in line ):
print("found:: %s"%line)

except:
print "error in file"


PHP:

<?php
$file = "/var/log/syslog";
if (file_exists($file)) {
$lines = file($file);
foreach ($lines as $line_num => $line)
if( preg_match("/usb/", $line) )
echo "$line";
}
?>


C:

#include
#include
#include
#include
#include

FILE *file = NULL;

int main(int argc, char **argv)
{
char line[1024];
int err;
char err_str[200];
char results[500];

regex_t preg;
regmatch_t pmatch[100];

if( (regcomp(&preg, argv[1], 0)) != 0) {
printf("regcomp error\n");
return -1;
}

if((file = fopen("/var/log/syslog", "r")) != NULL) {
while (!feof(file)){
fgets(line, 1024, file);

if( regexec(&preg, line, preg.re_nsub, pmatch, 0) == 0) {
printf("found:: %s", line);
}
}
}

regfree(&preg);
return 0;
}

Dienstag, 6. Oktober 2009

learning Scala 3

this tutorial based on:
http://www.artima.com/scalazine/articles/steps.html


def max(x: Int, y: Int): Int = if (x < y) y else x


def max(x: Int, y: Int) -> define a function, with 2 parameters (integer)
: Int -> return is integer.
if (x < y) y else x -> function body



scala> def greet() = println("Hello, world!")
greet: ()Unit

scala> greet()
Hello, world!

scala> greet
Hello, world!


greet save a Unit -> a function


args.foreach(arg => println(arg))
args.foreach((arg: String) => println(arg))
args.foreach(println)

for (arg <- args)
println(arg)


scala> for (i <- 0 to 5)
| println(i)
0
1
2
3
4
5



val greetStrings: Array[String] = new Array[String](3)

create an array with 3 strings


greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"


important indexes are in brakes () not square brackets[]


for (i <- 0 to 2)
print(greetStrings(i))

learning Scala 2

this tutorial based on:
http://www.thomasknierim.com/104/scala/scala-tutorial-1/
http://www.thomasknierim.com/104/scala/scala-tutorial-2/
http://www.thomasknierim.com/104/scala/scala-tutorial-3/


object Hello {
def main(args: Array[String]) {
for(val arg: String <- args)
System.out.println("Hello, " + arg + "!");
}
}


for(val arg: String <- args) -> an array into each arg as String

similar: args.foreach(arg => println("Hello" + arg + "!"))
"=>" that can be read “with a given argument this do that”

args.foreach(println)


var decrease = (x: Int) => x - 1
decrease(11)


(x: Int) => x - 1 -> anonymous function declaration


var decrease = (x: Int) => x – something

something is not defined yet. decrease is depend on context -> clousure

In terms of computer science, a closure is a function that is evaluated and whose result depend on an outer context.

something = 10
decrease(100)


some examples for higher-order functions

scala> List("alpha", "beta", "crash").map(
p => p.substring(0,1))
res0: List[java.lang.String] = List(a, b, c)

scala> List(1, 2, 3) map (_ * 2)
res1: List[Int] = List(2, 4, 6)

scala> List("Tango", "Mango", "Bobo", "Gogo")
map (_.endsWith("go"))
res2: List[Boolean] = List(true, true, false, true)



List("alpha", "beta", "crash") -> create a list

map() -> map each list element to some condition/function

p => p.substring(0,1) p is the element p.substring(0,1) is the function

_ -> ist the element (similar to perl $_ )
(_*2) is equivalent to (i => i * 2)




object FileMatcher {

private val filesHere=(new java.io.File(".")).listFiles

private def filesMatching(matcher: String => Boolean)=
for (file <- filesHere; if matcher(file.getName))
yield file

def filesEnding(query: String)=
filesMatching(_.endsWith(query))
def filesContaining(query: String)=
filesMatching(_.contains(query))
def filesRegex(query: String)=
filesMatching(_.matches(query))
}

object Main {
def main(args: Array[String]) {
val query = args(0);
var matchingFiles = FileMatcher.filesEnding(query)
matchingFiles = FileMatcher.filesContaining(query)
matchingFiles = FileMatcher.filesRegex(query)
}
}


let look:

scala> (new java.io.File(".")).listFiles
res2: Array[java.io.File] = Array(./FrenchDate$.class, ./FrenchDate.class, ./Complex.scala, ./s1.class, ./s1$.class, ./FrenchDate.scala, ./Timer$$anonfun$main$1.class, ./s1.scala, ./Timer$.class, ./Hello.class, ./ComplexNumbers.class, ./Hello.scala, ./Hello$$anonfun$main$1.class, ./blog_scala2.txt, ./Timer.class, ./Hello$.class, ./Complex.class, ./blog_scala1.txt, ./ComplexNumbers.sca...


new java.io.File(".") use the java api and create a file object File.
listFiles is the same as listFiles() in java. in scala you don't need brakes.

for (file <- filesHere; if matcher(file.getName))
yield file

iterate all filesHere (files in path "."). if name match to query, yield the file. the file get in an array.


scala> var matchingFiles = FileMatcher.filesEnding(query)
matchingFiles: Array[java.io.File] = Array(./Complex.scala, ./FrenchDate.scala, ./s1.scala, ./Hello.scala, ./Main.scala, ./ComplexNumbers.scala, ./Timer.scala)


def filesEnding(query: String)=
filesMatching(_.endsWith(query))


define different functions


filesMatching(matcher: String => Boolean)

define the function with a matcher(String) which return Boolean

learning Scala

This tutorial based on:
http://www.scala-lang.org/docu/files/ScalaTutorial.pdf
http://www.thomasknierim.com/77/scala/scala-tutorial-2/



write a file s1.scala

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}


object -> declare a static singleton object
args: Array[String] -> a variable args as Array of Strings

compile and execute

$ scalac s1.scala
$ ls
s1.class s1$.class s1.scala


you get created standard java class files, but you must use java interpreter with setted parameters.


$ java s1
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
....

$ file /usr/bin/scala
$ /usr/bin/scala: POSIX shell script text executable
$ cat /usr/bin/scala
#!/bin/sh

JAVA_DIR=/usr/share/java
LIB_CLASSPATH=$JAVA_DIR/scala-library.jar:$JAVA_DIR/scala-compiler.jar:$JAVA_DIR/jline.jar

exec ${JAVACMD:=java} ${JAVA_OPTS:=-Xmx256M -Xms16M} \
-Xbootclasspath/a:$LIB_CLASSPATH \
-classpath .:$CLASSPATH:$LIB_CLASSPATH \
scala.tools.nsc.MainGenericRunner \
"$@"

$ scala s1
hallo


As you can see, the scala executable is a simple shell script, which start the java virtual machine with specific parameters and the scala library.


Create Date.scala

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object FrenchDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
}








import java.util.{Date, Locale} import Date and Locale from java.util
import java.text.DateFormat._ DateFormat._ is similar to DateFormat.* in java
val declare an inmutable variable
val now = new Date simmilar to "Date now = new Date();" in java
println(df format now) "df format now" is similar to "df.format(now)"




In scala is everything an object. There is no primitive datatypes (that is similar to ruby).

Because scala based on functional programming functions are objects too.


object Timer {

def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}

def timeFlies() {
println("time flies like an arrow...")
}

def main(args: Array[String]) {
oncePerSecond(timeFlies)
}
}


with functions as object you can very simple code a callback.





timeFlies() our callback function.
callback: () => Unit parameter for a function. ()=>Unit is the type of all functions which take no arguments and return nothing.
Thread sleep 1000 is similar to Thread.sleep(1000)


rather the function timeFlies() you can use anonymous functions ala:

oncePerSecond(() => println("time flies like an arrow..."))

"() => code" declare an anonymous function



Clases:


class Complex(real: Double, imaginary: Double) {
def re() = real
def im() = imaginary
}


define class Complex with two arguments,
def re() and im() are getters. return type of these two methods is not given explicitly


object ComplexNumbers {
def main(args: Array[String]) {
val c = new Complex(1.2, 3.4)
println("imaginary part: " + c.im())
}
}


if you have no arguments in function, write: def re = real


class Complex(real: Double, imaginary: Double) {
def re = real
def im = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}


class Complex(real: Double, imaginary: Double) -> primary constructor, which is part of the class declaration
override -> for overriding functions

(if (im < 0) "" else "+") have you see it ;)




class Rational(numerator: Int, denominator: Int) {

require(denominator != 0)

private val gcd = greatestCommonDivisor(numerator.abs,
denominator.abs)
val n = numerator / gcd
val d = denominator / gcd

def this(n: Int) = this(n, 1)

private def greatestCommonDivisor(a: Int, b: Int): Int =
if (b == 0) a else greatestCommonDivisor(b, a % b)

def + (that: Rational): Rational =
new Rational(n * that.d + d * that.n, d * that.d)

def - (that: Rational): Rational =
new Rational(n * that.d - d * that.n, d * that.d)

def * (that: Rational): Rational =
new Rational(n * that.n, d * that.d)

def / (that: Rational): Rational =
new Rational(n * that.d, d * that.n)

override def toString = n + "/" + d
}



require(denominator != 0) -> look that variable is not null, throw exception


def this(n: Int) = this(n, 1) -> create from Rational(2) -> Rational(2,1)

you can see +-*/ are objects functions, which can be simple overrided

Samstag, 3. Oktober 2009

trying Scala ... 3

List:

scala> var lst = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)
res29: List[Int] = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)


scala> def odd(inLst:List[Int]):List[Int]={
| if(inLst==Nil) Nil
| else if(inLst.head%2==1) inLst.head::odd(inLst.tail)
| else odd(inLst.tail)
| }
odd: (List[Int])List[Int]


def define a function/method
od() name
inLst:List[Int] => inLst parameter name with a List type of Integers
:List[Int] return a List of Integers
Nil
similar to none, None, null, void
inLst.head first element
inLst.tail all other elements after first element



scala> odd(lst)
res32: List[Int] = List(1, 7, 5, 3, 9)

trying Scala ... 2


scala> val try1=if (1==2) 8 else 9
try1: Int = 9

scala> var total=18
total: Int = 18
scala> while(total < 17) total+=3
scala> total
res15: Int = 18

scala> for(i <- 1 to 4) println("hi five")
hi five
hi five
hi five
hi five

scala> for(i <- 1 until 5 ; j <- 1 to 3) println(i,j)
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
(4,1)
(4,2)
(4,3)

scala> for(c<-"hello")println(c)
h
e
l
l
o


hex, octal, bin is the same as in java


scala> val abyte: Byte = 27
abyte: Byte = 27


scala> var chr='A'
chr: Char = A
scala> chr=5
chr: Char =
scala> chr=66
chr: Char = B


define functions:

scala> def max(x: Int, y: Int): Int = {
| if (x > y) x
| else y
| }
max: (Int,Int)Int

scala> max(6,7)
res22: Int = 7

scala> max(3.6, 4)
:6: error: type mismatch;
found : Double(3.6)
required: Int
max(3.6, 4)



define a class


scala> class Point {
| var x=0
| var y=0
| }
defined class Point


scala> val p=new Point
p: Point = Point@18b62e0

scala> p.x=3
scala> p.y=4


scala> class Point( ix:Int,iy:Int){
| var x=ix
| var y=iy
| def vectorAdd(newpt:Point):Point={
| new Point(x+newpt.x,y+newpt.y)
| }
| }
defined class Point

scala> val p1=new Point(3,4)
p1: Point = Point@75455c
scala> val p2=new Point(7,2)
p2: Point = Point@1ba3523
scala> val p3=p1.vectorAdd(p2)
p3: Point = Point@1095c6c
scala> println(p3.x,p3.y)
(10,6)


scala> class Point( ix:Int,iy:Int){
| var x=ix
| var y=iy
| def +(newpt:Point):Point={
| new Point(x+newpt.x,y+newpt.y)
| }
| def -(newpt:Point):Point={
| new Point(x-newpt.x,y-newpt.y)
| }
| override def toString="Point("+x+","+y+")"
| }
defined class Point

scala> val p1=new Point(3,4)
p1: Point = Point(3,4)
scala> val p2=new Point(7,2)
p2: Point = Point(7,2)
scala> val p3=new Point(-2,2)
p3: Point = Point(-2,2)

scala> val p4=p1+p2-p3
p4: Point = Point(12,4)
scala> println(p4.x,p4.y)
(12,4)


there is no switch -> use match

scala> def decode(n:Int){
| n match {
| case 1 => println("One")
| case 2 => println("Two")
| case 5 => println("Five")
| case _ => println("Error")
| }
| }
decode: (Int)Unit









trying Scala ...

install scala, and type scala in console ;)


scala> 4/3.0
res5: Double = 1.3333333333333333

scala> 4/0
java.lang.ArithmeticException: / by zero
at .(:5)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAcce...

scala> var a=3
a: Int = 3

scala> a
res7: Int = 3

scala> a=5
a: Int = 5


scala> val b=3
b: Int = 3

scala> b
res10: Int = 3

scala> b=5
<console>:5: error: reassignment to val
b=5


var and val define variables, val is immutable

For printing is here
Ascala> println(b)
3


operators and control sturctures (if, else, ...) are the same as in java.


scala> val isBook = 6>=3
isBook: Boolean = true

scala> val price=16
price: Int = 16

scala> val vol=10
vol: Int = 10

scala> val sale=if (isBook)price*vol else price/vol
sale: Int = 160

scala> sale
res13: Int = 160

trying Scala

Tutorials: http://www.simplyscala.com/

Book: http://programming-scala.labs.oreilly.com/

further info from http://www.scala-lang.org/

http://www.scala-lang.org/node/1305