defeval(t: Tree, env: Environment): Int = t match { caseSum(l, r) => eval(l, env) + eval(r, env) caseVar(n) => env(n) caseConst(v) => v }
defderive(t: Tree, v: String): Tree = t match { caseSum(l, r) => Sum(derive(l, v), derive(r, v)) caseVar(n) if (v == n) => Const(1) case _ => Const(0) }
defmain(args: Array[String]) { val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Const(7), Var("y"))) val env: Environment = {case"x" => 5case"y" => 7} println("Expression: " + exp) println("Evalution with x=5, y=7: " + eval(exp, env)) println("Derivative relative to x:\n" + derive(exp, "x")) println("Derivative relative to y:\n" + derive(exp, "y")) } }
该示例主要用来说明两种 case 关键字,分别为:case class 和 ... match case ...,前者可认为是一个结构体,实例化时可以省略 new 关键字,参数有默认的 getter 函数,整个 case class 有默认的 equals 和 hashCode 方法实现,通过这两个方式可实现根据值判断类的两个实例是否相等,而不是通过引用,条件类同样有默认的 toString 方法实现;后者可认为是一种特殊的 switch case ,只不过 case 的判定和执行是函数式的,case class 可直接参与 match case 的判定(判定是不是属于该类)。第 7 行中有个 type 关键字,可认为是定义了一种新的类型(不是数据类型),示例中是函数类型,通过这个 type ,可直接将字符串映射为整型,23 行中将这个 type 与 case 结合使用,定义多个字符串映射多个整型的变量。第 18 行中有个 _ ,这是 scala 中的通配符,不同的语义下表示的含义不同,这里的含义是指,当上面的模式都不匹配时,将执行这个,相当于 switch case 中的 default。
objectQuickSort{ defqSort(xs: Array[Int]) { defswap(i: Int, j: Int) { val t = xs(i); xs(i) = xs(j); xs(j) = t; }
defsort(l: Int, r: Int) { val pivot = xs(l); var i = l+1; var j = r; while (i < j) { while (i <= r && xs(i) < pivot) i += 1; while (j > l && xs(j) > pivot) j -= 1;
if (i < j) { swap(i, j); i += 1; j -= 1; }
if (i > j) { i = j; } } while (i > l && xs(i) > pivot) { i -= 1; j -= 1; } swap(i, l);
if (l < j-1) sort(l, j-1); if (j+1 < r) sort(j+1, r); }
classHelloActorextendsActor{ defact() { while (true) { receive { case name: String => println("Hello, " + name) } } } }
objectAuctionService{ defmain(args: Array[String]) { val seller: Actor = newHelloActor val client: Actor = newHelloActor val minBid = 10 val closing = newDate()
val helloActor = newHelloActor helloActor.start() helloActor ! "leo" } }
通过重写 Actor 中的 act 方法即可简单的实现多线程编程,Actor 中有个特殊的标识符 !,该符号其实是是一种缩写,即可将 helloActor.!("leo") 缩写为 helloActor ! "leo",代表将数据传递给 Actor,由 Actor 内部的 receive case 接受数据并处理,当然也可通过 receiveWithin 控制数据传递时间,若超时,则默认触发 TIMEOUT 处理模式。
val n = xs.length / 2 if (n == 0) xs else merge(mergeSort(less)(xs take n), mergeSort(less)(xs drop n)) }
defmain(args: Array[String]) { val xs = List(4, 1, 5, 7,7,7,7, 2, 6); println(xs(0), xs(1), xs(xs.length-1)) val ys = mergeSort((x: Int, y: Int) => x > y)(xs); println(ys.mkString(" ")) } }
List 中有两个操作符非常类似,即 :: 和 :::, 前者用于 List 中的元素和 List 连接,即创建一个新 List,新 List 为原 List 头插入元素后的 List,后者用于连接两个 List,即创建一个新 List ,新 List 为将第二个 List 的元素全部放入第一个 List 尾部的 List。字符 Nil 代表空 List 和 List() 等效,head 方法返回 List 的第一个元素,tail 方法返回除第一个元素之外的其它所有元素,还是一个 List,isEmpty 方法当 List 为空时返回 true。List 的 case-match 方法中,case y :: ys 其中 y 代表 xs.head,ys 代表 xs.tail。(xs take n) 表示取 List 前 n 个元素,(xs drop n) 表示取 List 前 n 个元素之外的元素,即与 (xs take n) 取得元素正好互补,而 (xs split n) 返回一个元组,元组中第一个元素为 (xs take n),第二个元素为 (xs drop n)。关于 List 还有些更高阶得方法:filter,map, flatMap, reduceRight, foldRight 等方法就不继续写了。至于动态 List 可用 ListBuffer 结构,当然 Scala 中直接用 Seq 作为返回值和参数一般会更好些。
defeval(t: Tree, env: Environment): Int = t match { caseSum(l, r) => eval(l, env) + eval(r, env) caseVar(n) => env(n) caseConst(v) => v }
defderive(t: Tree, v: String): Tree = t match { caseSum(l, r) => Sum(derive(l, v), derive(r, v)) caseVar(n) if (v == n) => Const(1) case _ => Const(0) }
defmain(args: Array[String]) { val exp: Tree = Sum(Sum(Var("x"), Var("x")), Sum(Const(7), Var("y"))) val env: Environment = {case"x" => 5case"y" => 7} println("Expression: " + exp) println("Evalution with x=5, y=7: " + eval(exp, env)) println("Derivative relative to x:\n" + derive(exp, "x")) println("Derivative relative to y:\n" + derive(exp, "y")) } }
该示例主要用来说明两种 case 关键字,分别为:case class 和 ... match case ...,前者可认为是一个结构体,实例化时可以省略 new 关键字,参数有默认的 getter 函数,整个 case class 有默认的 equals 和 hashCode 方法实现,通过这两个方式可实现根据值判断类的两个实例是否相等,而不是通过引用,条件类同样有默认的 toString 方法实现;后者可认为是一种特殊的 switch case ,只不过 case 的判定和执行是函数式的,case class 可直接参与 match case 的判定(判定是不是属于该类)。第 7 行中有个 type 关键字,可认为是定义了一种新的类型(不是数据类型),示例中是函数类型,通过这个 type ,可直接将字符串映射为整型,23 行中将这个 type 与 case 结合使用,定义多个字符串映射多个整型的变量。第 18 行中有个 _ ,这是 scala 中的通配符,不同的语义下表示的含义不同,这里的含义是指,当上面的模式都不匹配时,将执行这个,相当于 switch case 中的 default。
objectQuickSort{ defqSort(xs: Array[Int]) { defswap(i: Int, j: Int) { val t = xs(i); xs(i) = xs(j); xs(j) = t; }
defsort(l: Int, r: Int) { val pivot = xs(l); var i = l+1; var j = r; while (i < j) { while (i <= r && xs(i) < pivot) i += 1; while (j > l && xs(j) > pivot) j -= 1;
if (i < j) { swap(i, j); i += 1; j -= 1; }
if (i > j) { i = j; } } while (i > l && xs(i) > pivot) { i -= 1; j -= 1; } swap(i, l);
if (l < j-1) sort(l, j-1); if (j+1 < r) sort(j+1, r); }
classHelloActorextendsActor{ defact() { while (true) { receive { case name: String => println("Hello, " + name) } } } }
objectAuctionService{ defmain(args: Array[String]) { val seller: Actor = newHelloActor val client: Actor = newHelloActor val minBid = 10 val closing = newDate()
val helloActor = newHelloActor helloActor.start() helloActor ! "leo" } }
通过重写 Actor 中的 act 方法即可简单的实现多线程编程,Actor 中有个特殊的标识符 !,该符号其实是是一种缩写,即可将 helloActor.!("leo") 缩写为 helloActor ! "leo",代表将数据传递给 Actor,由 Actor 内部的 receive case 接受数据并处理,当然也可通过 receiveWithin 控制数据传递时间,若超时,则默认触发 TIMEOUT 处理模式。
val n = xs.length / 2 if (n == 0) xs else merge(mergeSort(less)(xs take n), mergeSort(less)(xs drop n)) }
defmain(args: Array[String]) { val xs = List(4, 1, 5, 7,7,7,7, 2, 6); println(xs(0), xs(1), xs(xs.length-1)) val ys = mergeSort((x: Int, y: Int) => x > y)(xs); println(ys.mkString(" ")) } }
List 中有两个操作符非常类似,即 :: 和 :::, 前者用于 List 中的元素和 List 连接,即创建一个新 List,新 List 为原 List 头插入元素后的 List,后者用于连接两个 List,即创建一个新 List ,新 List 为将第二个 List 的元素全部放入第一个 List 尾部的 List。字符 Nil 代表空 List 和 List() 等效,head 方法返回 List 的第一个元素,tail 方法返回除第一个元素之外的其它所有元素,还是一个 List,isEmpty 方法当 List 为空时返回 true。List 的 case-match 方法中,case y :: ys 其中 y 代表 xs.head,ys 代表 xs.tail。(xs take n) 表示取 List 前 n 个元素,(xs drop n) 表示取 List 前 n 个元素之外的元素,即与 (xs take n) 取得元素正好互补,而 (xs split n) 返回一个元组,元组中第一个元素为 (xs take n),第二个元素为 (xs drop n)。关于 List 还有些更高阶得方法:filter,map, flatMap, reduceRight, foldRight 等方法就不继续写了。至于动态 List 可用 ListBuffer 结构,当然 Scala 中直接用 Seq 作为返回值和参数一般会更好些。