Dienstag, 6. Oktober 2009

learning Scala 2

this tutorial based on:

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”


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

(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

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)=
def filesContaining(query: String)=
def filesRegex(query: String)=

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)=

define different functions

filesMatching(matcher: String => Boolean)

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

Keine Kommentare:

Kommentar veröffentlichen