文件
import scala.io.Source
import java.io.PrintWriter
import java.io.File
object FileOps {
def main(args: Array[String]) {
val file = Source.fromFile("test.txt")
for (line <- file.getLines){ println(line)}
file.close
val webFile = Source.fromURL("http://spark.apache.org/")
webFile.foreach(print)
webFile.close
val writer = new PrintWriter(new File("scalaFile.txt" ))
for (i <- 1 to 100) writer.println(i)
writer.close()
print("Please enter your input : " )
val line = readLine
println("Thanks, you just typed: " + line)
}
正则表达式
object RegExpressOps {
def main(args: Array[String]): Unit = {
val regex="""([0-9]+) ([a-z]+)""".r
val numPattern = "[0-9]+".r
val numberPattern = """\s+[0-9]+\s+""".r
// findAllIn方法返回遍历所有匹配项的迭代器
for (matchString <- numPattern.findAllIn("99345 Scala, 22298 Spark")) println(matchString)
//99345
//22298
println(numberPattern.findFirstIn("99ss java, 222 hadoop"))
// Some( 222 ) 有Some 是因为有可能为空
println(numberPattern.findFirstIn("99ss java, 22a hadoop"))
// None
val numitemPattern="""([0-9]+) ([a-z]+)""".r
val numitemPattern(num, item) = "99 hadoop"
val line = "93459 spark" //93459 spark
// val line = "934s59 spark" //Oops..
line match{
case numitemPattern(num, blog) => println(num + "\t" + blog)
case _ => println("Oops...")
}
}
}
高阶函数
package functions
import scala.math._
object func {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
(1 to 9).map("*" * _).foreach(println _) //> *
//| **
//| ***
//| ****
//| *****
//| ******
//| *******
//| ********
//| *********
(1 to 9).filter(_ % 2 == 0) .foreach(println) //> 2
//| 4
//| 6
//| 8
println((1 to 9).reduceLeft(_ * _)) //> 362880
"Spark is the most exciting thing happening in big data today".split(" ").
sortWith(_.length < _.length).foreach(println)
//> is
//| in
//| the
//| big
//| most
//| data
//| Spark
//| thing
//| today
//| exciting
//| happening
val fun = ceil _ //> fun : Double => Double = <function1>
val num = 3.14 //> num : Double = 3.14
println(fun(num) ) //> 4.0
Array(3.14, 1.42, 2.0).map(fun).foreach(println)
//> 4.0
//| 2.0
//| 2.0
val triple = (x: Double) => 3 * x //> triple : Double => Double = <function1>
Array(3.14, 1.42, 2.0).map((x: Double) => 3 * x)
//> res0: Array[Double] = Array(9.42, 4.26, 6.0)
Array(3.14, 1.42, 2.0).map{ (x: Double) => 3 * x }
//> res1: Array[Double] = Array(9.42, 4.26, 6.0)
def high_order_functions(f: (Double) => Double) = f(0.25)
//> high_order_functions: (f: Double => Double)Double
println(high_order_functions(ceil _)) //> 1.0
println(high_order_functions(sqrt _)) //> 0.5
def mulBy(factor: Double) = (x: Double) => factor * x
//> mulBy: (factor: Double)Double => Double
val quintuple = mulBy(5) //> quintuple : Double => Double = <function1>
println(quintuple(20)) //> 100.0
println(high_order_functions((x: Double) => 3 * x))
//> 0.75
high_order_functions((x) => 3 * x) //> res2: Double = 0.75
high_order_functions(x => 3 * x) //> res3: Double = 0.75
println(high_order_functions(3 * _)) //> 0.75
val fun2 = 3 * (_: Double) //> fun2 : Double => Double = <function1>
val fun3: (Double) => Double = 3 * _ //> fun3 : Double => Double = <function1>
}
闭包
val data = List(1, 2, 3, 4, 5, 6) //> data : List[Int] = List(1, 2, 3, 4, 5, 6)
var sum = 0 //> sum : Int = 0
data.foreach(sum += _)
def add(more: Int) = (x: Int) => x + more
//> add: (more: Int)Int => Int
val a = add(1) //> a : Int => Int = <function1>
val b = add(9999) //> b : Int => Int = <function1>
println(a(10)) //> 11
println(b(10)) //> 10009
偏函数
val data = List(1, 2, 3, 4, 5, 6) //> data : List[Int] = List(1, 2, 3, 4, 5, 6)
data.foreach(println _) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
data.foreach(x => println(x)) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
def sum(a: Int, b: Int, c: Int) = a + b + c //> sum: (a: Int, b: Int, c: Int)Int
println(sum(1, 2, 3)) //> 6
// 偏函数, 不需要提供所有参数
// 有几个参数被实例化,其他参数没有实例化
//PartialAppliedFuntion 命名的意义
val fp_a = sum _ //> fp_a : (Int, Int, Int) => Int = <function3>
println(fp_a(1, 2, 3)) //> 6
println(fp_a.apply(1, 2, 3)) //> 6
val fp_b = sum(1, _: Int, 3) //主要是这里 //> fp_b : Int => Int = <function1>
println(fp_b(2)) //> 6
println(fp_b(10)) //> 14
data.foreach(println(_)) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6
data.foreach(println) //> 1
//| 2
//| 3
//| 4
//| 5
//| 6